2

Is there a built-in way to get Ant to throw an error when a file included in the classpath task doesn't exist? My goal is for Ant to throw a build error when a compile target is called but the required libraries don't exist.

Here is an example from the build.xml file that includes the dependent libraries, however it doesn't throw an error when one of the libraries doesn't exist.

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

1 回答 1

2

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

<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>  
于 2013-02-06T00:06:39.303 回答