How To Use Ant

Ant is a build tool written in Java, intended to be used for Java build processes. Ant is used for building small and large projects as well. You can use ant not only for automation of building Java projects, but for automating any complicated and repetitive tasks. Ant is using xml files to define tasks that should be run during a process.

How To Install Ant

Ant can be installed on almost any operating system in a few easy steps:

  1. Install Java Runtime Environment(JRE) or Java Development Kit(JDK). Ant need a JRE or JDK in order to run.
  2. 2. Download the ant binary distribution from this link and unzip it to a directory of your choice.
  3. 3. Add the location of the bin folder to the path environment variable. For example in windows: PATH=%PATH%;c:\ant\bin
  4. 4. Set ANT_HOME environment variable to the location where ant is installed. For example: ANT_HOME=c:\ant

How to create your first ANT project

Any ant project consist of one or more xml files containing the ant tasks. Lets create a small file named “build.xml”, containing only one task to print a small “Hello World!!!”. In order to run you have to go to the directory containing the build.xml file and type and run “ant”.

<project name="how-to-use-ant" basedir="." default="hello-world">

    <target name="hello-world">
        <echo message="Hello World!!!"/>

As you noticed we defined in this and project one task named hello-world and this is the default task. If everything goes well, the ant will run (we added in the path the ant bin directory when we installed it) and will print the following text:

Buildfile: build.xml

     [echo] Hello World!!!

Total time: 0 seconds

How To Compile and Build Using Ant

In real life we use Ant as the build tool for our applications and Ant is used in 90 percent of the cases for that. I’ve created a separate post to serve as a tutorial. You can check How to Create Jar Files from Ant.

Lets take this time in consideration a real life project. Most of the time Ant is used to compile and build java projects. This means generating, compiling and building jar files. For this we are going to define severals tasks. Lets consider the we create a buid.xml file in the directory where sources are located. The sources are put in the subdirectory called src. We also need to include a jar in the classpath while compiling. We are going to create a separate property file to define the location of this file:

#	File Name:


The ant task contains a few tasks for doing the following operations:
1. Clean the build directory if exists.
2. Create the build directory where the new classes will be built.
3. Compile the java classes.
4. Build a jar file from the java classes.

Here is the build.xml file. Note the dependencies between the tasks and how the properties are defined in the ant xml file or loaded from the properties file:

<project name="how-to-use-ant" basedir="." default="hello-world">

	<property name="build" value="build"/>
	<property file=""/>
	<target name="clean">
		<delete dir="${build}"/>

	<target name="init" depends="clean">
		<mkdir dir="${build}"/>
	<target name="compile" depends="init">
		<!-- Compile the java code -->
		<javac srcdir="src" destdir="${build}">
				<pathelement location="${env.LIB}/required-jar1.jar"/>
				<pathelement location="${env.LIB}/required-jar2.jar"/>
	<target name="jar" depends="compile">
		<!-- Build the jar file -->
		<jar basedir="${build}" destfile="${build}/new-jar.jar"/>

Now that you’ve seen how to create an ant task you might ask why you need an ant build file for building a jar file. This can be done as well with a few batch commands which will take only a 4 lines bat file. There are certain advantages. The ant files can be easily parametrized and reused, sub projects cand be created, lots of tasks are implemented, covering most of the operations you want to do (junits, sql operations, zip file tasks, tasks for replacing and editing text and xml files, …) and new tasks can be implemented in java and used in any ant file.

Leave a Comment.