编写自定义任务,轻松扩展Ant (2) 代码

类别:Java 点击:0 评论:0 推荐:

结束:全部代码:

 

(问什么不能带附件?)

 

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 李尚强 [email protected]

 */

 

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;

}

 

}

本文地址:http://com.8s8s.com/it/it16599.htm