如何在使用 Ant 构建 jar 文件时设置类路径顺序? [英] How to set classpath order while building jar file using Ant?
问题描述
我正在使用 zipgroupfileset 将 lib 文件夹中的所有 jar 捆绑到我的可执行应用程序 jar 文件中.
I am using zipgroupfileset to bundle all jars from lib folder to include in my executable application jar file.
<zipgroupfileset dir="${lib.dir}" />
必须以某种默认顺序包含罐子.我想更改此默认顺序.
Jars must be getting included in some default order. I want to alter this default order.
关于我为什么要这样做的说明:我的 Java 桌面应用程序使用了许多包含在类路径中的第三方 jar.当我通过 Eclipse 运行我的代码时,它工作正常.但是当我使用 ANT 构建 jar 文件时,它无法按预期工作.我确定它与类路径中的 jar 序列有关,就像我在 Eclipse 中更改 jar 顺序一样,它也会在那里失败.
A note on why I want this: my Java desktop application uses many third party jars which are included in class path. When I run my code through Eclipse it works fine. But when I build the jar file using ANT it doesn't work as expected. I am sure it is related to jar sequence in classpath as if I change jar order in Eclipse it fails there as well.
注意:我使用的是 Eclipse Kepler、Java 7、Ant 1.8.
Note: I am using Eclipse Kepler, Java 7, Ant 1.8.
推荐答案
我终于找到了解决方案.我在更改我的 ANT 文件后构建了 Jar.现在,我的 ANT 构建在类路径中明确包含了 3rd 方 API,这是解决方案的关键.
Finally I got the solution. I build the Jar after altering my ANT file. Now my ANT build includes 3rd party APIs explicitly in classpath and that was the key to the solution.
<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>
以前我在 ANT 中有类路径条目,如下所示:
Previously I had class-path entry in ANT as follows:
<attribute name="Class-Path" value="." />
这篇关于如何在使用 Ant 构建 jar 文件时设置类路径顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!