O'Reilly logo

Ant: The Definitive Guide by Jesse Tilly, Eric M. Burke

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required