Using Ant Libraries with AntBuilder Add comment to Wiki View in Wiki Edit Wiki page Printable Version


Recent version of Ant have included a mechanism called Antlibs. These allow you to define your own custom tasks, group them together with the appropriate definitions needed by Ant and use them in your Ant environment without nameclashes. Nameclashes are avoided by using namespaces. Numerous Antlibs are now available from both Apache (the developers of Ant) and other sources. Using these libraries with Groovy is fairly easy - though you have to be careful with some of the details.


The AntUnit antlib includes predefined <assert> tasks corresponding to the most common kind of checks you want to do within your build files. They are using thoughout the Ant codebase to test many of the ant tasks but you can use these assertions in your own build files (or any Groovy code) too.

Here is an example the uses the assertFileDoesntExist and assertFileExists checks.

First, we'll consider the traditional way of incorporating this antlib, by using namespaces (you'll need the antunit jar in your classpath before you begin - as we are relying on Ant's autodiscovery of antlibs mechanism here):

Notice that the antunit assertions all exist within their own namespace. That's OK for now, Groovy allows special symbols in method names so long as you include the method name in quotes.

We can also incorporate the antlib directly into the default namespace as follows:

This makes our code look simpler for this example but be careful with this approach though as you need to avoid name clashes. The preferred way is to use the NamespaceBuilder. Using this, our code becomes:

Maven Ant Tasks

Another useful antlib is the Maven Ant Tasks. They allow you to use Maven's artifact handling features from within Ant including:

  • Dependency management - including transitive dependencies, scope recognition and SNAPSHOT handling
  • Artifact deployment - file and SSH based deployment to a Maven repository
  • POM processing - for reading a Maven 2.0.x pom.xml file

Here is how you could use these tasks to download some required jars into your local maven repository cache (~/.m2 directory).

When run, this produces a log of the maven ant task activity, such as:

Downloading: jfree/jfreechart/1.0.5/jfreechart-1.0.5.pom
Transferring 298K

We can take this example further and show how to create the JFreeChart example from Plotting graphs with JFreeChart without having the JFreeChart jars statically defined in our classpath.

First another helper class:

Now, here is the code we require to dynamically download the JFreeChart jars and add them to our classpath then run the script:

Ivy Tasks

We can also download jars using Ivy. In this case we use MarkupBuilder to build an XML file that the Ivy retrieve task will use:

When run, this results in the files being downloaded:

[antlib:org.apache.ivy.ant:retrieve] :: Ivy 2.0.0-alpha-1-incubating - 20070416155158
[antlib:org.apache.ivy.ant:retrieve] downloading
[antlib:org.apache.ivy.ant:retrieve] 	[SUCCESSFUL ] [ jfree | jfreechart | 1.0.5 ]/jfreechart.jar[jar] (16735ms)
[antlib:org.apache.ivy.ant:retrieve] downloading
[antlib:org.apache.ivy.ant:retrieve] 	[SUCCESSFUL ] [ jfree | jcommon | 1.0.9 ]/jcommon.jar[jar] (6812ms)
[antlib:org.apache.ivy.ant:retrieve] :: resolution report ::
	|                  |            modules            ||   artifacts   |
	|       conf       | number| search|dwnlded|evicted|| number|dwnlded|
	|      default     |   2   |   2   |   0   |   0   ||   2   |   2   |
[antlib:org.apache.ivy.ant:retrieve] :: retrieving :: [ codehaus | grails ]
[antlib:org.apache.ivy.ant:retrieve] 	confs: [default]
[antlib:org.apache.ivy.ant:retrieve] 	2 artifacts copied, 0 already retrieved

If you included the optional report step (and add ant-trax.jar from your ant distribution to our classpath), then you would have some additional log information and it would produce the following pretty report on dependencies:



Results of your search request can come from various sources: the Groovy website itself, the JIRA issues, the API documentation, as well as a few other interesting Groovy-related blogs.

  By  -  pages  -  views  - last modified