我正在使用 Ant 构建我的 Java 应用程序并自动生成 MANIFEST.MF 文件,以便它包含我的 lib 目录中的所有 jar。
这似乎可行,但问题是,它不是将它们写为 lib/some.jar,而是包含我的 Eclipse 项目的名称:MyProject/lib/some.jar。
这当然是不正确的,并且会导致在作为独立应用程序运行时找不到任何 jar。
Build.xml(重要部分在最后):
<?xml version="1.0"?>
<project name="fidea_migration" default="dist">
<path id="compile.classpath">
<fileset dir="lib">
<include name="*.jar"/>
</fileset>
</path>
<target name="clean" description="cleaning the old deliverables">
<delete includeemptydirs="true">
<fileset dir="bin" includes="**/*"/>
</delete>
<delete includeemptydirs="true">
<fileset dir="_deliverables" includes="**/*"/>
</delete>
</target>
<target name="prepare" description="preparing the deliverables folders">
<mkdir dir="_deliverables/lib"/>
</target>
<path id="jarlib">
<fileset dir="lib/">
<include name="**/*.jar"/>
</fileset>
</path>
<manifestclasspath property="lib.list" jarfile=".">
<classpath refid="jarlib" />
</manifestclasspath>
<target name="compile" depends="clean, prepare" description="compiling java sources">
<mkdir dir="bin"/>
<javac srcdir="src/main/java" destdir="bin">
<classpath refid="compile.classpath"/>
</javac>
</target>
<target name="dist" depends="compile" description="creating binary distribution">
<copy todir="_deliverables/lib">
<fileset dir="lib"/>
</copy>
<copy todir="_deliverables">
<fileset dir="src/main/resources">
</fileset>
</copy>
<jar jarfile="_deliverables/lib/app.jar" basedir="bin">
<manifest>
<attribute name="Class-Path" value="${lib.list}"/>
</manifest>
</jar>
</target>
</project>
我的 list 的外观示例:
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.4
Class-Path: MyProject/lib/All-MB.jar MyProject/lib/activation.jar MyProject/lib/aspectjrt.jar
关于如何修复此问题的任何想法,以便它只显示 xxx.jar 而不是 MyProject/lib/xxx.jar (因此没有“MyProject/lib/”)?
干杯, 巴特
最佳答案
manifestclasspath
期望 jarfile
属性指向 JAR 文件的位置(该文件可能尚不存在,但没关系)。由于您在 _deliverables/lib/app.jar
创建 JAR 文件,并且还将所有 lib JAR 从 lib
复制到 _deliverables/lib
然后
<manifestclasspath property="lib.list" jarfile="lib/app.jar">
<classpath refid="jarlib" />
</manifestclasspath>
应该可以做到这一点,并将创建一个具有正确相对路径的最终类路径,即All-MB.jaractivation.jaraspectjrt.jar
等。
关于java - Ant 构建在生成 list 时创建了不正确的 jar 相对路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20683926/