当类路径任务中包含的文件不存在时,是否有内置方法让 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/