如果任务失败,我该如何执行Ant命令?
问题描述:
假设我有一些Ant任务 - 比如说javac或者junit - 如果任一任务失败了,我想执行一个任务,但是如果他们成功了,我不会。如果任务失败,我该如何执行Ant命令?
任何想法如何做到这一点?
答
在你junit目标,例如,你可以设置failureProperty
:
<target name="junit" depends="compile-tests" description="Runs JUnit tests">
<mkdir dir="${junit.report}"/>
<junit printsummary="true" failureProperty="test.failed">
<classpath refid="test.classpath"/>
<formatter type="xml"/>
<test name="${test.class}" todir="${junit.report}" if="test.class"/>
<batchtest fork="true" todir="${junit.report}" unless="test.class">
<fileset dir="${test.src.dir}">
<include name="**/*Test.java"/>
<exclude name="**/AllTests.java"/>
</fileset>
</batchtest>
</junit>
</target>
然后,创建仅在test.failed
属性设置运行的目标,但最终失败:
<target name="otherStuff" if="test.failed">
<echo message="I'm here. Now what?"/>
<fail message="JUnit test or tests failed."/>
</target>
最后,绑在一起:
<target name="test" depends="junit,otherStuff"/>
然后,只需调用test
目标即可运行JUnit测试。 junit
目标将运行。如果失败(失败或错误),test.failed
属性将被设置,并且otherStuff
目标的主体将执行。
javac任务支持failonerror
和errorProperty
属性,可用于获取相似的行为。从启提到
答
在要检查其故障的任务中设置属性,然后编写第二个任务,以便在未设置属性时执行该任务。我不记得build.xml的确切语法,或者我会举例说明。
答
:
ant-contrib有trycatch任务。
但是您需要最近的版本1.0b3。然后使用
<trycatch>
<try>
... <!-- your executions which may fail -->
</try>
<catch>
... <!-- execute on failure -->
<throw message="xy failed" />
</catch>
</trycatch>
诀窍是再次抛出一个错误,指示损坏的构建。
可悲的是,这将无法正常工作,因为我仍然希望构建失败。 – tomjen 2009-06-19 11:51:10