ANT (Another Need Tool) - build and deploy tool

The above diagram shows the tasks that have to be carried out to built a standalone java application. The process of building a project has to be done repeatedly during the development and the maintainance of a project. It is not advisable to built a project manually. To automate the build process we can use the tools like make, ant etc. ANT (Another Need Tool) tool uses a build file to decide about what task has to be done. The default name of build file is build.xml. "build.xml" file has the tags like project, target, etc.

<project name="demoproj" default="target">
<target name="targetx">
<echo name="Step one of targets">
<echo name="Step two of targetx">
<target name="targetz">
<delete dir="done" />
<delete dir="dtwo" />

mkdir, delete, echo are called as ant tasks. Ant is provided as part of various IDEs like Netbeans, Eclipse, IntelliJ. When we issue the command ant without any parameters, it will read the file build.xml and perform the tasks to reach the targets specified in the default attribute of project tag. To perform the tasks under a perticular target, we should specify the target name as the first parameter when executing the ant command. If the build file name is other than build.xml, we need to use "-f" options to specify the file name.
Example ant command issue statements

cwd> ant

cwd> ant targetx

cwd> ant -f ourbuild.xml

cwd> ant -f ourbuild.xml targetz

A target can depend upon multiple targets. To specify the dependencies, we should use depends attribute for target tag.

<target name="targetx" depends="targety, targetz">
<echo name="Step one of targety">
<echo name="Step two of targetx">

When ant is asked to reach targets, ant performs the task under targety then the task under taskz will be carried out and finally task under targetx will be carried out.

An example ant build script

<project name="ourproj" default="build">
<target name="build" depends="credirs, compile, pack, run">
<target name="cleanbuild" depends="clean, build">
<target name="clean">
<delete dir="build" />
<target name="credirs">
<mkdir dir="build" />
<mkdir dir="build/classes" />
<mkdir dir="build/lib" />
<target name="compile">
<javac srcdir="src" destdir="build/classes" includes="*.java" />
<target name="pack">
<jar jarfile="build/lib/ourproj.jar" basedir="build/classes" />
<target name="run">
<java classname="app" classpath="build/lib/ourproj.jar" />

To improve the flexibility of build file, we can use the ant properties. To do this create a properties file with the build file name and properties as the extension.


To use the value of the properties can be used in the build file with the below syntax.
Syntax: ${property}