Recommended: Sing it, brah! 5 fabulous songs for developers
JW's Top 5
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 3 of 5
The simple answer to the question "Can Ant do XYZ?" is "Yes, but you may have to customize it."
Two Ant extensions are interesting to discuss at this point. They are increased reporting and the ability to distribute code remotely using Ant.
If you wanted to extend Ant's functionality to provide notification when certain steps in the build process are completed or are in progress, you can create a class to listen to the Ant process as shown in the following example.
You can create a class that implements the BuildListener interface. Using this class, you can catch each event that is part of the Listener:
public void buildStarted(BuildEvent event);
public void buildFinished(BuildEvent event);
public void targetStarted(BuildEvent event);
public void targetFinished(BuildEvent event);
public void taskStarted(BuildEvent event);
public void taskFinished(BuildEvent event);
public void messageLogged(BuildEvent event);
The BuildEvent event object contains the following methods by which you can obtain information about the current status of the build:
public Project getProject() ;
public Target getTarget() ;
public Task getTask();
public String getMessage();
public int getPriority();
public Throwable getException();
So if you wanted to write a reporting tool, you need only create a class that implements the BuildListener interface and process the BuildEvents as needed by your design. Because Ant initiates the Java classloader, you must specify the listener as part of your command
line arguments. For example:
ant -listener org.apache.tools.ant.XmlLogger
This listener is included with the Ant distribution and outputs an XML representation of the build process to a file called "log.xml".
The above example shows how to extend the functionality of build reporting. It is of more interest to show how to extend the functionality of the build itself. For that, I've chosen to work through an example in which two files are copied from one machine to another machine, which then performs the Ant operation.
To do that, you can extend Ant by creating custom Task objects for the remote copy and remote ant commands. Here's an explanation of the remote copy task definition.
The RemoteTask object extends the Task object. The RemoteTask object performs all of the functionality necessary for the maintenance of the connection between the local and remote machines.
In this case, the connection is a socket-level connection.
The RemoteTask object contains the following method declaration that is necessary for any object that is to extend the Task object:
public void execute() throws BuildException
The Ant processor calls this method after all of the attributes have been set. Any custom Task object must override this method.
The RemoteCopyTask performs the steps required to execute the remote copy operation. The copy operation is on the local machine and transfers
files from the local machine to the remote machine. Some key things to notice in the RemoteCopyTask code are the three accessor methods that allow the creator of the Ant buildfile to set the name, directory, and file type
of the file to be transferred.