John Topley’s Weblog

Awesome Ant

The latest edition to my J2EE armoury is Apache Ant, which I've spent the past few days investigating. For those who don't know, Ant—which stands for Another Neat Tool—is a Java automated build tool. It uses an XML configuration file (or files) and can be used to perform a multitude of tasks. Tasks such as compiling Java source code, creating JAR, WAR or EAR archives, copying or moving files, generating Javadoc pages, deploying files to an application server, even running unit tests. And if Ant's set of built-in tasks isn't enough for you, you can extend it by writing your own Java classes. Ant build files can also call other Ant build files, which means that large projects can be more easily partitioned and managed.Apache Ant Logo

I did encounter one or two problems using Ant but only because I'm a newbie. I give details here in case anyone else gets caught out like I did.

I was using the jar task to create a Java archive and I was puzzled as to why I was ending up with two copies of each class in the JAR file. I had a block of code similar to this:

<jar destfile="${dist.jarfile}" basedir="${build.classes.dir}">
  <fileset dir="${build.classes.dir}">
    <include name="**/*.class"/>
    <exclude name="**/*Test*"/>

—I ran Ant using the -debug switch which generated a copious amount of output and made what was happening obvious. Re-reading the documentation confirmed it. The jar task forms an implicit Ant FileSet, so all I needed was:

<jar destfile="${dist.jarfile}" basedir="${build.classes.dir}">
  <include name="**/*.class"/>
  <exclude name="**/*Test*"/>

The other problem I came across prevented compilation of some source files because the compiler couldn't find some of the classes that the source depended upon. This took me a fair while to solve but it turned out to be because I'd specified an incorrect basedir attribute in my jar task, which meant that all of my .class files within my JAR were a directory deeper than they should have been!

In other words, instead of telling jar to start from the /classes directory, I'd told it to start from the project's root directory, meaning that the path to the compiled classes started with classes instead of gov which is the start of my package hierarchy. Doh!

I'm totally impressed with Ant and shall be using it for all my J2EE projects from now on. Although most Java IDEs now feature Ant integration, what it's crying out for is a decent GUI build file editor, i.e. an antidote to using Notepad!


There aren’t any comments on this post. Comments are closed.

I did encounter one or two problems using Ant but only because I'm a newbie.


  • Jan
  • Feb
  • Mar
  • Apr
  • May
  • Jun
  • Jul
  • Aug
  • Sep
  • Oct
  • Nov
  • Dec
  • 2019
  • 2018
  • 2017
  • 2016
  • 2015
  • 2014

More Archives

Sign In