【问题标题】:How to include the lib folder in the manifest classpath in Netbeans如何在 Netbeans 的清单类路径中包含 lib 文件夹
【发布时间】:2010-12-14 17:20:14
【问题描述】:

我的 java 应用程序使用的库需要在类路径中查找文件 (log4j.xml)。我使用 netbeans 来管理我的项目,但我找不到包含 lib/ 文件夹的方法。

Netbeans 自动在应用程序 jar 中创建一个 MANIFEST.MF 文件,并创建一个名为 lib/ 的文件夹,其中包含所有依赖项。此清单指定了覆盖命令行上提供的任何 -cp 参数的 Class-Path 属性。我可以在 netbeans 的库面板中选择任意文件夹,但它会在清单的类路径中创建一个子文件夹。我想要 lib/ 文件夹中的所有依赖项和 log4j.xml 文件。

希望可以在 IDE 中执行此操作。我包含了一个自动生成的 build-impl.xml 文件的 sn-p。

<target depends="init,compile,-pre-pre-jar,-pre-jar" if="manifest.available+main.class+mkdist.available" name="-do-jar-with-libraries">
    <property location="${build.classes.dir}" name="build.classes.dir.resolved"/>
    <pathconvert property="run.classpath.without.build.classes.dir">
        <path path="${run.classpath}"/>
        <map from="${build.classes.dir.resolved}" to=""/>
    </pathconvert>
    <pathconvert pathsep=" " property="jar.classpath">
        <path path="${run.classpath.without.build.classes.dir}"/>
        <chainedmapper>
            <flattenmapper/>
            <globmapper from="*" to="lib/*"/>
        </chainedmapper>
    </pathconvert>
    <taskdef classname="org.netbeans.modules.java.j2seproject.copylibstask.CopyLibs" classpath="${libs.CopyLibs.classpath}" name="copylibs"/>
    <copylibs compress="${jar.compress}" jarfile="${dist.jar}" manifest="${manifest.file}" runtimeclasspath="${run.classpath.without.build.classes.dir}">
        <fileset dir="${build.classes.dir}"/>
        <manifest>
            <attribute name="Main-Class" value="${main.class}"/>
            <attribute name="Class-Path" value="${jar.classpath}"/>
        </manifest>
    </copylibs>
    <echo>To run this application from the command line without Ant, try:</echo>
    <property location="${dist.jar}" name="dist.jar.resolved"/>
    <echo>java -jar "${dist.jar.resolved}"</echo>
</target>

谢谢。

【问题讨论】:

    标签: java ide netbeans classpath manifest.mf


    【解决方案1】:

    看来您的问题是“globmapper”将您的 log4j.xml 文件存储在 /lib 中 - 您希望它在“/”或 jar 中。

    【讨论】:

    • 如果我删除 globmapper 指令,则 Class-Path 指向错误的文件夹(不是 lib/)。如果我只是修改它,同样的事情。 jar 文件已正确捆绑。我想将 lib/ 文件夹添加到 Class-Path。
    • 然后只需将&lt;path&gt; 构建器更改为直接指向lib/,所以不要使用${run.classpath.without.build.classes.dir},而是使用${run.classpath.without.build.classes.dir}/lib
    • 更改 会在类路径中添加一个 lib/lib,这也是不正确的。我想要 lib/.xml 中的所有内容(jar 和 xml)。此外,下次 Netbeans 重新创建 build-impl.xml 时,对它的任何修改都将丢失。 (虽然这似乎是随机的)
    • 那么这意味着 netbeans 试图对其构建过程进行智能化 - 当软件试图比开发人员更智能时,这总是问题的根源;-)。我会稍微玩一下 Netbeans 并尝试提出解决方案。
    【解决方案2】:

    我找到了一种修改 build-impl.xml 的方法。

    我变了:

    <attribute name="Class-Path" value="${jar.classpath}"/>
    

    到:

    <attribute name="Class-Path" value="${jar.classpath} /lib"/>
    

    问题是netbeans会覆盖它,因为这个文件是自动生成的。

    【讨论】:

    • 也许您可以在 Netbeans 之外构建一个自定义 ANT 脚本,这样它就不会被覆盖。
    • 我可能没有别的办法。我对Ant一无所知,也没有太多时间去学习Ant。我以为我的问题会很简单。 :(
    【解决方案3】:

    您应该将此条目添加到 build.xml 文件中,而不是编辑 build-impl.xml 文件。当您修改项目中与该项目的构建有关的任何内容时,它将生成一个新的 build-impl.xml 文件。

    这是我在 build.xml 文件中放入的示例:

    <target depends="init" name="-do-clean">
        <delete dir="${build.dir}"/>
        <delete file="${dist.jar}"/>
        <delete dir="${dist.dir}/lib"/>
        <delete dir="${dist.dir}/resources"/>
    </target>
    

    由于我将它放在 build.xml 文件中,它将覆盖 build-impl.xml 文件的“-do-clean”部分,其中包含:

    <target depends="init" name="-do-clean">
        <delete dir="${build.dir}"/>
        <delete dir="${dist.dir}" followsymlinks="false" includeemptydirs="true"/>
    </target>
    

    此外,由于它位于 build.xml 中,Netbeans 不会对其进行修改。

    【讨论】:

      【解决方案4】:

      您可以简单地关闭项目选项 Build/Packaging/Copy Dependent Library 并手动编辑项目根文件夹中的 manifest.mf(这是 jar 文件中清单的模板)。

      【讨论】:

        猜你喜欢
        • 2016-04-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-15
        • 1970-01-01
        • 2013-12-15
        • 2016-04-29
        • 1970-01-01
        相关资源
        最近更新 更多