Java 2EE and XML Development

Скачать в pdf «Java 2EE and XML Development»


<target name=»run.task» depends=»compile»>


<echo>Running «run.task» target…»</echo>


<mytask message=»Hello from my task!» file=»messageFromTask.txt»/>


</target>


Invoking this target should produce identical results to the previous example, with the exception of a new file appearing in your working directory.

C.3.3 Integrating the task


To permanently add our new task to your Ant installation, you can edit the defaults.properties file in the Ant distribution JAR, adding its name and class name to Ant permanently. Taking the permanent approach means you need not explicitly declare the taskdef element in your build files anymore. See the comments in build.xml for more details.

C.4 Build listeners


Another extension you may wish to make to your Ant environment is to create a custom build listener.


DEFINITION A listener is a component that registers for callbacks with the Ant system when interesting events occur. Such events include message logging, beginning execution of targets and tasks, and ending the build process.


Common tasks for listeners are logging messages and sending notification of build problems to the configuration manager. Let us develop a simple listener to see how this aspect of Ant works.

C.4.1 Developing the listener


In this example, we develop a build listener that logs all messages produced by Ant during a build. The code for this component is shown in listing C.4. A class acting as a build listener must implement the callback methods of the org.apache.tools.ant.BuildListener interface. Our example listener is interested in two events; the messageLogged and buildFinished events.


Listing C.4 A build listener class


import java.io.*;

Скачать в pdf «Java 2EE and XML Development»