Building with Apache ANT part 1

Supun Dharmarathne
technodyne
Published in
3 min readJul 9, 2012

ANT is a java library which can be used to automate the building process of java application. It is a command-line tool. Lot of tasks can be done using ant. Compiling, Testing,Assembling and as well as running the java applications.

Why we need a build tool

In the process of building applications, there are several things should be done. Rather than doing them manually, a build tool can be used to make this automated. this will really save the developers time and other resources.

Ant is the most complete Java build and deployment tool which is currently available. Ant can be easily invoked from the command line and it can integrate with free and commercial IDEs.

So, lets move on how to build with ANTs.

Installing ANT

Download the latest distribution from here. Then extract it to your own folder (Ex: C:). Make sure that the JAVA_HOME environmental variable value has been set to the place where your JDK is installed.Then create an another environmental variable called ANT_HOME which is pointed to the Ant installation folder(Ex : C:Apache_ant). Then append the path variable to the location where the bin folder is located. (Ex:C:Apache_antbin). Now go to command prompt and type ant -version. Then you get a message which mentioning about the Ant version
C:Apache_ant ant -version
Apache Ant(TM) version 1.8.2 compiled on December 20 2010

Starting Building

Ant scripting is basically build on XML. A xml file, build.xml contains all the commands and details regarding to the build process. So by modifying this xml file we can control whatever the process we need ex: compiling, testing or archiving.

Suppose the project name is myproject. And we need to display a message.First create the project called myproject. Inside the root directory, create build.xml.

<?xml version="1.0"?>
<project name="myproject" default="info">
<target name="info">
<echo>Welcome to Apache Ant</echo>
</target>
</project>

Now go to myproject folder in commant prompt and type ant . This will give the following output.

C:myproject>ant
Buildfile: C:myprojectbuild.xml

info:
[echo] Welcome to Apache Ant

BUILD SUCCESSFUL
Total time: 0 seconds

C:myproject>

The following properties can be used in the build.xml file.



PropertiesDescriptionant.fileThe full location of the build file.ant.versionThe version of the Apache Ant installation.basedirThe basedir of the build, as specified in the basedir attribute of the project element.ant.java.versionThe version of the JDK that is used by Ant.ant.project.nameThe name of the project, as specified in the name atrribute of the project elementant.project.default-targetThe default target of the current projectant.project.invoked-targetsComma separated list of the targets that were invoked in the current projectant.core.libThe full location of the ant jar fileant.homeThe home directory of Ant installationant.library.dirThe home directory for Ant library files — typically ANT_HOME/lib folder.In addition to this the user can define his own properties for the build.xml .

<?xml version="1.0"?>
<project name="myproject" default="info">
<property name="logingname" value="technodyne"/>
<target name="info">
<echo>Apache Ant version is ${ant.version} - You have logged as
${logingname} </echo>
</target>
</project>
C:myproject>antBuildfile: C:myprojectbuild.xml

info:
[echo] Apache Ant version is Apache Ant(TM) version 1.8.2
compiled on December 20 2010 - You have logged as technodyne

BUILD SUCCESSFUL
Total time: 0 seconds
C:myproject>

--

--