This example shows how to write a simple Ant task. It demonstrates how to access the
Project object -the equivalent to the currently executing build file-, and the
Target object that called the task. It also accesses data from the build file, both in the form of a property ("name") and a task parameter ("myArg").
See the Ant manual for more elaborate examples and explanations.
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Target;
import org.apache.tools.ant.Task;
public class MyTask extends Task {
private String myArg;
private Project proj;
private Target target;
@Override
public void init() throws BuildException {
proj = getProject();
target = getOwningTarget();
proj.log("MyTask init");
}
@Override
public void execute() throws BuildException {
log("target name: " + target.getName());
log("value of property 'name' = " + proj.getProperty("name"));
log("task name: " + getTaskName());
log("value of task parameter 'myArg' = " + myArg);
}
public void setMyArg (String _myArg) {
this.myArg = _myArg;
}
}
It would be invoked like this:
<property name="name" value="myPropertyValue" />
<taskdef name="MyTaskName" classname="MyTask" />
<target name="myTarget">
<echo message="before MyTask"/>
<MyTaskName myArg="myArgValue" />
<echo message="after MyTask"/>
</target>
CodeSnippets