Chapter 6. User-Written Listeners

Writing a log is intrinsic to Ant. As you might expect, this functionality is built-in, and always on by default. What you might not expect is that you can modify the way Ant writes its logs. In fact, you’re not limited to just changing the logging mechanism. You can change the way Ant behaves during certain steps of a build. Ant provides this wonderful bit of flexibility in the form of an event model. Those familiar with GUI development have heard this term before, as GUI programming libraries are the most common libraries to put event models into practice. The concept of the event model is simple. The Ant engine maintains a list of objects that have requested to “listen” to the build’s various “events.” During processing, Ant announces these events, in the form of BuildEvent objects, to each of its listeners. The listeners, incidentally, are called BuildListeners. The BuildListener is a Java interface. Any time you want to write a new listener, implement the BuildListener interface in your new class and fill in the logic for each of the interface methods.

Writing your own class implementing the BuildListener interface is a straightforward undertaking, especially if you compare the effort to the effort required for writing an Ant task. The typical user-written listener turns out to be some form of specialized logger, replacing Ant’s built-in logging mechanism. Knowing this, Ant’s developers provide a BuildLogger class, extending from BuildListener ...

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.