如何在Netbeans的清单类路径中包含lib文件夹

时间:2009-11-09 17:29:52

标签: java ide netbeans classpath manifest.mf

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

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

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

<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>

感谢。

4 个答案:

答案 0 :(得分: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不会对其进行修改。

答案 1 :(得分:1)

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

我改变了:

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

为:

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

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

答案 2 :(得分:1)

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

答案 3 :(得分:0)

你的问题似乎是将你的log4j.xml文件存储在/ lib中的“globmapper” - 你想要它在“/”或jar上。