Conditional task when exec fails in Ant

I have some unit tests running through Ant, and I would like to be able to run some cleanup code if unit tests fail. I was looking for some kind of "final" block, but I was not lucky to find it. I tried using the errorproperty and if property for tasks, but ant only accepts true values, "on" and "yes" as true properties. A successfully completed task (at least for unix) returns 0, so I had to build an absurdly complex device:

<project name="TestBuild" default="build" basedir=".">
<target name="build" depends="truth,checkresult,cleanup" />

<target name="truth">
    <echo message="Running Truth" />
    <exec executable="false" errorproperty="testfailure"/>
</target>
<target name="checkresult">
    <condition property="testfailed">
        <not>
            <equals arg1="${testfailure}" arg2="0" />
        </not>
    </condition>
</target>
<target name="cleanup" if="testfailed">
    <echo message="cleanup" />
    <fail />
</target>

? -, , . , , , , failonerror, . , , , , - .

+2
2

-1-
try/catch/finally script
Ant, , .. =

Flaka
Antcontrib/Antelope

    <trycatch>
     <try>
      <exec .../>
     </try>
     <catch>
      do your cleanup here
      and afterwards don't forget to fail
      </fail message="......."/>
     </catch>
      optionally you may use a finally section also
     <finally>
      ..
     </finally>
   </trycatch>

-2-
buildlistener script ( , )

exec-listener : = http://people.apache.org/~kevj/ossummit/extending-ant.html ( exec-listener )

.

<!-- taskcontainer -->    
<exec-listener onSuccess="true|false">
..

 your stuff goes here 
..
</exec-listener>
+3

Ant contrib try-catch-finally. , script.

0

All Articles