| Method from org.apache.tools.ant.taskdefs.ExecTask Detail: |
public void addConfiguredRedirector(RedirectorElement redirectorElement) {
if (this.redirectorElement != null) {
throw new BuildException("cannot have > 1 nested < redirector >s");
}
this.redirectorElement = redirectorElement;
incompatibleWithSpawn = true;
}
Add a RedirectorElement to this task. |
public void addEnv(Environment.Variable var) {
env.addVariable(var);
}
Add an environment variable to the launched process. |
protected void checkConfiguration() throws BuildException {
if (cmdl.getExecutable() == null) {
throw new BuildException("no executable specified", getLocation());
}
if (dir != null && !dir.exists()) {
throw new BuildException("The directory " + dir + " does not exist");
}
if (dir != null && !dir.isDirectory()) {
throw new BuildException(dir + " is not a directory");
}
if (spawn && incompatibleWithSpawn) {
getProject().log("spawn does not allow attributes related to input, "
+ "output, error, result", Project.MSG_ERR);
getProject().log("spawn also does not allow timeout", Project.MSG_ERR);
getProject().log("finally, spawn is not compatible "
+ "with a nested I/O < redirector >", Project.MSG_ERR);
throw new BuildException("You have used an attribute "
+ "or nested element which is not compatible with spawn");
}
setupRedirector();
}
Has the user set all necessary attributes? |
public Commandline.Argument createArg() {
return cmdl.createArgument();
}
Adds a command-line argument. |
protected ExecuteStreamHandler createHandler() throws BuildException {
return redirector.createHandler();
}
Create the StreamHandler to use with our Execute instance. |
protected ExecuteWatchdog createWatchdog() throws BuildException {
return (timeout == null)
? null : new ExecuteWatchdog(timeout.longValue());
}
Create the Watchdog to kill a runaway process. |
public void execute() throws BuildException {
// Quick fail if this is not a valid OS for the command
if (!isValidOs()) {
return;
}
File savedDir = dir; // possibly altered in prepareExec
cmdl.setExecutable(resolveExecutable(executable, searchPath));
checkConfiguration();
try {
runExec(prepareExec());
} finally {
dir = savedDir;
}
}
|
public boolean getResolveExecutable() {
return resolveExecutable;
}
Indicates whether to attempt to resolve the executable to a
file. |
protected boolean isValidOs() {
//hand osfamily off to Os class, if set
if (osFamily != null && !Os.isOs(osFamily, null, null, null)) {
return false;
}
//the Exec OS check is different from Os.isOs(), which
//probes for a specific OS. Instead it searches the os field
//for the current os.name
String myos = System.getProperty("os.name");
log("Current OS is " + myos, Project.MSG_VERBOSE);
if ((os != null) && (os.indexOf(myos) < 0)) {
// this command will be executed only on the specified OS
log("This OS, " + myos
+ " was not found in the specified list of valid OSes: " + os,
Project.MSG_VERBOSE);
return false;
}
return true;
}
Is this the OS the user wanted? |
protected void logFlush() {
}
Flush the output stream - if there is one. |
protected void maybeSetResultPropertyValue(int result) {
if (resultProperty != null) {
String res = Integer.toString(result);
getProject().setNewProperty(resultProperty, res);
}
}
Helper method to set result property to the
passed in value if appropriate. |
protected Execute prepareExec() throws BuildException {
// default directory to the project's base directory
if (dir == null) {
dir = getProject().getBaseDir();
}
if (redirectorElement != null) {
redirectorElement.configure(redirector);
}
Execute exe = new Execute(createHandler(), createWatchdog());
exe.setAntRun(getProject());
exe.setWorkingDirectory(dir);
exe.setVMLauncher(vmLauncher);
exe.setSpawn(spawn);
String[] environment = env.getVariables();
if (environment != null) {
for (int i = 0; i < environment.length; i++) {
log("Setting environment variable: " + environment[i],
Project.MSG_VERBOSE);
}
}
exe.setNewenvironment(newEnvironment);
exe.setEnvironment(environment);
return exe;
}
Create an Execute instance with the correct working directory set. |
protected String resolveExecutable(String exec,
boolean mustSearchPath) {
if (!resolveExecutable) {
return exec;
}
// try to find the executable
File executableFile = getProject().resolveFile(exec);
if (executableFile.exists()) {
return executableFile.getAbsolutePath();
}
// now try to resolve against the dir if given
if (dir != null) {
executableFile = FILE_UTILS.resolveFile(dir, exec);
if (executableFile.exists()) {
return executableFile.getAbsolutePath();
}
}
// couldn't find it - must be on path
if (mustSearchPath) {
Path p = null;
String[] environment = env.getVariables();
if (environment != null) {
for (int i = 0; i < environment.length; i++) {
if (isPath(environment[i])) {
p = new Path(getProject(), getPath(environment[i]));
break;
}
}
}
if (p == null) {
Vector envVars = Execute.getProcEnvironment();
Enumeration e = envVars.elements();
while (e.hasMoreElements()) {
String line = (String) e.nextElement();
if (isPath(line)) {
p = new Path(getProject(), getPath(line));
break;
}
}
}
if (p != null) {
String[] dirs = p.list();
for (int i = 0; i < dirs.length; i++) {
executableFile
= FILE_UTILS.resolveFile(new File(dirs[i]), exec);
if (executableFile.exists()) {
return executableFile.getAbsolutePath();
}
}
}
}
// mustSearchPath is false, or no PATH or not found - keep our
// fingers crossed.
return exec;
}
The method attempts to figure out where the executable is so that we can feed
the full path. We first try basedir, then the exec dir, and then
fallback to the straight executable name (i.e. on the path). |
protected void runExec(Execute exe) throws BuildException {
// show the command
log(cmdl.describeCommand(), Project.MSG_VERBOSE);
exe.setCommandline(cmdl.getCommandline());
try {
runExecute(exe);
} catch (IOException e) {
if (failIfExecFails) {
throw new BuildException("Execute failed: " + e.toString(), e,
getLocation());
} else {
log("Execute failed: " + e.toString(), Project.MSG_ERR);
}
} finally {
// close the output file if required
logFlush();
}
}
Run the command using the given Execute instance. This may be
overridden by subclasses. |
protected final void runExecute(Execute exe) throws IOException {
int returnCode = -1; // assume the worst
if (!spawn) {
returnCode = exe.execute();
//test for and handle a forced process death
if (exe.killedProcess()) {
String msg = "Timeout: killed the sub-process";
if (failOnError) {
throw new BuildException(msg);
} else {
log(msg, Project.MSG_WARN);
}
}
maybeSetResultPropertyValue(returnCode);
redirector.complete();
if (Execute.isFailure(returnCode)) {
if (failOnError) {
throw new BuildException(getTaskType() + " returned: "
+ returnCode, getLocation());
} else {
log("Result: " + returnCode, Project.MSG_ERR);
}
}
} else {
exe.spawn();
}
}
A Utility method for this classes and subclasses to run an
Execute instance (an external command). |
public void setAppend(boolean append) {
redirector.setAppend(append);
incompatibleWithSpawn = true;
}
Set whether output should be appended to or overwrite an existing file.
Defaults to false. |
public void setCommand(Commandline cmdl) {
log("The command attribute is deprecated.\n"
+ "Please use the executable attribute and nested arg elements.",
Project.MSG_WARN);
this.cmdl = cmdl;
}
|
public void setDir(File d) {
this.dir = d;
}
Set the working directory of the process. |
public void setError(File error) {
this.error = error;
incompatibleWithSpawn = true;
}
Set the File to which the error stream of the process should be redirected. |
public void setErrorProperty(String errorProperty) {
redirector.setErrorProperty(errorProperty);
incompatibleWithSpawn = true;
}
Sets the name of the property whose value should be set to the error of
the process. |
public void setExecutable(String value) {
this.executable = value;
cmdl.setExecutable(value);
}
Set the name of the executable program. |
public void setFailIfExecutionFails(boolean flag) {
failIfExecFails = flag;
incompatibleWithSpawn = true;
}
Set whether to stop the build if program cannot be started.
Defaults to true. |
public void setFailonerror(boolean fail) {
failOnError = fail;
incompatibleWithSpawn |= fail;
}
Fail if the command exits with a non-zero return code. |
public void setInput(File input) {
if (inputString != null) {
throw new BuildException("The \"input\" and \"inputstring\" "
+ "attributes cannot both be specified");
}
this.input = input;
incompatibleWithSpawn = true;
}
Set the input file to use for the task. |
public void setInputString(String inputString) {
if (input != null) {
throw new BuildException("The \"input\" and \"inputstring\" "
+ "attributes cannot both be specified");
}
this.inputString = inputString;
incompatibleWithSpawn = true;
}
Set the string to use as input. |
public void setLogError(boolean logError) {
redirector.setLogError(logError);
incompatibleWithSpawn |= logError;
}
Controls whether error output of exec is logged. This is only useful when
output is being redirected and error output is desired in the Ant log. |
public void setNewenvironment(boolean newenv) {
newEnvironment = newenv;
}
Do not propagate old environment when new environment variables are specified. |
public void setOs(String os) {
this.os = os;
}
List of operating systems on which the command may be executed. |
public void setOsFamily(String osFamily) {
this.osFamily = osFamily.toLowerCase(Locale.US);
}
Restrict this execution to a single OS Family |
public void setOutput(File out) {
this.output = out;
incompatibleWithSpawn = true;
}
File the output of the process is redirected to. If error is not
redirected, it too will appear in the output. |
public void setOutputproperty(String outputProp) {
redirector.setOutputProperty(outputProp);
incompatibleWithSpawn = true;
}
Sets the property name whose value should be set to the output of
the process. |
public void setResolveExecutable(boolean resolveExecutable) {
this.resolveExecutable = resolveExecutable;
}
Set whether to attempt to resolve the executable to a file. |
public void setResultProperty(String resultProperty) {
this.resultProperty = resultProperty;
incompatibleWithSpawn = true;
}
Sets the name of a property in which the return code of the
command should be stored. Only of interest if failonerror=false. |
public void setSearchPath(boolean searchPath) {
this.searchPath = searchPath;
}
Set whether to search nested, then
system PATH environment variables for the executable. |
public void setSpawn(boolean spawn) {
this.spawn = spawn;
}
Set whether or not you want the process to be spawned.
Default is false. |
public void setTimeout(Long value) {
timeout = value;
incompatibleWithSpawn = true;
}
Set the timeout in milliseconds after which the process will be killed. |
public void setTimeout(Integer value) {
setTimeout(
(Long) ((value == null) ? null : new Long(value.intValue())));
}
Set the timeout in milliseconds after which the process will be killed. |
public void setVMLauncher(boolean vmLauncher) {
this.vmLauncher = vmLauncher;
}
Set whether to launch new process with VM, otherwise use the OS's shell.
Default value is true. |
protected void setupRedirector() {
redirector.setInput(input);
redirector.setInputString(inputString);
redirector.setOutput(output);
redirector.setError(error);
}
Set up properties on the redirector that we needed to store locally. |