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").
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;
public void init() throws BuildException {
proj = getProject();
target = getOwningTarget();
proj.log("MyTask init");
}
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 'arg1' = " + myArg);
}
public void setMyArg (String _myArg) {
this.myArg = _myArg;
}
}
CategoryCodeSamples CodeBarn