Creating Custom Listeners
Ant tracks build events, such as when tasks start and
finish, and you can catch those events with a
listener. Listeners implement the org.apache.tools.antBuildListener interface
and will receive BuildEvents for
these events:
Build started
Build finished
Target started
Target finished
Task started
Task finished
Message logged
To add a listener in code, you can create an ant Project object and then call its addBuildListener( ) method to add a listener
to the project. You can attach a listener to a build from the command
line as in this example:
ant -listener org.apache.tools.ant.XmlLogger
which runs Ant with a listener that generates an XML version of the build progress.
Warning
Listeners and loggers must not access System.out and System.err because output on these streams
is redirected by Ant's core to the build event system. In other words,
accessing these streams may cause an infinite loop.
To implement the listener interface, you implement methods
such as buildStarted( ), buildFinished( ), targetStarted( ), and so on. In
SoundListener.java, shown in Example 12-16, the listener code
uses the org.apache.tools.ant.taskdefs.optional.sound.AntSoundPlayer
class to play a sound when the build is finished. The addBuildSuccessfulSound(java.io.File file, int
loops, java.lang.Long duration) method is used to add a sound to
play for build success, and addBuildFailed-Sound(java.io.File fileFail, int loopsFail, java.lang.Long duration) to add a sound indicating build failure. ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access