Ant – the packaging system for Java – HowTo 2

Let’s be specific about java in this post. If you start reading this post afresh, please have a look at HowTo 1


Create a folder structure


public class HelloAnt{
public static void main(String [] args){
System.out.println("Hello Ant");


<?xml version="1.0" encoding="ISO-8859-1"?>
<project name="HelloAnt" basedir="." default="jar">
<target name="clean" description="delete all classes">
<delete dir="classes"/>
<delete file="HelloAnt.jar"/>

<target name="compile" description="compiles the task">
<mkdir dir="classes"/>
<javac srcdir="src" destdir="classes"/>

<target name="jar" description="jars the task">
<jar destfile="HelloAnt.jar" basedir="classes"/>

Build process

ant clean
ant compile
ant jar

Verify the build

pandian@Kannan:~/Downloads/ant/sample$ java -cp HelloAnt.jar HelloAnt
Hello Ant

Ant – the packaging system for Java – HowTo 1


Step -1: Download Ant

You can download the latest binary from

Step -2: Download JDK

JDK is mandatory.

Step -3: Installation

This tutorial examples are taken from Linux. But the commands should be common for all the platforms.

Extract the downloaded ant binary.
Goto ant/bin folder
Try to run ant

pandian@Kannan:~/Downloads/ant$ ant
Buildfile: build.xml does not exist!
Build failed

The Ant binary will contains an xml called fetch.xml. Execute the following command where the fetch.xml is available

pandian@Kannan:~/Downloads/ant/apache-ant-1.8.3$ ant -f fetch.xml -Ddest=system
Buildfile: /home/pandian/Downloads/ant/apache-ant-1.8.3/fetch.xml
Total time: 4 minutes 14 seconds

Step -4: Build XML file

The build instructions are given in XML format. By default ant looks for build.xml. That contains several tasks called targets. To explain the terminologies, A single project has multiple tasks – compile, build jar, upload etc.

A sample build.xml is given below.
The project name is “test project”. It contains a single target called ‘initialize’ (more will be added later)

pandian@Kannan:~/Downloads/ant$ cat build.xml
<?xml version="1.0"?>
<project name="test project" default="initialize">

<target name="initialize">
<echo>This is a test project</echo>

Execute ant

pandian@Kannan:~/Downloads/ant$ ant initialize
Buildfile: /home/pandian/Downloads/ant/build.xml

[echo] This is a test project

Total time: 0 seconds

Step -5: Read externalized parameters from property files

You can have the important parameters or fields in a property file and import it to build process to make it more flexible. Here is how you can do it.

I have a property file with 3 parameters

pandian@Kannan:~/Downloads/ant$ cat
country=United Kingdom

I am adding a new target to read and print one of the params.

<target name="get_properties">
<property file=""/>
<echo>Imported Properties for name ${name}</echo>

Now, lets execute the target

pandian@Kannan:~/Downloads/ant$ ant get_properties
Buildfile: /home/pandian/Downloads/ant/build.xml

[echo] Imported Properties for name grassfield

Total time: 0 seconds