Ant 自带了大量的任务 (Task) ,在网上也有大量的任务可用,可是如果你面对的是别人根本不会想到的问题,怎么办呢?其实,只要花一点点功夫就可以编写出自己的任务出来。我开始也没想到会这样简单(做嵌套任务碰到一点问题),由此可见 Ant 的结构设计是相当优秀的。
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
假设我们需要显示任务执行各花了多少时间,写一个 TimerTask
起步:覆盖 org.apache.tools.ant.Task 的 execute 方法
import org.apache.tools.ant.Task;
import org.apache.tools.ant.BuildException;
public class TimerTask extends Task {
public void execute() throws BuildException {
System.out.println( "I am a timer" );
}
}
注意,编译时 ant.jar 必须在 Classpath 上。
简单起见,假设 Java 源文件 , 类文件在同一目录下,在此目录再写一个 build.XML 文件:
<?xml version="1.0" ?>
<project name= "testTimer" default= "test" basedir= "." >
<taskdef name= "timer" classname= "TimerTask" classpath= "." />
<target name= "test" >
<timer />
</target>
</project>
打开 Dos 窗口,到此目录,运行 ant
够简单吧!如果类路径比这复杂或者在 jar 文件中,设置 <taskdef> 的 classpath 属性就行了。
继续:设置属性
假设我们想添加一个 action 属性,在 Java 文件中:
private String action;
….
public void execute() throws BuildException {
if (action.equals(“init”)) {
…
}
…
}
public String getAction() {
return action;
}
public void setAction(String string) {
action = string;
}
在 xml 文件中:
<timer action=”init” />
使用 Project 保存中间结果
对于这个任务来讲,必须把时间值保存起来,比较合适的就是 org.apache.tools.ant.Project 了, Project 对象生命期是整个 Build ,并且可以通过 setProperty, getProperty 等方法保存 / 获取值。
public void execute() throws BuildException {
if (getOwningTarget() == null )
return ;
Project proj = getOwningTarget().getProject();
…
// can do smt with proj now
}
晋级:嵌套任务
如果我们想要在 Timer 下面再嵌套子任务(好像没有这个必要 :- ):
<timer action=”init” >
<foo />
</timer>
那我们首先只需要写一个简单的 FooTask ,然后在 build.xml 加上 <foo> 的定义 :
<taskdef name= "timer" classname= "TimerTask" classpath= "." />
<taskdef name= "foo" classname= "FooTask" classpath= "." />
然后给 TimerTask 添加一个方法:
public void addFoo(FooTask foo) {
}
注意: addXXX 的 XXX 由 <taskdef> 中的 name 决定, Ant 会利用 Java Reflection 去找;类型必须是具体的类型 FooTask ,不能是 Task 。
结束:全部代码:
(问什么不能带附件?)
TimerTask.Java :
import java.util.*;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.BuildException;
/**
* TimerTask.java
* <p> Copyright: Copyright (c) 2003 你可以对本程序随意修改,复制,使用,但请保留这里注释声明!!! </p>
* @author 李尚强 blundblade@sina.com
*/
public class TimerTask extends Task {
private List fooList = new ArrayList();
private String action = "" ; //init, print
private static final String TIME_PROPERTY_INTERNAL = "timer.msecs" ;
private static final String TIME_PROPERTY_NAME = "timer.passed" ;
public void execute() throws BuildException {
System.out.println( "I am a timer" );
System.out.println( "but there are so many foos: " + fooList);
if ( this .getOwningTarget() == null )
return ;
Project proj = this .getOwningTarget().getProject();
String strTime = proj.getProperty(TIME_PROPERTY_INTERNAL);
try {
long currMSecs = System.currentTimeMillis();
if (action.equals( "print" )){
if (strTime == null )
proj.setProperty(TIME_PROPERTY_NAME, "Timer not initilized" );
else {
long startMSecs = Long.parseLong(strTime);
long passedSecs = currMSecs - startMSecs;
int minutes = ( int ) ( passedSecs * 1.0 / (1000 * 60 * 60) );
int hours = minutes / 60;
minutes = minutes % 60;
strTime = hours + " hours " + minutes + " minutes" ;
proj.setProperty(TIME_PROPERTY_NAME, strTime);
}
}
else if (action.equals( "init" )) {
proj.setProperty(TIME_PROPERTY_INTERNAL, Long.toString(currMSecs));
}
}
catch (NumberFormatException nfe) {
throw new BuildException(nfe.getMessage());
}
}
public void addFoo(FooTask foo){
fooList.add(foo);
}
/**
* @return
*/
public String getAction() {
return action;
}
/**
* @param string
*/
public void setAction(String string) {
action = string;
}
}
FooTask.Java :
/*
* Created on 2004-4-17
*
*/
import org.apache.tools.ant.Task;
/**
* FooTask.java
* <p> Copyright: Copyright (c) 2003 你可以对本程序随意修改,复制,使用,但请保留这里注释声明!!! </p>
* @author 李尚强 blundblade@sina.com
*/
public class FooTask extends Task {
private String name = "" ;
/**