Java Apache ant 清单类路径?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/682852/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 17:51:31  来源:igfitidea点击:

Apache ant manifest class-path?

javaantmanifest.mf

提问by

I have a standard project layout for a java project:

我有一个 Java 项目的标准项目布局:

project /
    src /
        source_file_1.java
        ...
        source_file_N.java
    build /
          classes /
              source_file_X.class
              ...
          jar /
              MyJar.jar
    lib /
          SomeLibrary.jar
          SomeOtherLibrary.jar

As far as I can tell, I am building the project correctly with Ant. I need to set the class-path attribute in the Manifest file so my classes can use the required libraries.

据我所知,我正在使用 Ant 正确构建项目。我需要在 Manifest 文件中设置 class-path 属性,以便我的类可以使用所需的库。

The following relevant information from build.xml

以下相关信息来自build.xml

<target name="compile" depends="init">
    <javac srcdir="src" destdir="build\classes">
        <classpath id="classpath">
            <fileset dir="lib">
                <include name="**/*.jar" />
            </fileset>
        </classpath>
    </javac>
</target>

<target name="jar" depends="compile">
    <jar destfile="build\jar\MyJar.jar" basedir="build\classes" >
        <manifest>
            <attribute name="Built-By" value="${user.name}" />
        </manifest>
    </jar>
</target>

Any push in the right direction is appreciated. Thanks

任何朝正确方向的推动都值得赞赏。谢谢

采纳答案by Michael Myers

Looking at my NetBeans-generated build file, I found this snippet in the -do-jar-with-librariestask:

查看我的 NetBeans 生成的构建文件,我在-do-jar-with-libraries任务中找到了这个片段:

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

So in other words, it looks like you just need to add another attribute to the manifest task that you already have.

因此,换句话说,您似乎只需要向已有的清单任务添加另一个属性。

See also the Manifest Task documentation.

另请参阅清单任务文档

回答by Daniel Nesbitt

Assuming the libraries do not change location from compiling to executing the jar file, you could create a path element to your classpath outside of the compile target like so:

假设库从编译到执行 jar 文件的位置没有改变,您可以在编译目标之外创建一个指向类路径的路径元素,如下所示:

<path id="compile.classpath">
    <fileset dir="lib" includes="**/*.jar"/>
</path>

Then you can use the created path inside your javac task in place of your current classpath.

然后,您可以使用 javac 任务中创建的路径代替当前的类路径。

<classpath refid="compile.classpath"/>

You can then use the path to set a manifestclasspath.

然后,您可以使用该路径来设置清单类路径。

<target name="jar" depends="compile">
    <manifestclasspath property="jar.classpath" jarfile="build\jar\MyJar.jar">
      <classpath refid="compile.classpath"/>
    </manifestclasspath>    
    <jar destfile="build\jar\MyJar.jar" basedir="build\classes" >
        <manifest>
            <attribute name="Built-By" value="${user.name}" />
            <attribute name="Class-Path" value="${jar.classpath}"/>
        </manifest>
    </jar>
</target> 

The manifestclasspath generates a properly formatted classpath for use in manifest file which must be wrapped after 72 characters. Long classpaths that contain many jar files or long paths may not work correctly without using the manifestclasspath task.

manifestclasspath 生成一个格式正确的类路径,用于清单文件,必须在 72 个字符后包装。如果不使用 manifestclasspath 任务,包含许多 jar 文件或长路径的长类路径可能无法正常工作。