Introducing ANT
ANT is an acronym for Another Neat Tool. It is a build tool. It is used to automate complicated repetitive tasks like setting classpath, compiling the code or packing the compiled files and many more things which you can ever imagine. ANT is developed in JAVA so it is platform independent. Ant accepts instructions in the form of XML documents thus is extensible and easy to maintain. You don’t need any special editor for writing ANT files a normal text editor is enough.
ANT Installation:
If you are using some IDE like Eclipse then there is no need to download anything as most of the IDE come with ANT libraries.
If you are not using any IDE and doing all in notepad and you
need to do these things:
Steps to set path cmd if you
are not using any IDE:
- Download the files from http://jakarta.apache.org/ant/index.html
and unzip them to some directory.
In our case we are naming the folder as ant. - Append /path/to/ant/bin to the PATH environment variable.
- Append the .jar files in /path/to/ant/lib/ to the CLASSPATH environment variable. Set JAVA_HOME to point to the location of the JDK installation on the machine that the software is being installed on. Append /path/to/jdk/lib/* to the CLASSPATH environment variable.
ANT file(build.xml) format:
ANT file is an XML file. As ANT is mostly used for building the
project we name it as build.xml.
Here you can see a snapshot of the build file. It is written to complie
the code present in src folder.
This is the declaration of the XML file
with its version. All files must start by this.
This project element has three
attributes.
1. name: Here you will write the name of your project
for which you are writing this file.
2. default: This is the default target to be executed
by ant file.
3. basedir: This is the base directory(root folder) of
your project. Here . means current directory. If this is omitted the
parent directory of the build file will be used.
This explanation is for you. DO IT
YOURSELF.
This element allows you to write your
own variable with their values. You can define as many properties as you
need. This is a name-value pair where you can access the value of a
property by writing it in $ {<proprtyNmae>}.
NOTE: THERE IS NO SPACE BETWEEN $ AND { }.
This point has two elements.
1. Target: This element can be
treated as a function which has a set of instructions to do. It has an
attribute name which is used to call the target of the specified name.
2. mkdir: This element is used to make directries. It
has an attribute dir which takes the name of the directory to be made.
This target compiles the code. Here the
depends attribute specifies that this target depends on init to
be performed.javac uses the system java compliler if
you have not given the classpath explicitly. So to run this you should
have all the JAVA_HOME and PATH variables set.
srcdir is the source directory in which all the source
file(.java) are present and destdir is the folder into
which all the complied files(.class) will be created.
Writing Build Files for compiling the project:
Here we will write an ant file which will perform a clean build of the project. Below you can see snapshot of the build.xml file. In this project we have all the source files in src folder and we have only one file HelloWorld.java to be complied.
<?xml version="1.0" encoding="UTF-8"?>
<!-- Project name Declaration -->
<project name="HelloWorld" basedir="." default="compile">
<!-- Setting the property here -->
<property name="bin" value="bin" />
<!-- This target makes a fresh directory if not present -->
<target name="init" depends="clean">
<mkdir dir="${bin}" />
</target>
<!-- Delete the old directory with all its files -->
<target name="clean">
<delete dir="${bin}" />
</target>
<!-- Compiles the source files -->
<target name="compile" depends="init">
<javac srcdir="src" destdir="${bin}" />
</target>
</project>
As the code is self Explanatory we will leave all the
dicussed topics and will bring new important thing called dependency.
depends: When a target declares depends attribute then that
target is only executed after the execution of the target on which it
depends.
- In this case the dependency goes like this
- Compile is the default target which the file calls.
- Compile target depends on init so init target starts executing.
- Now init also depends on clean so the target names clean will be executed before the execution of init.
- So first of all target clean is executed, then init and then compile is executed.
Note: You can write more than one names of target in depends attribute like depends=”clean,init” and the execution is from left to right. So first clean will be executed and then init will get executed.Be careful while declaring dependency.
Running an ANT file
If you are using IDE like eclipse you can simply right click and select Run As –> ANT Build

If your classpath and code is all correct then you will see the following text in your console

If Environment variables like PATH and JAVA_HOME is not set in your system than you will get an error as shown below.

So if you get this error message forst set all the variables and then restart the eclipse and run the ant file again.
Note: In ant the error messages are well explained so error can be identified easily. If you set the verbose mode by giving the argument -v then you will get detailed log in the console.From http://himanshugpt.wordpress.com/2010/04/22/introducing-ant/
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)






Comments
Daniel Alexiuc replied on Fri, 2010/04/23 - 12:40am
Lieven Doclo replied on Fri, 2010/04/23 - 1:14am
Zqudlyba Navis replied on Fri, 2010/04/23 - 1:44am
Nicolas Frankel replied on Fri, 2010/04/23 - 5:40am
in response to: dalexiuc
James Sugrue replied on Fri, 2010/04/23 - 6:48am
in response to: dalexiuc
I think this is useful content. OK, a lot of people might already know ANT, or have moved on from it. But there's still an audience for introductory material to well established technologies.
James
Peter Veentjer replied on Fri, 2010/04/23 - 10:27am
Himanshu Gupta replied on Fri, 2010/04/23 - 10:32am
Mike P(Okidoky) replied on Fri, 2010/04/23 - 11:10am
Arek Stryjski replied on Fri, 2010/04/23 - 12:21pm
I think this audience is about people who work with Java for years now, and still would like to learn more about Java, other JVM languages, and new Java tools/projects.
There is nothing wrong with writing article about ANT this days, but this is not a place to publish it.
Is DZone trying to radically change its profile, and the audience?
Himanshu Gupta replied on Fri, 2010/04/23 - 12:40pm
in response to: areks
David Lee replied on Fri, 2010/04/23 - 1:55pm
It was worthy article and definitely worthy to be posted on this site.
99% of the articles posted on java lobby get no responses because they have no practical value to most of developers. Just look at the front page today.
Maybe we should make a new site, where this type of practical info is welcomed.
Mike P(Okidoky) replied on Fri, 2010/04/23 - 2:38pm
in response to: sybrix
Ankur Gupta replied on Fri, 2010/04/23 - 8:07pm
Bruno Borges replied on Sun, 2010/04/25 - 1:18pm
I just wonder why you linked the article to the old Ant URL.
Ant became an Apache top level project on 2002.
I almost forgot for a moment that Ant was actually a Jakarta project years ago. :-)
Still, nice article for newbies.
James Sugrue replied on Sun, 2010/04/25 - 11:31pm
in response to: himanshu13
Himanshu, you are right in your assumption of the JavaLobby audience, and that is everyone in the community, experts and non-experts. An article that goes back to basics does no harm, and can be a really useful reference for the rest of the visitors of the site.
Keep up the good work
James
Benedict Aluan replied on Wed, 2010/04/28 - 12:09am