[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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值