In the previous post, we have learned what is Apache Ant, its features and an example to create simple build file. This post will guide you other basic ant tasks that are frequently used to create and deploy packages.


 Ant allows us to declare properties and assign values that can be used as variables

<property name="property_name" value="property_value"/>

And that can be used using


Ant predefined properties




Version of apache ant

Java version that apache ant is using


Home directory where apache is installed


Path of ant library files


Location of build file


Base directory of build

Echoing message on screen

<echo message="Version of Apache Ant ${}: ${ant.file}"/>

Define classpath

<path id="classpath">
    <fileset dir="directory_path" includes="*.jar" />
    <fileset dir="directory_path" includes="*.jar" />

Compiling Java code

<javac srcdir="source_path_of_java_files" classpathref="classpath" destdir="destination_of_compiled_files" />

Creating Jar 

<jar jarfile="jar_name.jar" basedir="compiled_files_path" includes="**/*.class" update="true">

Create directory

<mkdir dir="directory_name" />

Delete directory

<delete dir="directory_name" />

Copy directory

<copy todir="directory_path_where_to_copy" overwrite="true">
    <fileset dir="directory_path_to_copy_files">
         <include name="**/*.*" />

Copy files

<copy file="file_to_copy" todir="directory_path_where_to_copy" overwrite="true" />

Delete files

     <fileset dir="directory_path_to_copy_files">
         <include name="file_name_or_wild_card"/>
         <include name="file_name_or_wild_card"/>

File loading 

<loadfile property="defined_property_name" srcFile="file_to_load_as_string" />

Replace string in file

<replace file="file_path" token="string_to_replace" value="string_value_to_replace_with"></replace>

Set property based on condition

Based on condition property value is set to true or false.

<condition property="property_name_to_set">
     <matches string="string_data" pattern="pattern_to_match_in_string" />

Above code will set property if particular pattern is matched in specified string. Pattern can hold regex expression. You can specified string inline or property that holds the string file data. Detailed explanation is available on official site.


   <equals arg1="value" arg2="value"/>
   <then><echo message="if block executed"/></then>
         <echo message="else block executed"/>

Like if-else in any other language, based on condition task is performed. Detailed explanation is available on official site.

Call target inside a target

<antcall target="valid_target_name"/>

Add external tasks

<taskdef name="xmltask" classname="com.oopsconsultancy.xmltask.ant.XmlTask" classpath="./lib/xmltask.jar" />

Xmltask : Insert data using XPath

<xmltask source="source_file_to_edit" dest="destination_to_save_file">
     <insert path="//*[local-name()='beans'][1]/*[local-name()='bean'][last()]" position="after">
              <import xmlns="" resource="MyData.xml"/>
 Above command will insert xml-import after the path defined using XPath.

Xmltask : Insert data based on condition using XPath

<xmltask source="source_file_to_edit" dest="destination_to_save_file">
     <copy path="//*[local-name()='beans'][1]/*[local-name()='bean'][@resource='MyData.xml']/@resource" property="ITEM_EXISTS_ALREADY" />
     <insert path="//*[local-name()='beans'][1]/*[local-name()='bean'][last()]" position="after"> position="after" unless="ITEM_EXISTS_ALREADY">
            <import xmlns="" resource="MyData.xml"/>

The above code will set property ITEM_EXISTS_ALREADY if the attribute resource with value MyData.xml  exists under given XPath. Then insertion will be performed based on the ITEM_EXISTS_PROPERTY.

Xmltask : Remove xml content

<xmltask source="source_file_to_edit" dest="destination_to_save_file">
    <remove path="/beans/bean/import[@resource='MyData.xml']"/>

The above code will remove the import with resource value MyData.xml.


Categories: Apache AntJava

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts

Apache Ant

Difference between Maven and Apache Ant

Build tools are programs to manage and automate build process of your application. By build process means compiling source code, linking the packages, bundle them into an executable binary. Maven and Apache Ant are popular build Read more…

Apache Ant

Apache Ant : Java Build Tool

Apache Ant is a Java library and command-line tool to automate the repetitive tasks and helps to build software. Ant is flexible and does not impose coding conventions. It supplies a number of built-in tasks allowing Read more…


Advantages of spring interceptor over servlet filter

Even filters can do the same things that interceptors can but interceptors can do more easily and more elegantly. Interceptors are more coupled to the request/response cycle than filters. Can inject other beans in the Read more…