ANT : Java Glossary

*0-9ABCDEFGHIJKLMNOPQRSTUVWXYZ (all)

ANT ANT
ANT (A Neat Tool). It is an open-source Java-based platform-independent Make tool. The current version is 1.9.7. Last revised/verified: 2016-04-12
Introduction Javac srcdir
Advantages Javac destdir
Disadvantages Javac classpath
Configuring Tips
Running When To Clean Compile
ANT Scripts Extracting Version Number from a File
Complex Build Script Stomper
Simple Build Script Summary
Meta Script Extra Libraries
Learning ANT Books
basedir Links
Antish or Javish?

Introduction

Javac does not need a make since it is capable of figuring out for itself what needs to be recompiled, however, ANT is useful for all the auxiliary build tasks like time stamping, copying, creating jars, jar signing, bundling for distribution, version control, logging, testing… ANT is multiplatform where batch files are not, making it better suited for team programming where developers may be using a variety of OSes (Operating Systems). You might think of it more as an XML-based scripting language than a simple make. Ant is not clever enough to figure out which classes it needs to include in a jar. An ANT-extension called Ivy, tackles that problems.

Advantages

Disadvantages

Configuring

ANT is relentlessly multiplatform and hence requires almost no configuring. It comes as a zip without an installer. ANT does not use the registry. You must put its bin directory on the PATH for it to find its executables and set up ANT_HOME in the set environment using settings ⇒ Control Panel ⇒ system ⇒ advanced ⇒ environment.

SET ANT_HOME=J:\Program Files (x86)\apache-ant-1.8.2

I found that sometimes ANT could not find its own class files. I solved this by copying all its jars to the java ext directory. ANT requires that the environment variable JAVA_HOME be set correctly to J:\Program Files\java\jdk1.8.0_112\.

If you use genjar, copy GenJar.jar and genjar.properties to "J:\Program Files (x86)\apache-ant-1.9.7\lib\".

Running

Here is how you might run an ANT script build.xml in the current directory:

or using CyWin Bash:

rem build with ANT under CygWin Bash
rem -v = verbose
rem -Duser.home=W: = set System property user.home to W:
rem -f build2.xml = use ANT script build2.xml (default is build.xml)
rem build = run the "build" target

ant -v -Duser.home=cygdrive/w -f build2.xml build

ANT Scripts

Following, in the next major section, is a fairly complicated script, one I use in production to build and sign a jar for the Wassup Applet. Just try to get the general drift of how it works.

Complex Build Script

This build script builds Wassup, a signed Applet.

Simple Build Script

Here is a much simpler script. It compiles and creates a jar for the com.mindprod.holidays.Holidays Applet. This Applet has a set of dynamic classes, one for each holiday in the year. It also has a resource properties file listing all the dynamic classes. You can take this script, change the properties in the definitions section and use it for your own projects.

Note that a typical ANT script only compiles explicitly mentioned *.java files and their dependencies. It does not do a javac *.java.

Meta Script

This script compiles many other scripts, doing the cleans in all scripts first, then the compiles etc.

Learning ANT

ANT is an unusual program. No matter what you tell it to do, it rarely complains. It trusts you completely and finds some way to interpret your commands as reasonable. It is just that it will compile quite unexpected files, put them in quite unexpected places and delete quite unexpected files. Do your initial experiments on a small set of expendable files until you get the hang of how it works. You can easily be lulled into a false sense that you understand ANT when you don’t.

basedir

ANT is quite elegant and simple. Nearly all the problems I had with it come from the manual which was written by people who knew too much and failed to explain the basics, such as what the basedir is. Let’s say you want to compile the classes in the com.mindprod.holidays package, what is the basedir? Nope. None of them!

The ANT people tend to set up directory structures like this: C:\project7\source\com\mindprod\holidays\ for the *.java files and C:\project7\build\classes\com\mindprod\holidays\ for the *.class files.

If you do things the Antish way, then the basedir is C:\project7\.

If you insist on doing things the simpler way with classes and source in the same directory and you have a simple directory structure like this J:\com\mindprod\holidays\ for both *.java and *.class, then your basedir is C:\.

The basedir defaults to the current directory when ANT was invoked. This is rarely what you want. When you use a meta-ANT script to invoke all your little build.xml scripts using <ANT or <subant, it can set and believe it or not, even override the setting in the individual build.xml scripts.

Though I don’t see ANT experts doing this, it seems logical you should explicitly specify the basedir in each xml script. Examples often show it as . or *.*. To me, this makes no sense. Then the script only works if you put the build.xml in the basedir and cd to the basedir before you start the script.

Beware the index=true option on <jar. At least turn it off if Java can’t find your classes. The problem has been reported in older ANT versions when you have a manifest Class-Path entry. If you set it to true, you must include additional jars using the indexjars feature.

Antish or Javish?

What are the pros and cons of setting you your file structure in the Antish separate directories for source and classes or the Javish way of merging *.java and *.class files in the same directories?

Javish has the following advantages:

Antish has the following advantages:

Javac srcdir

Unlike basedir, srcdir is not built into ANT. It is just a convention. You have to define it with a property.

<property name="srcdir" value="source"/>
Further, javac is not smart enough to automatically look in the srcdir property. You have to explicitly specify it: the  If you define srcdir as a relative directory name, it is relative to basedir, not the current directory. Setting it to . means it is the same as the basedir.

Now where should srcdir point? It depends just how much you want to recompile. Let’s assume you just want to compile the classes in the com.mindprod.holidays package.

If you do things the antish way, srcdir should point to C:\project7\source\com\mindprod\holidays\ where the *.java files you want to compile are.

If you do things the simple way with *.java and *.class mixed then srcdir should point to J:\com\mindprod\holidays\.

Let’s assume you want to recompile every java source in the entire com tree.

If you do things the antish way, srcdir should point to C:\project7\source\com\,

If you do things the simple way with *.java and *.class mixed then srcdir should point to C:\com\.

Javac destdir

Like srcdir, destdir is just a convention for the name of a property. It defines which directory to put the compiled class files. Where should destdir point? If you do things the antish way, it should point to C:\project7\classes, not to C:\project7\classes\com\mindprod\holidays where the class files will eventually end up.

If you do things the simple way with *.java and *.class mixed then destdir should point to C:\, or just leave it out and javac will automatically put class files in the same directory as the corresponding source file.

Javac classpath

There are two ways to handle the classpath: The second way is preferable, since then your script will run on another machine without the environment classpath:

Tips

When To Clean Compile

Ant and Java combined create a mighty compiling engine that will blow your socks off. The reason is, it loads the Java compiler only once no matter how many source files you have. So even a clean compile is over in a twinkling.

My rule in Java is this:

Building the jars and the zips takes more time than compiling, so compiling is not the bottleneck any more. MAKE-like tracking dependencies as a game not worth the candle.

Extracting Version Number from a File

Stomper

ANT is verbose and repetitive. If you clone and modify scripts, then discover an error, spelling mistake or typo in the original, or discover a better way to do something, you will have propagated the error in many places to fix. So what I did was write a Java program to stomp out my ANT scripts like a cookie cutter. That way they are perfectly consistent, perfectly aligned and consistently commented. If I fix a bug in one script, it is fixed in all. I describe each package with a couple of lines and the cookie cutter produces the reams and reams of ANT coding.

I have not officially released my Stomper. It contains far too much code specific to my needs, but it might give you an idea of how you could write you own. You can view it in the Subversion repository. You debug once, not once for each package. Either everything works or nothing works. This is important because debugging ANT scripts is difficult. ANT gives you very little explanation of why it does what it does and the documentation is abysmal. You have to figure most things out by experiment or by looking at other people’s scripts.

Summary

Though ANT has two major warts:
  1. convoluted verbosity
  2. atrocious manuals
It has four features that make it mandatory for any medium to large project:
  1. It is cross platform. You need only one set of scripts for a team to do builds on wide variety of hardware. You can give build scripts to customers that will work without tweaking no matter what their hardware.
  2. It is much faster than doing the compiles with javac.exe.
  3. It works. It is relatively bug free. Once you learn how it works, it is actually easier than writing the equivalent batch scripts.
  4. It is extensible. If ANT is missing functionality, you can write Java code to add what you need. Most other scripting languages are closed.

Extra Libraries

Many ANT commands such as FTP (File Transfer Protocol) and GENJAR require auxiliary jars not part of ANT. See the Ant Library Dependencies Documentation to find out what these jars are and where to download them. Just put them in your ext directory to make them available to ANT.

Books

book cover recommend book⇒Ant in Action: Java Development with Ant, second editionto book home
by Steve Loughran and Erik Hatcher 978-1-932394-80-1 paperback
publisher Manning
published 2007-07-12
Convert JUnit and extending the core tasks. This is not just a repackaging of the ANT documentation. Also covers Ivy dependency manager.
Australian flag abe books anz abe books.ca Canadian flag
German flag abe books.de amazon.ca Canadian flag
German flag amazon.de Chapters Indigo Canadian flag
Spanish flag amazon.es Chapters Indigo eBooks Canadian flag
Spanish flag iberlibro.com abe books.com American flag
French flag abe books.fr amazon.com American flag
French flag amazon.fr Barnes & Noble American flag
Italian flag abe books.it Nook at Barnes & Noble American flag
Italian flag amazon.it Kobo American flag
India flag junglee.com Google play American flag
UK flag abe books.co.uk O’Reilly Safari American flag
UK flag amazon.co.uk Powells American flag
UN flag other stores
Greyed out stores probably do not have the item in stock. Try looking for it with a bookfinder.
book cover recommend book⇒Pro Apache Antto book home
by Matthew Moodie 978-1-4302-1309-3 paperback
publisher Apress 978-1-59059-559-6 hardcover
published 2005-11-16 B001PBEVYY kindle
Step by step examples.
Australian flag abe books anz abe books.ca Canadian flag
German flag abe books.de amazon.ca Canadian flag
German flag amazon.de Chapters Indigo Canadian flag
Spanish flag amazon.es Chapters Indigo eBooks Canadian flag
Spanish flag iberlibro.com abe books.com American flag
French flag abe books.fr amazon.com American flag
French flag amazon.fr Barnes & Noble American flag
Italian flag abe books.it Nook at Barnes & Noble American flag
Italian flag amazon.it Kobo American flag
India flag junglee.com Google play American flag
UK flag abe books.co.uk O’Reilly Safari American flag
UK flag amazon.co.uk Powells American flag
UN flag other stores
Greyed out stores probably do not have the item in stock. Try looking for it with a bookfinder.
book cover recommend book⇒Ant: The Definitive Guide, second editionto book home
by Steve Holzner 978-0-596-00609-9 paperback
publisher O’Reilly recommended 978-0-596-55253-4 eBook
published 2005-04-20 B0043EWU3Y kindle
You would expect there to have been an ant on the cover, but that creature was already spoken for Oracle PL /SQL Programming. Not one of O’Reilly’s better efforts. Covers only up to Ant 1.6.1.
Australian flag abe books anz abe books.ca Canadian flag
German flag abe books.de amazon.ca Canadian flag
German flag amazon.de Chapters Indigo Canadian flag
Spanish flag amazon.es Chapters Indigo eBooks Canadian flag
Spanish flag iberlibro.com abe books.com American flag
French flag abe books.fr amazon.com American flag
French flag amazon.fr Barnes & Noble American flag
Italian flag abe books.it Nook at Barnes & Noble American flag
Italian flag amazon.it Kobo American flag
India flag junglee.com Google play American flag
UK flag abe books.co.uk O’Reilly Safari American flag
UK flag amazon.co.uk Powells American flag
UN flag other stores
Greyed out stores probably do not have the item in stock. Try looking for it with a bookfinder.

This page is posted
on the web at:

http://mindprod.com/jgloss/ant.html

Optional Replicator mirror
of mindprod.com
on local hard disk J:

J:\mindprod\jgloss\ant.html
logo
Please the feedback from other visitors, or your own feedback about the site.
Contact Roedy. Please feel free to link to this page without explicit permission.

IP:[65.110.21.43]
Your face IP:[54.163.147.69]
You are visitor number