Himanshu Gupta is currently working as a Software Engineer in India. He has been working in Java for the last 3 years. He has a BE degree from Rajasthan University. He holds SCJP-5 and SCWCD-5 Sun certifications. Himanshu has posted 5 posts at DZone. View Full User Profile

Introducing ANT

04.23.2010
| 6587 views |
  • submit to reddit

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:

  1. 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.
  2. Append /path/to/ant/bin to the PATH environment variable.
  3. 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 $ {&ltproprtyNmae>}.
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
  1. Compile is the default target which the file calls.
  2. Compile target depends on init so init target starts executing.
  3. Now init also depends on clean so the target names clean will be executed before the execution of init.
  4. 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/

Published at DZone with permission of its author, Himanshu Gupta.

(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

Introducing ANT? Did I fall into a wormhole again?

Lieven Doclo replied on Fri, 2010/04/23 - 1:14am

It's the newest of tactics used by Ant lovers: pretend it's new and innovative again :)

Zqudlyba Navis replied on Fri, 2010/04/23 - 1:44am

Leave Britney alone.

Nicolas Frankel replied on Fri, 2010/04/23 - 5:40am in response to: Daniel Alexiuc

If you did, you are not alone. I ponder what to think of it myself...

James Sugrue replied on Fri, 2010/04/23 - 6:48am in response to: Daniel Alexiuc

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

I use ANT on a daily basis, only not in its original form, but as plugin in Maven and since recently in Gradle. So ANT is not completely exit :)

Himanshu Gupta replied on Fri, 2010/04/23 - 10:32am

This article is for the audience who are willing to learn ANT. They use it or not that depends on various factors. And Yes for the people who are new to Java world there is no harm in knowing what old technologies/frameworks were there and what were they lacking. Hope this makes the things clear.

Mike P(Okidoky) replied on Fri, 2010/04/23 - 11:10am

Ant eh? And I've been writing batch script using javac and zip all these years... omg where have I been... As for Maven, thanks but no thanks. Been generating build.xml using XSL templates for years, which works like a train.

Arek Stryjski replied on Fri, 2010/04/23 - 12:21pm

This article is for the audience who are willing to learn ANT.

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: Arek Stryjski

Well I don't want to get in some kind of conflict but I was totally unaware that this audience is about people who work with Java for years now. I thought that this is an open platform for all (newbies, wanna be experts and experts).

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: David Lee

Agreed! I'm sick of the hollow meaningless stylistic philosophical (wannabe) crap, and articles like this one about ant has more actual substance than most. I mean, how are the younger ones and the beginners get on board these days? By reading fluffy pointless crap that doesn't ever invoke a compiler, or about actual technology. Still many good tangible articles here on dzone though.

Ankur Gupta replied on Fri, 2010/04/23 - 8:07pm

huh another ANT - for a moment, I was tricked that ANT has probably come up with some new incarnation. I guess tutorials on ANT are in plenty and spread over universities, companies, open-source websites, etc.

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: Himanshu Gupta

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

Thanks. Really great article. Keep posting.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.