Ant Installation Tutorial
Join the DZone community and get the full member experience.Join For Free
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 distribution.
- extract it (my location is e:\apache-ant-1.7.1 )
- set the following system variables
- ant_home =e:\apache-ant-1.7.1
- path = %ant_home%\bin
to make sure the installation is proper, go to command prompt and execute the command "ant - version", you will see the installed ant version.
in 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.
<?xml version="1.0" ?> <project name="hello world" default="compress"> <target name="compile"> <javac srcdir="."/> <echo> compilation complete! </echo> </target> <target name="compress" depends="compile"> <jar destfile="helloworld.jar" basedir="." includes="*.class" /> <echo> building .jar file complete! </echo> </target> </project>
- 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 two targets compile and compress .
- 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.
- 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="helloworld.jar" , basedir="." and includes="*.class" indicates all the .class files in the current directory should be compressed 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.
you can download the build file here.
build file : download
project : download
Opinions expressed by DZone contributors are their own.