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=”http://ant-contrib.sourceforge.net”&gt;
            <classpath>
                <pathelement location=”${ant-contib.jar.location}”/>
            </classpath>
        </taskdef>       

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

build.properties file for ant properties


#javac.debug=on
#author: Tuna Tore#this is a comment
failmessage=Please press key 1, 2 or 3...  Other key is pressed!!!
ant-contib.jar.location=YouShouldWriteHereANT_HOMELocation\\lib\\ant-contrib-1.0b3.jar
javac.deprecation=off
javac.nowarn=off

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="http://ant-contrib.sourceforge.net">
            <classpath>
                <pathelement location="${ant-contib.jar.location}"/>
            </classpath>
        </taskdef>
        <description>
         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.
        </description>
        <!-- getting build.properties file and set a prefix for it -->
        <property file="build.properties" prefix="projectproperties"/>
        <target name="decideProgramFlow" description="Deciding" >
        <input message="Please enter key 1,2 or 3..." addproperty="input"/>
        <ant:if>
            <equals arg1="${input}" arg2="1" trim="true"/>
                <then>
                    <antcall target="onePressed" />
                </then>
        <elseif>
            <equals arg1="${input}" arg2="2" trim="true"/>
                <then>
                    <antcall target="twoPressed" />
                </then>
        </elseif>
        <elseif>
            <equals arg1="${input}" arg2="3" trim="true"/>
                <then>
                    <antcall target="threePressed" />
                </then>
        </elseif>
        <else>
            <antcall target="fail" />
        </else>
        </ant:if>
        </target>
        <target name="onePressed">
            <echo message="ONE is pressed!..."/>
        </target>
        <target name="twoPressed">
            <echo message="TWO is pressed!..."/>
        </target>
        <target name="threePressed">
            <echo message="THREE is pressed!..."/>
        </target>
        <target name="fail">
            <fail message="${projectproperties.failmessage} pressed--> ${input}"/>
        </target>
        <target name="buildProject" depends="decideProgramFlow">
            <echo message="decideProgramFlow target is called by ANT..."/>
        </target>
</project>

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

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

onePressed:
     [echo] ONE is pressed!…

buildProject:
     [echo] decideProgramFlow target is called by ANT…

BUILD SUCCESSFUL
Total time: 9 seconds

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

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

fail:

BUILD FAILED
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 build.properties file

Advertisements

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 ${webapp.name}.war">
  <mkdir dir="${dist.dir}"/>
  <war destfile="${dist.dir}/${webapp.name}.war"
   webxml="WebContent/WEB-INF/web.xml"
   manifest="WebContent/META-INF/manifest.mf"
   compress="true">
   <classes dir="${build.dir}/classes"/>
   <zipfileset dir="${lib.dir}" prefix="WEB-INF/lib"/>
   <fileset dir="WebContent"  />
  </war>
  <copy todir="C:/appservers/apache-tomcat-5.5.27/webapps"
            file="${dist.dir}/${webapp.name}.war" />
  <delete dir="${build.dir}"/>
        <delete dir="${dist.dir}"/>
 </target>