Methods Summary |
---|
public void | addConfiguredRedirector(org.apache.tools.ant.types.RedirectorElement redirectorElement)Add a RedirectorElement to this task.
if (this.redirectorElement != null) {
throw new BuildException("cannot have > 1 nested <redirector>s");
}
this.redirectorElement = redirectorElement;
incompatibleWithSpawn = true;
|
public void | addEnv(Environment.Variable var)Add an environment variable to the launched process.
env.addVariable(var);
|
protected void | checkConfiguration()Has the user set all necessary attributes?
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();
|
public Commandline.Argument | createArg()Adds a command-line argument.
return cmdl.createArgument();
|
protected ExecuteStreamHandler | createHandler()Create the StreamHandler to use with our Execute instance.
return redirector.createHandler();
|
protected ExecuteWatchdog | createWatchdog()Create the Watchdog to kill a runaway process.
return (timeout == null)
? null : new ExecuteWatchdog(timeout.longValue());
|
public void | execute()Do the work.
// 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()Indicates whether to attempt to resolve the executable to a
file.
return resolveExecutable;
|
private boolean | isPath(java.lang.String line)
return line.startsWith("PATH=") || line.startsWith("Path=");
|
protected boolean | isValidOs()Is this the OS the user wanted?
//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;
|
protected void | logFlush()Flush the output stream - if there is one.
|
protected void | maybeSetResultPropertyValue(int result)Helper method to set result property to the
passed in value if appropriate.
if (resultProperty != null) {
String res = Integer.toString(result);
getProject().setNewProperty(resultProperty, res);
}
|
protected Execute | prepareExec()Create an Execute instance with the correct working directory set.
// 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;
|
protected java.lang.String | resolveExecutable(java.lang.String exec, boolean mustSearchPath)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).
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(), environment[i].substring(5));
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(), line.substring(5));
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;
|
protected void | runExec(Execute exe)Run the command using the given Execute instance. This may be
overridden by subclasses.
// 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();
}
|
protected final void | runExecute(Execute exe)A Utility method for this classes and subclasses to run an
Execute instance (an external command).
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();
}
|
public void | setAppend(boolean append)Set whether output should be appended to or overwrite an existing file.
Defaults to false.
redirector.setAppend(append);
incompatibleWithSpawn = true;
|
public void | setCommand(org.apache.tools.ant.types.Commandline cmdl)Sets a command line.
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(java.io.File d)Set the working directory of the process.
this.dir = d;
|
public void | setError(java.io.File error)Set the File to which the error stream of the process should be redirected.
this.error = error;
incompatibleWithSpawn = true;
|
public void | setErrorProperty(java.lang.String errorProperty)Sets the name of the property whose value should be set to the error of
the process.
redirector.setErrorProperty(errorProperty);
incompatibleWithSpawn = true;
|
public void | setExecutable(java.lang.String value)Set the name of the executable program.
this.executable = value;
cmdl.setExecutable(value);
|
public void | setFailIfExecutionFails(boolean flag)Set whether to stop the build if program cannot be started.
Defaults to true.
failIfExecFails = flag;
incompatibleWithSpawn = true;
|
public void | setFailonerror(boolean fail)Fail if the command exits with a non-zero return code.
failOnError = fail;
incompatibleWithSpawn |= fail;
|
public void | setInput(java.io.File input)Set the input file to use for the task.
if (inputString != null) {
throw new BuildException("The \"input\" and \"inputstring\" "
+ "attributes cannot both be specified");
}
this.input = input;
incompatibleWithSpawn = true;
|
public void | setInputString(java.lang.String inputString)Set the string to use as input.
if (input != null) {
throw new BuildException("The \"input\" and \"inputstring\" "
+ "attributes cannot both be specified");
}
this.inputString = inputString;
incompatibleWithSpawn = true;
|
public void | setLogError(boolean 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.
redirector.setLogError(logError);
incompatibleWithSpawn |= logError;
|
public void | setNewenvironment(boolean newenv)Do not propagate old environment when new environment variables are specified.
newEnvironment = newenv;
|
public void | setOs(java.lang.String os)List of operating systems on which the command may be executed.
this.os = os;
|
public void | setOsFamily(java.lang.String osFamily)Restrict this execution to a single OS Family
this.osFamily = osFamily.toLowerCase(Locale.US);
|
public void | setOutput(java.io.File out)File the output of the process is redirected to. If error is not
redirected, it too will appear in the output.
this.output = out;
incompatibleWithSpawn = true;
|
public void | setOutputproperty(java.lang.String outputProp)Sets the property name whose value should be set to the output of
the process.
redirector.setOutputProperty(outputProp);
incompatibleWithSpawn = true;
|
public void | setResolveExecutable(boolean resolveExecutable)Set whether to attempt to resolve the executable to a file.
this.resolveExecutable = resolveExecutable;
|
public void | setResultProperty(java.lang.String resultProperty)Sets the name of a property in which the return code of the
command should be stored. Only of interest if failonerror=false.
this.resultProperty = resultProperty;
incompatibleWithSpawn = true;
|
public void | setSearchPath(boolean searchPath)Set whether to search nested, then
system PATH environment variables for the executable.
this.searchPath = searchPath;
|
public void | setSpawn(boolean spawn)Set whether or not you want the process to be spawned.
Default is false.
this.spawn = spawn;
|
public void | setTimeout(java.lang.Long value)Set the timeout in milliseconds after which the process will be killed.
timeout = value;
incompatibleWithSpawn = true;
|
public void | setTimeout(java.lang.Integer value)Set the timeout in milliseconds after which the process will be killed.
setTimeout(
(Long) ((value == null) ? null : new Long(value.intValue())));
|
public void | setVMLauncher(boolean vmLauncher)Set whether to launch new process with VM, otherwise use the OS's shell.
Default value is true.
this.vmLauncher = vmLauncher;
|
protected void | setupRedirector()Set up properties on the redirector that we needed to store locally.
redirector.setInput(input);
redirector.setInputString(inputString);
redirector.setOutput(output);
redirector.setError(error);
|