An Example: XmlLogger

The XmlLogger source code is included with every source distribution of Ant. If you wish to follow along with the code, you’ll need to download the source distribution.[44] The XmlLogger redirects the normal logging output from Ant and writes it to a file in XML markup. Its simplicity and wide availability in source form make it a good example for learning how to write build listeners.

If you’re interested in seeing how XmlLogger works, test it with your standard Ant installation. There’s no need to download the source distribution as the XmlLogger class comes with all binary distributions. Unlike the case when adding tasks, there’s no need for you to declare a listener using markup in the buildfile like <taskdef>. Instead, declare it on the command line. First, insure the class is visible to Ant. You can do this by adding it to your system’s classpath or by packaging it into a JAR and placing the JAR in ANT_HOME/lib. Then, specify the listener class as an argument to the ant command. The -listener listenerClass argument notifies the Ant engine that it must add the specified listener class to the internally managed list of build listeners. You may specify more than one listener argument, with no limit on the total number. Well, there’s almost no limit. Any command-line byte-length limitations inherent to your shell still apply. To use the XmlLogger listener, you call ant like so:

ant -listener org.apache.tools.ant.XmlLogger

Running this command and its argument ...

Get Ant: The Definitive Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.