Creating a new task for Ant is very straightforward. The actual need for such work is quite rare since for the majority of cases Ant has a built-in task to do what is required.
To begin, create a class that extends
org.apache.tools.ant.Task and a setter method (i.e.,
setXXXX) for each attribute. Finally, add a public void execute
method, with no arguments, that throws a
BuildException. This method implements the actual task
This simple example, taken from the Ant documentation, provides a
task which logs the message attribute to
public class NewTask extends Task
// The method executing the task
public void execute() throws BuildException
System.out.println( msg );
// The setter for the "message" attribute
public void setMessage( String msg )
this.msg = msg;
private String msg;
To add the task to Ant, simply declare a
element to your project:
<taskdef name="newtask" classname="com.domain.NewTask"/>
And to use the new task, simply use it within a target as with any other task:
<newtask message="Hello World! NewTask works!"/>
Before running Ant, ensure that your new task class is in the
classpath. An alternative method for adding your task is to add the
task name and implementing class name to the
default.properties file in the
org.apache.tools.ant.taskdefs package. Now your
task can be used as though it was built into Ant.
The Jakarta Project homepage: this is where to find the Ant homepage
As with any new tool, it's up to the various individuals and teams out there to decide if Ant fits with the way they currently work or the way they want to work. Many other Java open source projects now use Ant as the build tool, particularly the various Jakarta projects but also external projects like JDOM.
At The Games Kitchen, we have used Ant in several trial projects and are now migrating our entire build process to Ant owing to its success. The fact that the supplied tasks provide integration to several existing source control systems, including ClearCase, Perforce, and Visual SourceSafe, should make most migrations relatively straightforward.
I recommend people either make the change to Ant or at least investigate it as a serious option for managing the build process.
David Thomson is CEO & Head Chef at The Games Kitchen Ltd.
Return to ONJava.com.