0

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

为了启动 ant 构建文件,我使用以下命令。我这样做是因为我相信它允许我运行以前抱怨需要工作空间才能运行的 eclipse 任务。如果这是不正确的/如果有更好的方法,请通知我。

我的批处理脚本:

    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
4

1 回答 1

1

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

<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包含任务类的 .jar 清单添加一个属性。该属性的值是一个以空格分隔的相对 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>
于 2016-07-22T21:51:25.980 回答