6 Tips for Improving Apache Ant

I have worked with Apache Ant for years. I keep running into its limitations, probably because I have never used it to compile Java! On the plus side, it has some of the trappings of a functional language:

  • Ant has unchangeable “properties” instead of variables. (OK, FuncFans call them “immutable”, not unchangeable.)
  • “Target” dependencies are declarative, rather than procedural.

Apache Ant also has limited functionality, especially in parsing files, parsing strings, looping, and I am sure there are more that I have forgotten.

There is one more annoying bit about Ant: you write it in XML. That means it is extra work to edit it. Even if you are using a powerful XML editor that closes tags automatically, renames closing tags when the opening tag is renamed, and helps you wrap new elements around existing ones, that is not enough.

Those features are all available in the oXygen XML editor, and in many other XML editors, but you need more. You need automatic lists of allowed elements for the spot you are editing, and automatic lists of attributes allowed for the element you are editing. oXygen XML doesn’t provide those features, because you can extend Ant with the element, giving you an unpredictable set of XML tags. I haven’t seen any editor that deals with this properly. Let me know if I missed one!

So my first conclusion is, don’t use Apache Ant for a new project.

However, maybe Ant has already been picked for you. Picked by a previous designer, picked by a project that you need to work with. That’s too bad, but you can work with it. Here are some tricks to make Ant more fun to work with.

Ant Contrib

Grab it, download it, it’s just a small jar file. Learn the funky chord of Ant that makes it work. Play with it.

You need it, if only just because of “if”, and then for “for”, or else “foreach”. Didn’t you always want to write that sentence in an English essay, maybe without the quote marks?

The funky chord is:
<taskdef resource=”net/sf/antcontrib/antcontrib.properties”/>

This works if you put the antcontrib jar into the Ant lib folder with the standard Ant jars. Otherwise, see the Installation instructions on the main page of the Ant project. Caveat: Some of the specialized loops don’t work if your project starts by calling Ant from inside a Java program, rather than from the command line. The Ant Contrib project is here: – http://ant-contrib.sourceforge.net/ In the Apache Ant project, Ant Contrib isn’t listed with External Tools and Tasks, but rather with Related Projects.

#2 Download the Ant Manual, and the Ant Contrib manual

When you have them at your fingertips, maybe in a bookmark bar in your browser, you can access them much faster than if you run them from their home on the web. You can even get at them when you’re offline.

#3 Get an Ant book

I have “Ant, the Definitive Guide”, which is out of date. I still refer to it as most of the language is backwards compatible, even at 1.9.7. There are several other books, including a newer version of Ant the Definitive Guide. There is a wikibook: – https://en.wikibooks.org/wiki/Apache_Ant The Ant project has a list of books on the Resources page: – https://ant.apache.org/resources.html

#4 Output lists of system properties

This is a great help in debugging. You can dump it in the main log file, or a separate file. You can list the Properties set by Java, by Ant, or all of them. You can choose to list only those with a common prefix. See the docs for details on how to use:

#5 Read settings from files

You can store settings in files, in Java properties format. Remember that Java Property format allows you to have spaces before and after the equal sign, which makes the file easier to read and edit.. To read a Java properties file, use:
<propertyfile file=”quagmire.properties”/>

Set properties from the command line, or not

Any properties you set from the command line, perhaps from a calling program, will be completely immutable in your Ant program, even in other Ant files called from it with <ant/> and <antcall/>.

If you need to repeatedly call a task from a set of values, make sure that, on the command line, you do not set any of the properties that change for each value. One option is to set a temporary property on the command line, then use that one to set the real property when your code needs it.

These six suggestions should make it easier to use Apache Ant, when you have to. Let me know if there is an editor or IDE out there that edits Apache Ant with all the usual features that other languages get, even when you add AntContrib and other addons!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s