Apache Ant-Contrib Tutorial 1

Sometime we need some additional task capabilities which are not available with standard ant. Ant-Contrib is most commonly used task collection library. This post explains some of the tasks of ant.

You can download ant-contrib jar here. I have used ant-contrib-1.0b2 for this tutorial

Install/Include ant-contrib jar

Copy ant-contrib.jar in lib folder of your project and then include taskdef element in your build.xml as follow:

<!-- Include ant-contrib.jar dynamically -->
<taskdef resource="net/sf/antcontrib/antlib.xml">
	<classpath>
		<pathelement location="${basedir}/lib/ant-contrib.jar"/>
	</classpath>
</taskdef>

Sometime we want to perform some task based on conditions. In such situation we can use ant-contrib task to achieve this. Following examples explain you different types of usage of If task.

If-Else

<!-- Usage of if-else -->
<target name="test-ant-contrib">
	<if>
		<equals arg1="${testValue}" arg2="A" />
		<then>
			<echo message="Test value you have specified is A." />
		</then>
		<else>
			<echo message="Test value you have specified is other than A." />
		</else>
	</if> 
</target>

Run above target using ant test-ant-contrib -DtestValue=A and ant test-ant-contrib -DtestValue=B to test above example.

If-Else-If

<!-- Usage of if-else-if -->
<target name="test-ant-contrib">
	<if>
		<equals arg1="${testValue}" arg2="A" />
		<then>
			<echo message="Test value you have specified is A." />
		</then>
		<elseif>
			<equals arg1="${testValue}" arg2="B" />
			<then>
			<echo message="Test value you have specified is B." />
			</then>
		</elseif>
		<else>
			<echo message="Test value you have specified is other than A/B." />
		</else>
	</if>
</target>

Run above target using ant test-ant-contrib -DtestValue=A, ant test-ant-contrib -DtestValue=B and ant test-ant-contrib -DtestValue=C to test above example.

Not Equals

<!-- Usage of not-qauals -->
<target name="test-ant-contrib">
	<if>
		<not><equals arg1="${testValue}" arg2="A" /></not>
		<then>
			<echo message="Test value you have specified is other than A." />
		</then>
		<else>
			<echo message="Test value you have specified is A." />
		</else>
	</if>
</target>

Run above target using ant test-ant-contrib -DtestValue=A and ant test-ant-contrib -DtestValue=B to test above example.

OR Condition

<!-- Usage of or-condition -->
<target name="test-ant-contrib">
	<if>
		<or>
			<equals arg1="${testValue}" arg2="A" />
			<equals arg1="${testValue}" arg2="B" />
		</or>
		<then>
			<echo message="Test value you have specified is A/B." />    
		</then>
		<else>
			<echo message="Test value you have specified is other than A/B." />
		</else>
	</if>
</target>

Run above target using ant test-ant-contrib -DtestValue=A, ant test-ant-contrib -DtestValue=B and ant test-ant-contrib -DtestValue=C to test above example.

AND Condition

<!-- Usage of and-condition -->
<target name="test-ant-contrib">
	<if>
		<and>
			<equals arg1="${testValue1}" arg2="A" />
			<equals arg1="${testValue2}" arg2="B" />
		</and>
		<then>
			<echo message="You have specified A as testValue1 and B as testValue2." />    
		</then>
		<else>
			<echo message="Test values you have specified are not as expected." />
		</else>
	</if>
</target>

Run above target using ant test-ant-contrib -DtestValue1=A -DtestValue2=B and ant test-ant-contrib -DtestValue1=A -DtestValue2=C to test above example.

Loop through values using foreach

Sometime we want to iterate through the list of values separated by some delimiter. In below sample I am iterating through comma separated values.

<!-- Usage of for task --&gt;
<target name="test-ant-contrib">
	<for list="${testValues}" delimiter="," param = "val">
		<sequential>
			<echo message = "Value: @{val}" />
		</sequential>
	</for>
</target>

Run above target using ant test-ant-contrib -DtestValues=”value1,value2,value3,value4,value5″ to test above example.