Methods Summary |
---|
public void | execute()Execute the task.
run(command);
|
protected void | logFlush()Close output.
if (fos != null) {
fos.close();
}
|
protected void | outputLog(java.lang.String line, int messageLevel)Log an output message.
if (fos == null) {
log(line, messageLevel);
} else {
fos.println(line);
}
|
protected int | run(java.lang.String command)Execute the command.
int err = -1; // assume the worst
// test if os match
String myos = System.getProperty("os.name");
log("Myos = " + myos, Project.MSG_VERBOSE);
if ((os != null) && (os.indexOf(myos) < 0)) {
// this command will be executed only on the specified OS
log("Not found in " + os, Project.MSG_VERBOSE);
return 0;
}
// default directory to the project's base directory
if (dir == null) {
dir = getProject().getBaseDir();
}
if (myos.toLowerCase().indexOf("windows") >= 0) {
if (!dir.equals(getProject().resolveFile("."))) {
if (myos.toLowerCase().indexOf("nt") >= 0) {
command = "cmd /c cd " + dir + " && " + command;
} else {
String ant = getProject().getProperty(MagicNames.ANT_HOME);
if (ant == null) {
throw new BuildException("Property '" + MagicNames.ANT_HOME + "' not "
+ "found", getLocation());
}
String antRun = getProject().resolveFile(ant + "/bin/antRun.bat").toString();
command = antRun + " " + dir + " " + command;
}
}
} else {
String ant = getProject().getProperty(MagicNames.ANT_HOME);
if (ant == null) {
throw new BuildException("Property '" + MagicNames.ANT_HOME + "' not found",
getLocation());
}
String antRun = getProject().resolveFile(ant + "/bin/antRun").toString();
command = antRun + " " + dir + " " + command;
}
try {
// show the command
log(command, Project.MSG_VERBOSE);
// exec command on system runtime
Process proc = Runtime.getRuntime().exec(command);
if (out != null) {
fos = new PrintWriter(new FileWriter(out));
log("Output redirected to " + out, Project.MSG_VERBOSE);
}
// copy input and error to the output stream
StreamPumper inputPumper =
new StreamPumper(proc.getInputStream(), Project.MSG_INFO);
StreamPumper errorPumper =
new StreamPumper(proc.getErrorStream(), Project.MSG_WARN);
// starts pumping away the generated output/error
inputPumper.start();
errorPumper.start();
// Wait for everything to finish
proc.waitFor();
inputPumper.join();
errorPumper.join();
proc.destroy();
// close the output file if required
logFlush();
// check its exit value
err = proc.exitValue();
if (err != 0) {
if (failOnError) {
throw new BuildException("Exec returned: " + err, getLocation());
} else {
log("Result: " + err, Project.MSG_ERR);
}
}
} catch (IOException ioe) {
throw new BuildException("Error exec: " + command, ioe, getLocation());
} catch (InterruptedException ex) {
//ignore
}
return err;
|
public void | setCommand(java.lang.String command)Set the command to exec.
this.command = command;
|
public void | setDir(java.lang.String d)Set the directory.
this.dir = getProject().resolveFile(d);
|
public void | setFailonerror(boolean fail)Set the failOnError attribute.
Default is false.
failOnError = fail;
|
public void | setOs(java.lang.String os)Set the Operating System that this exec is to run in.
this.os = os;
|
public void | setOutput(java.lang.String out)Set the output filename.
this.out = out;
|