tags:

views:

401

answers:

3

Suppose I have some Ant task, say javac or junit. If either task fails, I want to execute a command, but if they succeeds I don't.

Any idea how to do this?

+1  A: 

ant-contrib has a trycatch task.

Kai
Sadly that won't work, as I still want the build to fail.
tomjen
A: 

Set a property in the task you want to check for failure, and write the second task so that it executes if the property is not set. I don't remember the exact syntaxes for build.xml, or I'd give examples.

jrh.

Here Be Wolves
+2  A: 

In your junit target, for example, you can set the 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>

Then, create a target that only runs if the test.failed property is set, but fails at the end:

<target name="otherStuff" if="test.failed">
    <echo message="I'm here. Now what?"/>
    <fail message="JUnit test or tests failed."/>
</target>

Finally, tie them together:

<target name="test" depends="junit,otherStuff"/>

Then just call the test target to run your JUnit tests. The junit target will run. If it fails (failure or error) the test.failed property will be set, and the body of the otherStuff target will execute.

The javac task supports failonerror and errorProperty attributes, which can be used to get similar behavior.

CoverosGene