Automating Batch Tasks with Ant
by Hugo Troche


Listing One

<project name="example" basedir="." default="deploy-example">
     <path id="classpath">
        <fileset dir="/home/youdir/deps"/>
    </path>    
    <target name="compile-example" >
        <javac srcdir="/home/yourdir/java" 
                       destdir="/home/yourdir/dest" classpathref="classpath">
        </javac>
    </target>

    <target name="jar-example">
        <jar basedir="/home/yourdir/dest" 
                         destfile="/home/yourdir/lib/example.jar"/>
    </target>
    <target name="deploy-example" depends="compile-example,jar-example"/>
</project>


Listing Two

package example;
import org.apache.tools.ant.Task;
import java.io.*;

public class ParserTask extends Task {
    private File sourceFile = null; //This is the file with personnel records
    public ParserTask() {
    }
    public void setSourceFile(File file) { //To use attributes in an Ant task
        this.sourceFile = file;       //all you have to do is create a setter.
    }               //The attribute for this task will be sourceFile.
                    //Note how ant will parse the String in the attribute to
                    //a java.io.File type automatically.
    public void execute() { //Method that gets called when task is executed
        try{
            PersonPaser parser = new PersonParser();
            parser.setFile(sourceFile);
            parser.parseFile();
        } catch (Exception e) {
            super.log("Operation failed, error message bellow");
                                                      //This uses ant logging
            e.printStackTrace();
        }
        super.log("Operation succeeded");
    }
}





2


