[英]How do I execute an Ant command if a task fails?
假設我有一些Ant任務 - 比如javac或junit - 如果任一任務失敗,我想執行一個任務,但如果他們成功,我就不會。
知道怎么做嗎?
例如,在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
屬性,可用於獲取類似行為。
正如凱提到的那樣:
ant-contrib有一個trycatch任務。
但是你需要最新的版本1.0b3。 然后使用
<trycatch>
<try>
... <!-- your executions which may fail -->
</try>
<catch>
... <!-- execute on failure -->
<throw message="xy failed" />
</catch>
</trycatch>
訣竅是再次拋出錯誤以指示構建中斷。
ant-contrib有一個trycatch任務。
在要檢查其失敗的任務中設置屬性,然后編寫第二個任務,以便在未設置屬性時執行該任務。 我不記得build.xml的確切語法,或者我舉例說明。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.