JaVa
   

Using the New Task

As this point, we've written all of the code needed for our Ant task (following is the entire listing). The purpose of this Ant task example is to show how to get the appropriate attributes and subelements within a task, process the information, and do something with it. All of the built-in tasks for Ant work in the same manner. The goal is to have another class like TDSSLauncher that does most all of the work. Ant acts as a global parser and gathers all of the information to be processed by some outside applicaton. For our task, we are able to produce all of the information for a global environment and execute an app using the build script. Not only can the build script compile the code and deploy it, but also Ant can execute the code.

package com.company.ant;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.taskdefs.Java;
import org.apache.tools.ant.types.Environment;
import org.apache.tools.ant.types.FileSet;
import org.apache.tools.ant.types.Path;
import org.apache.tools.ant.types.Reference;
import java.io.*;
import java.util.*;
public class TDSSTask extends org.apache.tools.ant.Task{
 private String TDSSDir = null;
 private boolean displayoutput = false;
 private boolean verbose = true;
 private String outputfile = "";
 private path classpath;
 private ArrayList nodes = new ArrayList();
 public void setTDSSDir(String TDSSDir) {
 this.TDSSDir = TDSSDir;
 }
 public void setVerbose (boolean verbose) {
 this.verbose = verbose ;
 }
 public void setDisplayoutput (boolean displayoutput) {
 this.displayoutput = displayoutput ;
 }
 public void setOutputfile (boolean outputfile) {
 this.outputfile = outputfile ;
 }
 public void setClasspath(Path classpath) {
 this.classpath = classpath;
 }
 public void setClasspathRef(Reference ref) {
 createClasspath().setRefid(ref);
 }
 public void execute() throws BuildException {
 try {
 FileOutputStream fo = null;
 ObjectOutputStream oo = null;
 try {
 fo = new FileOutputStream(TDSSDir + "/nodes.txt"); oo = new ObjectOutputStream(fo);
 oo.writeObject(nodes);
 oo.close();
 fo.close();
 } catch(Exception fileE) {
 } Java javaTask = null;
 javaTask = (Java) getProject().createTask("java");
 javaTask.setTaskName(getTaskName());
 javaTask.setClassname("com.company.app.TDSSLauncher");
 javaTask.setClasspath(classpath);

 // Add command-line arguments
 javaTask.createArg().setValue("-h");
 javaTask.createArg().setFile(TDSSDir);
 if (outputFile != null) {
 javaTask.createArg().setValue("-o");
 javaTask.createArg().setFile(outputFile);
 }
 if (showoutput) {
 javaTask.createArg().setValue("-d");
 }
 if (verbose) {
 javaTask.createArg().setValue("-v");
 }
 javaTask.setFork(true);
 if (javaTask.executeJava() != 0) {
 throw new BuildException("Error from launching BattleRunner");
 }
 } catch (Exception e) {
 throw new BuildException(e)
 }
 }
 private boolean includeNode(Node node) {
 String ifProperty = node.getIf();
 Project p = getProject();
 if (ifProperty != null && p.getProperty(ifProperty) == null) {
 return false;
 }
 return true;
 }
 public void addConfiguredNode(Node n) implements Serializable {
 if (includeNode(n)) {
 nodes.add(robot);
 }
 }
public static class Node implements Serializable {
private int id = 0;
private String username;
private String password;
private int popularity = 0;
private boolean raid = false;
private String ifProperty;
public void setID(int id) {
 this.id = id;
}
public void setUsername(String username) { this.username = username;
}
public void setPassword(String password) {
 this.password = password;
}
public void setPopularity(int popularity) {
 this.popularity = popularity;
}
public void setRaid(boolean raid) {
 this.raid = raid;
}
public void setIf(String ifProperty) {
 this.ifProperty = ifProperty;
}
public int getID() {
 return id;
}
public String getUsername() {
 return username;
}
public String getPassword() {
 return password;
}
public int getPopularity() {
 return popularity;
}
public boolean getRaid() {
 return raid;
}
public String getIf() {
 return ifProperty;
}
 }
 }


JaVa
   
Comments