ANT eclipse 无头构建 - java.lang.NoClassDefFoundError

ANT eclipse headless build - java.lang.NoClassDefFoundError

我正在尝试制作一个需要 eclipse 特定任务的无头构建。

为了启动 ant 构建文件,我使用了以下命令。我这样做是因为我相信它允许我 运行 eclipse 之前抱怨他们需要一个工作空间到 运行 的任务。如果这是 incorrect/if 有更好的方法,请告诉我。

我的批处理脚本:

    java -jar %EQUINOX_LAUNCHER_JAR% -application org.eclipse.ant.core.antRunner -buildfile %ANT_SCRIPT_JAR% -data %WORKSPACE_PATH%

在我的 ant 构建文件中,我需要定义一个任务:

<taskdef name="myTask" classname="path.to.class.with.execute"><classpath><pathelement location="path\to\dependency.jar"/></classpath></taskdef>

当运行宁

<myTask/>

我明白了

java.lang.NoClassDefFoundError: path/to/class/that/I/tried/to/import

Class您的任务代码使用的 es 必须在 class 路径中。一种选择是在定义任务时将它们显式添加到 class 路径:

<taskdef name="myTask" classname="path.to.class.with.execute">
    <classpath>
        <pathelement location="path/to/dependency.jar"/>
        <pathelement location="path/to/transitive-dependency.jar"/>
        <pathelement location="path/to/other-transitive-dependency.jar"/>
    </classpath>
</taskdef>

如果所有.jar文件都在同一个目录树中,可以缩短为:

<taskdef name="myTask" classname="path.to.class.with.execute">
    <classpath>
        <fileset dir="path/to/dir" includes="**/*.jar"/>
    </classpath>
</taskdef>

另一种可能性是将 Class-Path 属性添加到包含任务 class 的 .jar 的清单中。该属性的值是一个 space 分隔的相对 URL 列表,它们的隐含基础是清单所在的 .jar 文件。例如:

Class-Path: transitive-dependency.jar utils/other-transitive-dependency.jar

如果您在 Ant 中构建任务 .jar 本身,您可以在 Ant 的 jar 任务中指定 Class-Path 属性:

<jar destfile="task.jar">
    <fileset dir="classes"/>
    <manifest>
        <attribute name="Class-Path"
            value="transitive-dependency.jar utils/other-transitive-dependency.jar"/>
    </manifest>
</jar>