tags:

views:

245

answers:

2

I have ant code that kicks off a release build in all subdirectories:

   <target name="all-release" >
    <subant target="sub-release" failonerror="true">
        <fileset dir="." includes="*/build.xml" />
    </subant>
   </target>

As written, if any individual build fails, all-release will fail fast (none of the later builds will succeed. If I switch failonerror="false", the all-release will succeed all the time. It turns out that all the sub-builds are independent, so what I really want is:

run all sub-release builds, and then have all-release fail afterwards if one or more sub-releases failed (ideally with a nice error message about which builds failed).

Any ideas?

+1  A: 

Suggest you look at the extensions available in the ant-contrib tasks.

The 'for' task can probably be adapted to meet your requirements.

Your 'all-release' target, with the ant-contrib taskdef might look like this:

<taskdef resource="net/sf/antcontrib/antlib.xml">
    <classpath>
        <pathelement location="lib/ant-contrib-1.0b3.jar"/>
    </classpath>
</taskdef>

<target name="all-release">
    <for keepgoing="true" param="file">
        <path>
            <fileset dir="." includes="*/build.xml" />
        </path>
        <sequential>
            <ant antfile="@{file}" target="sub-release" />
        </sequential>
    </for>
</target>

Using some other ant-contrib features it may be possible to get the list of fails.

Sample log from above build.xml:

$ ant all-release
Buildfile: build.xml

all-release:
     [echo] /work/Scratch/dir1/build.xml

sub-release:
     [echo] dir1
     [echo] /work/Scratch/dir2/build.xml

sub-release:
     [echo] dir2
      [for] /work/Scratch/dir2/build.xml: The following error occurred while executing this line:
      [for] /work/Scratch/build.xml:17: The following error occurred while executing this line:
      [for] /work/Scratch/dir2/build.xml:6: dir2 failed
     [echo] /work/Scratch/dir3/build.xml

sub-release:
     [echo] dir3

BUILD FAILED
/work/Scratch/build.xml:11: Keepgoing execution: 1 of 3 iterations failed.

Total time: 0 seconds
martin clayton
A: 

Antelope Ant extensions have a try-catch command which can be used to to what you need:

<taskdef name="try" classname="ise.antelope.tasks.TryTask"/>
....
<try break="false" printmessage="true" >
   <antcall target="xmlValidate" />
   <antcall target="runJunit" />
   <antcall target="..." />
   <catch>
      <property name="haderrors" value="true"/>
   </catch>
</try>
<fail message="FAILED" if="haderrors" />

break=false let's continue the next command after failing. But failed targets set the haderrors property which is checked in the end. I used it a lot for build jobs (and it works fine) but I am not sure it works for <fileset> inside <subant>. Maybe you have to list all <subant> calls explicitly.

Peter Kofler