Step -1: Download Ant
You can download the latest binary from http://ant.apache.org/
Step -2: Download JDK
JDK is mandatory.
- Download from http://www.oracle.com/technetwork/java/javase/downloads/index.html
- Install JDK
- set JAVA_HOME environmental variable to point to JDK installation folder
- Ensure the bin folder of the JDK is in PATH
Step -3: Installation
This tutorial examples are taken from Linux. But the commands should be common for all the platforms.
Extract the downloaded ant binary.
Goto ant/bin folder
Try to run ant
pandian@Kannan:~/Downloads/ant$ ant
Buildfile: build.xml does not exist!
Build failed
The Ant binary will contains an xml called fetch.xml. Execute the following command where the fetch.xml is available
pandian@Kannan:~/Downloads/ant/apache-ant-1.8.3$ ant -f fetch.xml -Ddest=system
Buildfile: /home/pandian/Downloads/ant/apache-ant-1.8.3/fetch.xml
.....
BUILD SUCCESSFUL
Total time: 4 minutes 14 seconds
Step -4: Build XML file
The build instructions are given in XML format. By default ant looks for build.xml. That contains several tasks called targets. To explain the terminologies, A single project has multiple tasks – compile, build jar, upload etc.
A sample build.xml is given below.
The project name is “test project”. It contains a single target called ‘initialize’ (more will be added later)
pandian@Kannan:~/Downloads/ant$ cat build.xml
<?xml version="1.0"?>
<project name="test project" default="initialize">
<target name="initialize">
<echo>This is a test project</echo>
</target>
</project>
Execute ant
pandian@Kannan:~/Downloads/ant$ ant initialize
Buildfile: /home/pandian/Downloads/ant/build.xml
initialize:
[echo] This is a test project
BUILD SUCCESSFUL
Total time: 0 seconds
Step -5: Read externalized parameters from property files
You can have the important parameters or fields in a property file and import it to build process to make it more flexible. Here is how you can do it.
I have a property file with 3 parameters
pandian@Kannan:~/Downloads/ant$ cat ant.properties
name=grassfield
city=London
country=United Kingdom
I am adding a new target to read and print one of the params.
<target name="get_properties">
<property file="ant.properties"/>
<echo>Imported Properties for name ${name}</echo>
</target>
Now, lets execute the target
pandian@Kannan:~/Downloads/ant$ ant get_properties
Buildfile: /home/pandian/Downloads/ant/build.xml
get_properties:
[echo] Imported Properties for name grassfield
BUILD SUCCESSFUL
Total time: 0 seconds

Pingback: Ant – the packaging system for Java – HowTo 2 « JavaShine