Simple Apache Ant Tutorial

[ This Blog Was Moved To : ]


Ant is free and open source build tool, written in Java, helps in automating the entire build process of a Java development project.

  • Ant uses XML build files.
  • By default, Ant looks for a build file named build.xml.
  • The build file contains information about how to build a particular project.
  • Each project contains multiple targets like creating directory, compiling source codes.
  • Target can depend on other targets.
  • Targets contain tasks.
  • Behind each task is a Java class that performs the described work.


To install Ant follow the steps given below.

  • Download the latest Ant package.
  • Extract it (my location is /home/dino/installations/apache-ant-1.7.1)
  • Set the following system variables
  • ANT_HOME =/home/dino/installations/apache-ant-1.7.1
  • PATH = %ANT_HOME%/bin


  • If you have root access, you can use your preferred Linux package manager (I use Yast) to install Ant

To make sure the installation is done correctly, open a console window and execute the command “ant – version”, you will see the installed ant version.

dino@linux-vxrd:~> ant -version
Apache Ant version 1.7.1 compiled on October 20 2009


In the next example you will see how to compile a java program and compress it into a .jar file using Ant build file. The following listing shows the build.xml file.

<?xml version="1.0" ?>
<project name="Hello World" default="compress">

	<target name="compile" depends="init">
		<echo>Starting the compilation process</echo>
		<javac srcdir="src" destdir="build/classes" source="1.5" target="1.5"/>
		<echo>Compilation Completed Successfully !</echo>

	<move todir="build/classes/">
		<fileset dir="src">
			<include name="**/*.class"/>
			<exclude name="**/*.java"/>

	<target name="init">
		<mkdir dir="build/classes" />
		<mkdir dir="dist" />

	<target name="compress"  depends="compile">
		<jar destfile="dist/HelloWorld.jar" basedir="build/classes" >
				<attribute name="Created-By" value="Diaa Kasem"/>
				<attribute name="Main-Class" value="HelloWorld"/>
		<echo>Building .jar file completed successfully!</echo>

	<target name="execute" depends="compile">
		<java classname="dino.HelloWorld" classpath="build/classes" />

	<target name="clean">
		<delete dir="build" />
		<delete dir="dist" />


  • The <project> element is the root element in Ant build files. The name attribute of the project element indicates the project name. Each project element can contain multiple <target> elements.
  • A target represents a single stage in the build process. A build process can have multiple targets. Here we have several targets compile, compress, init, execute and clean.
  • The default attribute of project element indicates the default target to be executed. Here the default target is compress.
  • When you see the compress target, it in turn depends on the compile target, that is indicated by the depends attribute. So the compile target will be executed first and because compile target depends on init target, init should be executed first.
  • The compile target has two task elements <javac> and <echo>. The javac task is used to compile the java files. The attribute srcdir=”.” indicates all the java files in the current directory. Echo task is used to display message on the console.
  • The compress target also performs two tasks, first the <jar> element as the name indicates, is used to build the jar file. The attributes destfile=”dist/HelloWorld.jar” , basedir=”build/classes” and includes=”**/*.class” indicates all the .class files in all the sub-directories should be compressed into HelloWorld.jar file. Later the echo task is used to display the success message on the console.

For a easier maintenanace all the source file should be kept in the src directory, the compressed jar file should be kept in the dist directory and all the intermediate class files should be kept in the build/classes directory.

By imposing structure cleaning the project becomes easy, we can just delete the directory and recreate it.

Using the <mkdir> task we create build/classes and dist directory.

<target name="init">
    <mkdir dir="build/classes" />
    <mkdir dir="dist" />

During the compilation process all the java files in the src directory will be compiled and the generated class files will be placed in the build/classes directory.

Since we placed all the class files under the build/classes directory, creating jar file becomes easier, you can simply specify the basedir attribute as “build/classes” instead of specifying basedir=”.” and includes=”*.class”. After creating the jar file we place it in the dist directory (destfile=”dist/HelloWorld.jar”).

<target name="compile" depends="init">
    <javac srcdir="src" destdir="build/classes" />
<target name="compress" depends="compile">
        <jar destfile="dist/HelloWorld.jar" basedir="build/classes" />

You can use the java task to execute a class file as shown below. The classname attribute refers to the java class to be executed and the classpath attribute refers to the directory in which the class is located.

<target name="execute" depends="compile">
    <java classname="dino.HelloWorld" classpath="build/classes" />

Since all the class files and jar files are isolated, we can easily clean the project by deleting the respective directories.

<target name="clean">
    <delete dir="build" />
    <delete dir="dist" />

When you run the “ant execute” command the execute target will be invoked. Execute target also depends on compile target which in turn depends on init target, but now the directories won’t be created again because it already exist. Since the java file is already compiled it won’t be compiled again. Ant checks the timestamp of the file and compiles only the updated files. The HelloWorld.class file will be execute and the “HelloWorld!” message will be displayed on the console.

dino@linux-vxrd:~/Documents/anttest/helloworld> ant execute
Buildfile: build.xml


[echo] Starting the compilation process
[echo] Compilation Completed Successfully !

[java] Hello ANT Wolrd !

Total time: 1 second

The java source file I used is :

package dino;

public class HelloWorld{
public static void main(String [] args){
System.out.println("Hello ANT Wolrd !");

And this is the directory structure:

Ant Project Directory Structure

Ant Project Directory Structure

I guess this is it for now.

Hope you like it and share your opinion below.


Posted in JAVA. Tags: , , , , . 1 Comment »

One Response to “Simple Apache Ant Tutorial”

  1. Niranjan Bansal Says:

    Thanks for such a lovely tutorial. May god bless you

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: