简体   繁体   English

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

[英]How to include the lib folder in the manifest classpath in Netbeans

A library that my java application uses needs looks for a file (log4j.xml) in the class path. 我的Java应用程序使用的库需要在类路径中查找文件(log4j.xml)。 I use netbeans to manage my project, but I can't find a way to include the lib/ folder. 我使用netbeans管理我的项目,但是找不到包含lib /文件夹的方法。

Netbeans automatically creates a MANIFEST.MF file inside the application jar and also creates a folder called lib/ which includes all dependencies. Netbeans会在应用程序jar中自动创建一个MANIFEST.MF文件,并还会创建一个名为lib /的文件夹,其中包含所有依赖项。 This manifest specifies a Class-Path attribute that overrides any -cp argument provided on the command line. 该清单指定了Class-Path属性,该属性将覆盖命令行上提供的任何-cp参数。 I can select an arbitrary folder in netbeans' library panel, but it creates a sub folder in the manifest's classpath. 我可以在netbeans的库面板中选择一个任意文件夹,但是它将在清单的类路径中创建一个子文件夹。 I'd like all dependencies and the log4j.xml file inside the lib/ folder. 我想要所有依赖项和lib /文件夹中的log4j.xml文件。

Hopefully it's possible to do this in the IDE. 希望可以在IDE中执行此操作。 I include a snippet of the auto-generated build-impl.xml file. 我包含了自动生成的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>

Thanks. 谢谢。

Instead of editing the build-impl.xml file you should add this entry to the build.xml file. 代替编辑build-impl.xml文件,您应该将此条目添加到build.xml文件中。 When you modify anything in your project pertaining to the building of that project, it will generate a new build-impl.xml file. 当您在项目中修改与该项目的构建有关的任何内容时,它将生成一个新的build-impl.xml文件。

Here is an example of what I put in my build.xml file: 这是我放入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>

Since I put this in the build.xml file, it will override the "-do-clean" section of the build-impl.xml file which contains: 由于我将其放入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>

Furthermore, since it is in the build.xml it won't be modified by Netbeans. 此外,由于它位于build.xml中,因此Netbeans不会对其进行修改。

您只需关闭项目选项Build / Packaging / Copy Dependent Library,然后在项目的根文件夹(这是jar文件中的清单模板)中手动编辑manifest.mf。

I found a way to acheive this modifying the build-impl.xml. 我找到了一种修改build-impl.xml的方法。

I changed: 我变了:

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

to: 至:

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

The problem is that netbeans will overwrite it since this file is automatically generated. 问题在于,由于该文件是自动生成的,因此netbeans将覆盖它。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM