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

问题描述:

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

Netbeans会自动在应用程序jar中创建一个MANIFEST.MF文件,并创建一个名为lib /的文件夹,其中包含所有依赖关系。此清单指定一个覆盖命令行上提供的任何-cp参数的Class-Path属性。我可以在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> 

谢谢。

我发现了一种方法来实现这个修改build-impl.xml。

我改变:

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

到:

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

的问题是,NetBeans将覆盖它,因为会自动生成该文件。

+0

也许你可以在Netbeans之外建立一个自定义的ANT脚本,这样它就不会被覆盖。 – ssakl 2009-11-16 20:54:37

+0

我可能没有别的作品。我对Ant没有什么了解,也没有太多时间来学习Ant。我认为我的问题不会是一件容易的事。 :( – FabienB 2009-11-18 23:06:04

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

+0

如果我删除globmapper指令,那么Class-Path指向错误的文件夹(不是lib /)。同样的事情,如果我只是修改它。 jar文件已正确绑定。我想将lib /文件夹添加到Class-Path中。 – FabienB 2009-11-13 19:13:06

+0

然后,只需将''构建器更改为直接指向lib /,而不是'$ {run {classpath.without.build.classes.dir}'使用'$ {run.classpath.without.build.classes.dir }/lib' – Guss 2009-11-14 22:16:26

+1

更改会在类路径中添加一个lib/lib,这是不正确的。我希望lib /中的所有内容(jar和xml)。 此外,对于build-impl.xml的任何修改都会在Netbeans重新创建时丢失。(这看起来是随机的) – FabienB 2009-11-18 23:01:21

不应编辑build-impl.xml文件,而应将此条目添加到build.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文件中,它将覆盖的的“-DO清洁”节build-impl.xml文件包含:

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

此外,由于它在build.xml中,所以它不会被Netbeans修改。

您可以在项目的根文件夹(它是jar文件中的清单模板)中简单地关闭项目选项Build/Packaging/Copy Dependent Library和manualy edit manifest.mf。