Injecting JavaScript into Ant builds

Background

Recently I’ve been working on an implementation of hybris, a Java e-commerce package whose build system is based on Ant.  The Maven Ant Tasks plugin is available for importing dependencies, but I wasn’t happy with the default tasks that wrap this plugin.

AntThey make no attempt to purge old dependencies that have been removed from the POM.  The build script keeps your Eclipse projects up to date with most changes, but does not update classpath settings when dependencies change.  Most importantly, the Maven plugin uses the excludeTransitive=true option, so Maven will not import the dependencies of your dependencies.

Fortunately, every step in the build process includes a “callback”, which is a hook where you can inject your own customizations.  I decided to forgo the default task for updating Maven dependencies, and do it myself in one of the extension’s callbacks.

Cleaning the extension’s /lib directory was easy, as was setting excludeTransitive=false to resolve the full dependency hierarchy.  However, overwriting the Eclipse project’s .classpath file was a bit more tricky than I expected.  Basically, I needed to:

  1. Write to .classpath the fixed information that never changes
  2. Iterate through each dependency in the extension’s /lib directory, and append a line in .classpath for it
  3. Append the closing tag at the bottom of .classpath

Eek!  It’s been awhile since I’ve made heavy use of Ant, and I suddenly remembered that it doesn’t really have general loops or iterator constructs.  The open-source ant-contrib bundle comes installed with hybris, and it provides useful tasks such as <for> and <foreach>.  However, it is poorly-documented, and hasn’t been updated in forever (much like Ant!).  Also, the property scoping rules are very quirky, and it looks like I would have to create a completely separate Ant task just for the code within my loop.

There has to be an easier way to put a simple loop within an Ant script, right?

JavaScript in Ant?

I have been primarily living in the Maven world for some time now.  I knew that ad-hoc scripting is a standard feature in more modern build systems, such as Gradle (Groovy), Buildr (JRuby), and sbt (Scala).  However, after a little research I was shocked to discover that good-old Ant was there a decade ago!

With the comments powered by Disqus