ant - 如果类路径任务中包含的文件不存在,如何让 Ant 抛出错误?

标签 ant

当类路径任务中包含的文件不存在时,是否有内置方法让 Ant 抛出错误?我的目标是当调用编译目标但所需的库不存在时,Ant 会抛出构建错误。

下面是 build.xml 文件中的一个示例,其中包含依赖库,但是当其中一个库不存在时,它不会引发错误。

<target name="compile" description="Compiles the Java code" depends="init">
    <mkdir dir ="${build}/${module-package}" />
    <javac srcdir="${src}/main/${module-package}" 
           destdir="${build}" 
           includeantruntime="off"
           debug="true" 
           fork="true">

        <classpath>
            <fileset dir="${lib}" >
                <include name="joda-time/joda-time-2.1.jar" />
                <include name="jackson/jackson-core-lgpl-1.9.7.jar"/>
                <include name="jackson/jackson-mapper-lgpl-1.9.7.jar"/>
            </fileset>
        </classpath>
    </javac>
</target>  

最佳答案

您可以使用available ANT 中的任务来检查您知道应该存在的类是否存在(由 jar 提供)。

<path id="compile.path">
    <fileset dir="${lib}" >
        <include name="joda-time/joda-time-2.1.jar" />
        <include name="jackson/jackson-core-lgpl-1.9.7.jar"/>
        <include name="jackson/jackson-mapper-lgpl-1.9.7.jar"/>
    </fileset>
</path>

<available classname="org.joda.time.DateTime" property="joda.present" classpathref="compile.path"/>
<available classname="org.codehaus.jackson.JsonFactory" property="jackson.present" classpathref="compile.path"/>

<target name="compile" description="Compiles the Java code" depends="init">
    <mkdir dir ="${build}/${module-package}" />

    <fail message="Joda time missing" unless="joda.present"/>
    <fail message="Jackson missing"   unless="jackson.present"/>

    <javac srcdir="${src}/main/${module-package}" 
           destdir="${build}" 
           includeantruntime="off"
           debug="true" 
           fork="true"
           classpathref="compile.path"
           />
</target>  

关于ant - 如果类路径任务中包含的文件不存在,如何让 Ant 抛出错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14695862/

相关文章:

java.lang.AbstractMethodError - 使用 ANT 内置 jar 时

android - Phonegap 运行 android 不工作

java - 在 ant build 中加速 junit 测试

java - 在 Ant exec 任务中检测超时

java - Ant javac 任务运行可执行文件而不是 java 类

java - 如何编写可移植的build.xml?

java - 存储 Ant SCP 任务的用户名/密码的最佳方式是什么

机器人/位图.h : No such file or directory (after updating Android SDK tools to revision 14)

ant - 使用 Ant 解压 tgz 文件

deployment - 混淆的 JavaFX JAR 中的 FXML 找不到 Controller 类