2009-06-19 89 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> 

诀窍是再次抛出一个错误,指示损坏的构建。

相关问题