gpt4 book ai didi

java - Apache ant list 类路径?

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:02:53 24 4
gpt4 key购买 nike

我有一个 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

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

以下来自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>

任何朝着正确方向的插入都是值得赞赏的。谢谢

最佳答案

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

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

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

<classpath refid="compile.classpath"/>

然后您可以使用该路径来设置 list 类路径。

<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>

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

关于java - Apache ant list 类路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/682852/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com