You are on page 1of 19



Using Ant to build J2EE Applications

Rajesh Kumar

How does ANT work ?
Sample Build file
Built-in properties
ANT – Different flows
Writing your own task
Command-line options
IDE Integration

What Is Ant?
ü A build tool like ‘make’
ü Open source
ü – from the Apache Jakarta project

ü –

ü Implemented in Java
ü Used to build many open source products

Introduction ..contd
ü Ease of use
– Ant is extended using Java classes
– The configuration files are XML-based, calling out a target
tree where various tasks get executed
– Same config file (build.xml) can be across multiple platorms

How does ANT Work ?

ü Ant commands (or tasks) are implemented by Java classes
– many are built-in
– others come in optional JAR files
– custom commands can be created
ü Each project using Ant will have a build file
– typically called build.xml since Ant looks for this by default
ü Each build file is composed of targets
– these correspond to common activities like compiling and running code
ü Each target is composed of tasks
– Each task is run by an object that implements a particular Task interface
– executed in sequence when the target is executed
– like make, Ant targets can have dependencies
– for example, modified source files must be compiled before the application can be run

How does ANT Work ? ..

üTargets to be executed
– can be specified on the command line when invoking Ant
– if none are specified then the default target is executed
– execution stops if an error is encountered
– so all requested targets may not be executed
ü Each target is only executed once
– regardless of the number of other targets that depend on it
– for example
• the “test” and “deploy” targets both depend on “compile”
• the “all” target depends on “test” and “deploy”
but “compile” is only executed once when “all” is executed
ü Some tasks are only executed when they need to be
– for example, files that have not changed since the last time they were compiled are not

Sample Build file

<project name=“test" default=“hello">
<target name=“hello" depends=“setup, pre-hello1, pre-hello2“>
<echo> Hello World</echo>
<target name=“setup”>
<property name=“” value=“MindTree”/>
<condition property="">
<os name="SunOS" />
<target name=“pre-hello1” if="“>
<echo> You are running this script in Solaris </echo>
<target name=“pre-hello2” unless="“>
<echo> You are NOT running this script in Solaris </echo>

Sample Build file

Save the file as test.xml in some temporary folder ( Say C:\Temp)
Set ANT_HOME= C:\Jakarta-Ant-1.5
Cd C:\Temp
ant –buildfile
Buildfile: test.xml
[echo] You are NOT running this script in Solaris
[echo] Hello World
Total time: 1 second

Built-in Properties
ü Ant provides access to all system properties
and also has some additional properties.
basedir The absolute path of the project's basedir
(as set with the basedir attribute of
ant.file The absolute path of the buildfile.
ant.version The version of Ant The name of the project that is currently
executing; it is set in the name attribute
of <project>. the JVM version Ant detected;

Ant – Different flows

Using “depends”
Using “antcall”
Using “ant”

Ant – Different flows

Using “depends” – Last task to first task
Eg :
<target name="compile" depends="init, setup" description="compile
the source " >

<!-- Compile the java code from ${src} into ${build} -->

<javac srcdir="${src}" destdir="${build}"/>


Ant – Different flows

Using “antcall” – Sequential & Functional oriented

Calling different targets in the same build.xml (very similar to calling functions in regular programming language)

Eg :

<antcall target="copymodule">

<param name="" value="user"/>


<target name="copymodule" if="gws.prepared">

<echo>Module : ${} </echo>

<copy todir="${}/j2ee-apps/gws/${}" includeEmptyDirs="no">

<fileset dir="${gws.class.folder}">


<include name="**/${}/**"/>





Ant – Different flows

Using “ant”
This is used for running scripts for sub-
Eg :
<target name="ROOT">

<ant dir="${basedir}/ROOT" target="dist"/>


<target name="examples">

<ant dir="${basedir}/examples" target="dist"/>

<ant antfile="subproject/subbuild.xml" dir=“$

{basedir}/subproject" target="compile"/>


Core & Optional tasks

Writing your own task

1. Create a Java class that extends

2. For each attribute, write a setter method.

3. Implement the interface if your task contains other

tasks as nested elements

4. Write a public void execute method, with no arguments, that throws a BuildException

5. Adding your task to the system

1. Make sure the class that implements your task is in the classpath when starting Ant.
2. Add a <taskdef> element to your project. This actually adds your task to the system.
3. Use your task in the rest of the buildfile
6. Eg:
<?xml version="1.0"?>

<project name="OwnTaskExample" default="main" basedir=".">

<taskdef name="mytask“ classname="com.mydomain.MyVeryOwnTask"/>

<target name="main">

<mytask message="Hello World! MyVeryOwnTask works!"/>



Command line options

ant [options] [target [target2 [target3] ...]]


-help print this message

-projecthelp print project help information

-version print the version information and exit

-diagnostics print information that might be helpful to diagnose or report problems.

-quiet, -q be extra quiet

-verbose, -v be extra verbose

-debug print debugging information

-emacs produce logging information without adornments

-logfile <file> use given file for log

-l <file> ''

-logger <classname> the class which is to perform logging

-listener <classname> add an instance of class as a project listener

-buildfile <file> use given buildfile

-file <file> ''

-f <file> ''

-D<property>=<value> use value for given property

-propertyfile taking precedence

-inputhandler <class> the class which will handle input requests
-find <file> <name> load all properties from file with -D properties search for buildfile towards the
root of the filesystem and use it

IDE Integration
ü Ant can be integrated with the following Java
– Jbuilder
– IntelliJ Idea
– Eclipse
ü See the Ant User Manual for more details
– in

Home –


Mailing Lists


Java Development with Ant -

Ant: The Definitive Guide -

Related Projects :

Maven -

Centipede -

Tools built over Ant :

AntHill -

CruiseControl -

Thank You !