Monday 13 August 2007

Managing ant dependencies

We tried several packages to manage ant build dependencies. Antlion looked useful, but the documentation was dreadful and we never worked out if it would do what we wanted or not. Ivy looked useful at first, but it didn't really do the dependency management we wanted. We even looked at Maven. Stone me that is complicated, and even in version 2, too full of bugs to be useful. I wasted two days on that before giving up in despair and returning to ant.

My colleague Jon Siddle decided to write his own ant dependency manager which he has called decorum. It does just enough dependency management -- it doesn't download stuff from ibiblio, but it does know about other ant projects in the same file system and will build them if their targets are out of date. I works very well in a project with many sibling sub-projects. It also builds the path refs for the compile and test targets. And it handles external jars (which we don't usually build) nicely as well.

Here's a simple example: say my project needs log4j. All I need to do it put these lines in my build.xml:

<taskdef name="dependencies" classname="org.trapdoor.decorum.Decorum">
<classpath location="${lib.dir}/decorum-0.2.jar" />
</taskdef>

This tells ant to add Decorum's task definition. Now I can add dependencies entries to my build.xml:
<dependencies id="main" libdir="${lib.dir}" excludes="clean">
<dependency name="log4j" />
</dependencies>
When I use the generated path in my compile target, it will find the most recent version of log4j in the library directory:
<target name="compile" depends="init">
<javac srcdir="${src.dir}/main/java" destdir="${build.dir}/main/classes"
includes="**/*.java" classpathref="path.main">
In this example "path.main" refers back to the id I gave my dependencies.

Here is a complete [FILE REMOVED -- I no longer use decorum] example with minimal decorum dependencies. In my next post I will illustrate some of the more advanced features like version and building.

No comments: