2009-06-19 88 views

回答

14

在你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任務支持failonerrorerrorProperty屬性,可用於獲取相似的行爲。從啓提到

1

ant-contrib有一個trycatch任務。

+0

可悲的是,這將無法正常工作,因爲我仍然希望構建失敗。 – tomjen 2009-06-19 11:51:10

0

在要檢查其故障的任務中設置屬性,然後編寫第二個任務,以便在未設置屬性時執行該任務。我不記得build.xml的確切語法,或者我會舉例說明。

6

ant-contrib有trycatch任務。

但是您需要最近的版本1.0b3。然後使用

<trycatch> 
    <try> 
     ... <!-- your executions which may fail --> 
    </try> 
    <catch> 
     ... <!-- execute on failure --> 
     <throw message="xy failed" /> 
    </catch> 
</trycatch> 

訣竅是再次拋出一個錯誤,指示損壞的構建。

相關問題