[Apache --- Ant]DIY Ant Task II --- a little more difficult

build.xml:

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

 <property name="classpath" value="${basedir}/bin"/>
 <property environment="env" />
 <echo message="Number of processors = ${env.NUMBER_OF_PROCESSORS}" />
 <echo message="ANT_HOME = ${env.ANT_HOME}"/>
 <echo message="ITS_HOME = ${env.ITS_HOME}" />
 
 <target name="init">
    <tstamp>
        <format property="TODAY" pattern="yyyy-MM-dd hh:mm aa" />
    </tstamp>
 <taskdef name="simpletask" classname="org.xxx.anttest.SimpleTask" classpath="${classpath}" />
 <taskdef name="proctask" classname="org.xxx.anttest.FileProcTask" classpath="${classpath}"/>

 </target>

 <target name="simpletask" depends="init" >
  <simpletask path="${env.ANT_HOME}:build/output" />
 </target>
 
 <target name="proctask" depends="init">
  <echo message="Current Time is: ${TODAY}"></echo>
  <proctask  dir="${basedir}">
   <include name="**/*.java"/>
  </proctask>
 </target>

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

</project>

 

Source:

package org.xxx.anttest;

import java.io.File;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.MatchingTask;

public class FileProcTask extends MatchingTask {
    private File dir;
    public void setDir (File dir) {
    this.dir = dir;
    }
    public void execute() throws BuildException {
    if (dir == null) {
    throw new BuildException("dir must be specified");
    }
    log("dir = " + dir, Project.MSG_DEBUG);
    DirectoryScanner ds = getDirectoryScanner(dir);
    String[] files = ds.getIncludedFiles();
    for (int i = 0; i < files.length; i++) {
    log("file: " + files[i]);
    }
    dir = null;
    }

}

你可能感兴趣的:([Apache --- Ant]DIY Ant Task II --- a little more difficult)