Ant is free
and open source build tool, written in Java, helps in automating
the entire build process of a Java development project.
uses XML build files
default, Ant looks for a build file named build.xml
build file contains information about how to build a particular
project contains multiple targets like creating directory, compiling
can depend on other targets
each task is a Java class that performs the described work.
install Ant follow the steps given below.
the latest Ant distribution.
it (my location is E:\apache-ant-1.7.1)
the following system variables
make sure the installation is proper, go to command prompt and
execute the command "ant - version", you will see the
installed ant version.
this 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.
name="Hello World" default="compress">
Compilation Complete! </echo>
destfile="HelloWorld.jar" basedir="." includes="*.class"
Building .jar file Complete! </echo>
<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
target represents a single stage in the build process. A build
process can have multiple targets.
Here we have two targets compile and compress.
default attribute of project element indicates the default target
to be executed. Here the
default target is compress.
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
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.
compress target also performs two tasks, first the <jar>
element as the name indicates, is
used to build the jar file. The attributes destfile="HelloWorld.jar"
, basedir="." and includes="*.class"
indicates all the .class files in the current directory should
into HelloWorld.jar file. Later the echo task is used to display
the success message
on the console.
To run the build.xml file, open the
command prompt, go to the example directory, type the command "ant".
You will see the following information.