How to use Ant-Contrib Tasks ant:if example

If you want to use ANT-contrib tasks inside your ANT project. First you must install ANT itself on your machine.

Second you should download the required ant-contrib (ant-contrib-1.0b3.jar) library from here and put it inside ANT_HOME\lib folder

You should also add the following lines(green lines) in your build.xml file

<project name=”projectname” default=”buildProject” basedir=”.” xmlns:ant=”antlib:net.sf.antcontrib>

        <taskdef resource=”net/sf/antcontrib/antlib.xml” uri=””&gt;
                <pathelement location=”${ant-contib.jar.location}”/>

Here is a complete project for testing a ant-if task; file for ant properties

#author: Tuna Tore#this is a comment
failmessage=Please press key 1, 2 or 3...  Other key is pressed!!!

Here is build.xml file

<?xml version="1.0" encoding="UTF-8"?>
<!-- Author: Tuna Tore -->
    <project name="testingantcontrib" default="buildProject" basedir="." xmlns:ant="antlib:net.sf.antcontrib">
        <taskdef resource="net/sf/antcontrib/antlib.xml" uri="">
                <pathelement location="${ant-contib.jar.location}"/>
         This script will use if conditions inside ANT tasks,
         Console input will take some inputs from users and the program's flow
         will be changed according to these inputs.
        <!-- getting file and set a prefix for it -->
        <property file="" prefix="projectproperties"/>
        <target name="decideProgramFlow" description="Deciding" >
        <input message="Please enter key 1,2 or 3..." addproperty="input"/>
            <equals arg1="${input}" arg2="1" trim="true"/>
                    <antcall target="onePressed" />
            <equals arg1="${input}" arg2="2" trim="true"/>
                    <antcall target="twoPressed" />
            <equals arg1="${input}" arg2="3" trim="true"/>
                    <antcall target="threePressed" />
            <antcall target="fail" />
        <target name="onePressed">
            <echo message="ONE is pressed!..."/>
        <target name="twoPressed">
            <echo message="TWO is pressed!..."/>
        <target name="threePressed">
            <echo message="THREE is pressed!..."/>
        <target name="fail">
            <fail message="${projectproperties.failmessage} pressed--> ${input}"/>
        <target name="buildProject" depends="decideProgramFlow">
            <echo message="decideProgramFlow target is called by ANT..."/>

If you call this project using ant command(from terminal or console), theoutput will be such as the following one (will be changed based on input);

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\tunat>cd Desktop

C:\Documents and Settings\tunat\Desktop>cd antDevelopment

C:\Documents and Settings\tunat\Desktop\antDevelopment>ant
Buildfile: C:\Documents and Settings\tunat\Desktop\antDevelopment\build.xml

    [input] Please enter key 1,2 or 3…

     [echo] ONE is pressed!…

     [echo] decideProgramFlow target is called by ANT…

Total time: 9 seconds

C:\Documents and Settings\tunat\Desktop\antDevelopment>ant
Buildfile: C:\Documents and Settings\tunat\Desktop\antDevelopment\build.xml

    [input] Please enter key 1,2 or 3…


C:\Documents and Settings\tunat\Desktop\antDevelopment\build.xml:41: The following error occurred while executing this line:
C:\Documents and Settings\tunat\Desktop\antDevelopment\build.xml:59: Please press key 1, 2 or 3…  Other key is pressed!!! pressed–> 4

Total time: 4 seconds

C:\Documents and Settings\tunat\Desktop\antDevelopment>
Download source –> build.xml and file


ANT SSH task for copying file on Linux Server

 <!-- Copy files to or from a remote server using SSH.-->
  <target name="SSH" description="used for copying bla.ear file on remote LINUX server using SSH">
    <echo message="deploying bla.ear file on server IP: ${serverIP}"/>
   <scp file="${fileLocationWillBeHere}" todir="root@${serverIPwillBeHere}:/tmp" password="passwordWillBeHere" trust="true"/>

ANT war task

<!--  B U I L D    T H E    W A R    F I L E  -->
 <target name="war" depends="cleancache,compile" description="Build ${}.war">
  <mkdir dir="${dist.dir}"/>
  <war destfile="${dist.dir}/${}.war"
   <classes dir="${build.dir}/classes"/>
   <zipfileset dir="${lib.dir}" prefix="WEB-INF/lib"/>
   <fileset dir="WebContent"  />
  <copy todir="C:/appservers/apache-tomcat-5.5.27/webapps"
            file="${dist.dir}/${}.war" />
  <delete dir="${build.dir}"/>
        <delete dir="${dist.dir}"/>