您的位置:首页 > 运维架构 > Apache

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

2007-01-11 16:33 239 查看
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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: