JavaRanch Home    
 
This page:         last edited 13 July 2008         What's Changed?         Edit

Ant Task   

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;

    // called to initialize the task
    public void init() throws BuildException {
        proj = getProject();
        target = getOwningTarget();
        proj.log("MyTask init");
    }

    // called to execute the task
    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);
    }

    // called to set task parameters; in this case "myArg"
    public void setMyArg (String _myArg) {
        this.myArg = _myArg;
    }
}


CategoryCodeSamples CodeBarn

JavaRanchAbout us — Copyright © 1998-2010 Paul Wheaton