Home > Apache Ant, General, Technology > Ant if-then-else condition

Ant if-then-else condition

Unlike my previous post, this demo lists only the Directory Names and implicitly helps you in understanding how to change property values.

It is said that property value once set can’t be changed in the ant script.  But in this demo you’ll see how to re-set the property values.


  1. You should’ve downloaded ant-contrib jar from http://ant-contrib.sourceforge.net/#install
  2. Place ant-contrib.jar into <ANT_HOME>/lib directory


You first need to refer to your contrib jar in the <project> tag of build.xml.

Following sample code should help you understand if-else-condition using Ant-contrib.

Note: Code in the previous post been extended for this demo. Please note that else block never gets executed in this demo.

<?xml version="1.0" encoding="UTF-8"?>
<project name="ForTest" default="build" basedir="." xmlns:ac="antlib:net.sf.antcontrib">
 <taskdef uri="antlib:net.sf.antcontrib"

 <property name="directory" value=""/>

 <target name="test">
 <echo message="Folders in the directory are:"/>
 <ac:foreach target="list.dirs" param="dir.name">
 <dirset dir="${basedir}" includes="*"/>

 <target name="list.dirs">
 <ac:var name="directory" unset="true"/>
 <basename property="directory" file="${dir.name}"/>
 <echo message="Absolute Path : ${dir.name}"/>
 <echo message="Directory     : ${directory}"/>

 <!--Logic for if then else-->
 <equals arg1="${directory}" arg2="Test 1"/> <!-- if(directory=="Test 1") -->
 <then> <!--if directory name is Test 1-->
 <echo message="PING directory First directory"/>
 <equals arg1="${directory}" arg2="Test 2"/> <!-- if(directory=="Test 2") -->
 <echo message="PING directory Second directory"/>
 <echo message="PING No Directory. This never gets executed for this SCRIPT"/>
 <!--end if then else condition-->


 <target name="build" depends="test" description="Test For loop"/>


Following would be the output

Hope this post helps…

  1. July 20, 2014 at 8:26 AM

    Images can be uploaded onto the net and can be viewed from anywhere in the world
    with online access – How does this work. These automatic and wireless security systems
    come with additional features like informing to
    the nearby police station, in case an outsider is detected.
    Most organizations will keep the information in fireproof safes
    or storage rooms, while others will remove the tapes and store them
    in multiple locations.


  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: