If-Else in Ant

When I need to write quick script to perform a simple task, I opt to use Ant instead of a shell script. Unfortunately, the Ant core tasks doesn’t provide any flow logic like the everyday if-else statements.

Ant-Contrib provides a collection of useful tasks missing in the Ant core.

Installing Ant-Contrib

There are 2 methods to install ant-contrib:

Copy ant-contrib-1.0b3.jar to the lib directory of your Ant installation. If you want to use one of the tasks in your own project, add the lines

<taskdef resource="net/sf/antcontrib/antcontrib.properties"/>

Keep ant-contrib-1.0b3.jar in a separate location. You now have to tell Ant explicitly where to find it e.g. a lib directory relative to the build.xml

<taskdef resource="net/sf/antcontrib/antcontrib.properties" classpath="lib/ant-contrib-1.0b3.jar"/>

Example Using If-Else Task

<if>
  <equals arg1="${status}" arg2="OFFLINE"/>
  <then>
    [...]
  </then>
  <else>
    [...]
  </else>
  </if>

Related posts:

  1. Sending Email with Ant
  2. Some Useful Ant Tasks
  3. Java Code Snippet for Sending Email using Gmail SMTP Server
This entry was posted in Ant, Java and tagged , . Bookmark the permalink.

Comments are closed.