[Apache --- Ant]DIY Ant Task I --- The Simplest one

build.xml:

<project name="AntTest" default="main" basedir=".">

 <property name="classpath" value="${basedir}/bin"/>
 
 <target name="init"/>

 <target name="simpletask" depends="init">
  <taskdef name="simpletask" classname="org.xxx.anttest.SimpleTask" classpath="${classpath}"/>
  <simpletask />
 </target>

 <target name="main" depends="simpletask" />

</project>
 

Source:

package org.xxx.anttest;

public class SimpleTask {
   
    public void execute(){
        System.out.println("This is a DIY ant task!!");
    }

}

Task lifecycle
There are different stages in the processing of a build file, and the objects that implement tasks are used throughout the stages.

Here is the lifecycle of Ant tasks. The build begins with Ant loading and parsing the
build file.
      1 As Ant parses the build file, it creates an instance of the appropriate subclass of
Task for every declaration of a task in the file, using its empty constructor.
      2 Ant then informs the task about its containing project, target, and some other
minor details, such as which line of the build file contains it.
      3 Ant calls the init()method of the Task class. Most tasks do not override this.
      4 Ant proceeds to execute the targets in the order it determines is appropriate,
conceivably not executing all of them, depending upon whether conditional
targets have their conditions met.
      5 The tasks inside a target are executed one by one, For each task, Ant configures
it with the attribute and element values in the build file, then calls its execute()
method
This does not quite explain how a class that does not extend org.apache.tools.
ant.Task works. The answer is that there is a TaskAdapter in Ant’s API that does
extend from Task, and contains an instance of the Object, and invokes its execute
method. The TaskAdapter is used internally to Ant for tasks that do not
extend from Task.

你可能感兴趣的:([Apache --- Ant]DIY Ant Task I --- The Simplest one)