有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Ant build在生成清单时创建了不正确的JAR相对路径

我正在使用Ant构建Java应用程序并生成清单。MF文件,因此它包含我的lib目录中的所有JAR

这似乎是可行的,但问题是不是将它们写成lib/some。jar,它包括我的Eclipse项目的名称:MyProject/lib/some。罐子

这当然是不正确的,当作为独立应用程序运行时,会导致找不到任何jar

建造。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>

我的清单的外观示例:

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/”)

干杯, 巴特


共 (2) 个答案

  1. # 1 楼答案

    manifestclasspath期望jarfile属性指向JAR文件的位置(它可能还不存在,但没关系)。因为您在_deliverables/lib/app.jar创建JAR文件,并且还将所有库JAR从lib复制到_deliverables/lib,所以

    <manifestclasspath property="lib.list" jarfile="lib/app.jar">
        <classpath refid="jarlib" />
    </manifestclasspath>
    

    应该这样做,并将创建具有正确相对路径的最终类路径,即All-MB.jar activation.jar aspectjrt.jar

  2. # 2 楼答案

    必须在生成中设置项目目录。xml作为

    <property name="projectDir" value=".." />
    

    在此之后,您必须尝试使用相对路径处理所有内容。目前,问题背后的原因是正在使用绝对路径