简体   繁体   English

如何使用Ant构建jar文件时设置classpath顺序?

[英]How to set classpath order while building jar file using Ant?

I am using zipgroupfileset to bundle all jars from lib folder to include in my executable application jar file. 我正在使用zipgroupfileset从lib文件夹捆绑所有jar,以包含在我的可执行应用程序jar文件中。

<zipgroupfileset dir="${lib.dir}" />

Jars must be getting included in some default order. 罐子必须包含在某个默认顺序中。 I want to alter this default order. 我想改变这个默认顺序。

A note on why I want this: my Java desktop application uses many third party jars which are included in class path. 关于我为什么要这样做的说明:我的Java桌面应用程序使用许多包含在类路径中的第三方jar。 When I run my code through Eclipse it works fine. 当我通过Eclipse运行我的代码时它工作正常。 But when I build the jar file using ANT it doesn't work as expected. 但是当我使用ANT构建jar文件时,它无法按预期工作。 I am sure it is related to jar sequence in classpath as if I change jar order in Eclipse it fails there as well. 我确信它与classpath中的jar序列有关,就像我在Eclipse中更改jar顺序一样,它也失败了。

Note: I am using Eclipse Kepler, Java 7, Ant 1.8. 注意:我使用的是Eclipse Kepler,Java 7,Ant 1.8。

Finally I got the solution. 最后我得到了解决方案。 I build the Jar after altering my ANT file. 我在改变我的ANT文件后构建了Jar。 Now my ANT build includes 3rd party APIs explicitly in classpath and that was the key to the solution. 现在,我的ANT构建在classpath中明确包含第三方API,这是解决方案的关键。

<property name="lib.dir" value="lib" />

<manifestclasspath property="jar.classpath" jarfile="${lib.dir}/*.jar">
    <classpath refid="project.class.path"/>
</manifestclasspath>

<jar destfile="${jar.dir}/${jar.name}">
        <fileset dir="${class.root}" includes="**/*.*" />
        <manifest>
                <attribute name="Main-Class" value="${Main-Class}" />
                <attribute name="Class-Path" value="${jar.classpath}" />
        </manifest>
</jar>

Previously I had class-path entry in ANT as follows: 以前我在ANT中有类路径条目,如下所示:

<attribute name="Class-Path" value="." />

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

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