If you are using Ant, you owe it to yourself to check out Ant-contrib, a great collection of really useful tasks that make writing highly functional Ant scripts a snap.

For example, there’s a <if> task that lets you write in conditional logic, like this:

<if>
  <equals arg1="${target}" arg2="windows" />
  <then>
    <antcall target="build-windows"/>
  </then>
  <else>
    <antcall target="build-solaris"/>
  </else>
</if>

As you can see, the <if> task supports <else> conditions, and also supports <elseif>. It can test on any condition that Ant’s built-in <condition> task can, such as equality, the availability of a property, whether file(s) are up to date, even if a document is available via an http request.


For very complicated branching logic, there’s a <switch> task which works like a java <switch> statement.

There’s a <foreach> task that executes (antcall’s) a target for each file in a fileset. This is similar to Ant’s built-in <exec> task, except instead of executing a native executable, it calls an Ant task on each file found.

There’s a <trycatch> and <throw> set of tasks that let you continue processing if an enclosed task fails.

One task that I’ve found very useful is <propertycopy>. This task allows you to get the value of another property whose name is found inside the value of another property or expression. For example, suppose you have a set of properties as follows:

user.john.department=40
user.mike.department=45
user.adam.department=10

If you have another property named ${myname} with the value ‘john’, ‘mike’, or ‘adam’ in it, you can do this:

<propertycopy name="mydept" from="user.${myname}.department"/>

and the appropriate value will be put into the new property ${mydept}. This is something you cannot do with Ant’s <property> task.

For those of you running a suite of JUnit tests nightly (you are running a suite of JUnit tests, aren’t you?) you may have certain tests that spawn servers that do not terminate if the tests error or fail. If you have test suites that accidentally get stuck because of a failure and are still running when you get in the next morning, Ant-contrib’s <limit> task is just what you need. It allows you to surround tasks with a time-limited container, so that if the contents take longer than the limit you specify, the contents stop and can optionally fail the build or continue.

These are the tasks that Ant-contrib offers that I have used extensively myself, and represent only about a third of the tasks available. If you use Ant, check the product out at http://ant-contrib.sourceforge.net and see if it can’t boost your productivity and power in your Ant scripts.

Leave a Reply