org.apache.tools.ant.taskdefs

Class ExecTask

Known Direct Subclasses:
ExecuteOn

public class ExecTask
extends Task

Executes a given command if the os platform is appropriate.

Since:
Ant 1.2

Field Summary

protected Commandline
cmdl
protected boolean
failOnError
protected boolean
newEnvironment
protected Redirector
redirector
protected RedirectorElement
redirectorElement

Fields inherited from class org.apache.tools.ant.Task

description, location, target, taskName, taskType, wrapper

Fields inherited from class org.apache.tools.ant.ProjectComponent

project

Method Summary

void
addConfiguredRedirector(RedirectorElement redirectorElement)
Add a RedirectorElement to this task.
void
addEnv(Environment.Variable var)
Add an environment variable to the launched process.
protected void
checkConfiguration()
Has the user set all necessary attributes?
Commandline.Argument
createArg()
Adds a command-line argument.
protected ExecuteStreamHandler
createHandler()
Create the StreamHandler to use with our Execute instance.
protected ExecuteWatchdog
createWatchdog()
Create the Watchdog to kill a runaway process.
void
execute()
Do the work.
boolean
getResolveExecutable()
Indicates whether to attempt to resolve the executable to a file
protected boolean
isValidOs()
Is this the OS the user wanted?
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
protected Execute
prepareExec()
Create an Execute instance with the correct working directory set.
protected String
resolveExecutable(String exec, boolean searchPath)
The method attempts to figure out where the executable is so that we can feed the full path.
protected void
runExec(Execute exe)
Run the command using the given Execute instance.
protected void
runExecute(Execute exe)
A Utility method for this classes and subclasses to run an Execute instance (an external command).
void
setAppend(boolean append)
Sets whether output should be appended to or overwrite an existing file.
void
setCommand(Commandline cmdl)
Sets a command line
void
setDir(File d)
Set the working directory of the process.
void
setError(File error)
File the error stream of the process is redirected to.
void
setErrorProperty(String errorProperty)
Sets the name of the property whose value should be set to the error of the process.
void
setExecutable(String value)
Set the name of the executable program.
void
setFailIfExecutionFails(boolean flag)
Sets a flag to stop the build if program cannot be started.
void
setFailonerror(boolean fail)
Fail if the command exits with a non-zero return code.
void
setInput(File input)
Set the input to use for the task
void
setInputString(String inputString)
Set the string to use as input
void
setLogError(boolean logError)
Controls whether error output of exec is logged.
void
setNewenvironment(boolean newenv)
Do not propagate old environment when new environment variables are specified.
void
setOs(String os)
List of operating systems on which the command may be executed.
void
setOutput(File out)
File the output of the process is redirected to.
void
setOutputproperty(String outputProp)
Sets the property name whose value should be set to the output of the process.
void
setResolveExecutable(boolean resolveExecutable)
Sets a flag indicating whether to attempt to resolve the executable to a file
void
setResultProperty(String resultProperty)
Sets the name of a property in which the return code of the command should be stored.
void
setSpawn(boolean spawn)
set whether or not you want the process to be spawned default is not spawned
void
setTimeout(Integer value)
Timeout in milliseconds after which the process will be killed.
void
setTimeout(Long value)
Timeout in milliseconds after which the process will be killed.
void
setVMLauncher(boolean vmLauncher)
Sets a flag indicating if we want to launch new process with VM, otherwise use the OS's shell.
protected void
setupRedirector()
Set up properties on the redirector that we needed to store locally.

Methods inherited from class org.apache.tools.ant.Task

execute, getDescription, getLocation, getOwningTarget, getRuntimeConfigurableWrapper, getTaskName, getTaskType, getWrapper, handleErrorFlush, handleErrorOutput, handleFlush, handleInput, handleOutput, init, isInvalid, log, log, maybeConfigure, perform, reconfigure, setDescription, setLocation, setOwningTarget, setRuntimeConfigurableWrapper, setTaskName, setTaskType

Methods inherited from class org.apache.tools.ant.ProjectComponent

getProject, log, log, setProject

Field Details

cmdl

protected Commandline cmdl


failOnError

protected boolean failOnError


newEnvironment

protected boolean newEnvironment


redirector

protected Redirector redirector


redirectorElement

protected RedirectorElement redirectorElement

Method Details

addConfiguredRedirector

public void addConfiguredRedirector(RedirectorElement redirectorElement)
Add a RedirectorElement to this task.

Parameters:
redirectorElement - RedirectorElement.


addEnv

public void addEnv(Environment.Variable var)
Add an environment variable to the launched process.

Parameters:
var - new environment variable


checkConfiguration

protected void checkConfiguration()
            throws BuildException
Has the user set all necessary attributes?

Throws:
BuildException - if there are missing required parameters


createArg

public Commandline.Argument createArg()
Adds a command-line argument.

Returns:
new command line argument created


createHandler

protected ExecuteStreamHandler createHandler()
            throws BuildException
Create the StreamHandler to use with our Execute instance.

Returns:
instance of ExecuteStreamHandler

Throws:
BuildException - under unknown circumstances


createWatchdog

protected ExecuteWatchdog createWatchdog()
            throws BuildException
Create the Watchdog to kill a runaway process.

Returns:
instance of ExecuteWatchdog

Throws:
BuildException - under unknown circumstances


execute

public void execute()
            throws BuildException
Do the work.
Overrides:
execute in interface Task

Throws:
BuildException - in a number of circumstances :
  • if failIfExecFails is set to true and the process cannot be started
  • the java13command launcher can send build exceptions
  • this list is not exhaustive or limitative


getResolveExecutable

public boolean getResolveExecutable()
Indicates whether to attempt to resolve the executable to a file

Since:
Ant 1.6


isValidOs

protected boolean isValidOs()
Is this the OS the user wanted?

Returns:
boolean
  • true if the os under which ant is running is matches one os in the os attribute or if the os attribute is null
  • false otherwise.


logFlush

protected void logFlush()
Flush the output stream - if there is one.


maybeSetResultPropertyValue

protected void maybeSetResultPropertyValue(int result)
helper method to set result property to the passed in value if appropriate

Parameters:
result - value desired for the result property value


prepareExec

protected Execute prepareExec()
            throws BuildException
Create an Execute instance with the correct working directory set.

Returns:
an instance of the Execute class

Throws:
BuildException - under unknown circumstances.


resolveExecutable

protected String resolveExecutable(String exec,
                                   boolean searchPath)
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 ther path).

Parameters:
exec - the name of the executable
searchPath - if true, the excutable will be looked up in the PATH environment and the absolute path is returned.

Returns:
the executable as a full path if it can be determined.

Since:
Ant 1.6


runExec

protected void runExec(Execute exe)
            throws BuildException
Run the command using the given Execute instance. This may be overridden by subclasses

Parameters:
exe - instance of Execute to run

Throws:
BuildException - if the new process could not be started only if failIfExecFails is set to true (the default)


runExecute

protected final void runExecute(Execute exe)
            throws IOException
A Utility method for this classes and subclasses to run an Execute instance (an external command).

Parameters:
exe - instance of the execute class


setAppend

public void setAppend(boolean append)
Sets whether output should be appended to or overwrite an existing file. Defaults to false.

Parameters:
append - if true append is desired

Since:
1.30, Ant 1.5


setCommand

public void setCommand(Commandline cmdl)
Sets a command line

Parameters:
cmdl - command line


setDir

public void setDir(File d)
Set the working directory of the process.

Parameters:
d - the working directory of the process


setError

public void setError(File error)
File the error stream of the process is redirected to.

Parameters:
error - a file to which send stderr to

Since:
ant 1.6


setErrorProperty

public void setErrorProperty(String errorProperty)
Sets the name of the property whose value should be set to the error of the process.

Parameters:
errorProperty - name of property

Since:
ant 1.6


setExecutable

public void setExecutable(String value)
Set the name of the executable program.

Parameters:
value - the name of the executable program


setFailIfExecutionFails

public void setFailIfExecutionFails(boolean flag)
Sets a flag to stop the build if program cannot be started. Defaults to true.

Parameters:
flag - stop the build if program cannot be started

Since:
Ant 1.5


setFailonerror

public void setFailonerror(boolean fail)
Fail if the command exits with a non-zero return code.

Parameters:
fail - if true fail the command on non-zero return code.


setInput

public void setInput(File input)
Set the input to use for the task

Parameters:
input - name of a file to get input from


setInputString

public void setInputString(String inputString)
Set the string to use as input

Parameters:
inputString - the string which is used as the input source


setLogError

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

Parameters:
logError - set to true to log error output in the normal ant log


setNewenvironment

public void setNewenvironment(boolean newenv)
Do not propagate old environment when new environment variables are specified.

Parameters:
newenv - if true, do not propagate old environment when new environment variables are specified.


setOs

public void setOs(String os)
List of operating systems on which the command may be executed.

Parameters:
os - list of operating systems on which the command may be executed


setOutput

public void setOutput(File out)
File the output of the process is redirected to. If error is not redirected, it too will appear in the output

Parameters:
out - name of a file to which send output to


setOutputproperty

public void setOutputproperty(String outputProp)
Sets the property name whose value should be set to the output of the process.

Parameters:
outputProp - name of property


setResolveExecutable

public void setResolveExecutable(boolean resolveExecutable)
Sets a flag indicating whether to attempt to resolve the executable to a file

Parameters:
resolveExecutable - if true, attempt to resolve the path of the executable


setResultProperty

public void setResultProperty(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.

Parameters:
resultProperty - name of property

Since:
Ant 1.5


setSpawn

public void setSpawn(boolean spawn)
set whether or not you want the process to be spawned default is not spawned

Parameters:
spawn - if true you do not want ant to wait for the end of the process

Since:
ant 1.6


setTimeout

public void setTimeout(Integer value)
Timeout in milliseconds after which the process will be killed.

Parameters:
value - timeout in milliseconds


setTimeout

public void setTimeout(Long value)
Timeout in milliseconds after which the process will be killed.

Parameters:
value - timeout in milliseconds

Since:
Ant 1.5


setVMLauncher

public void setVMLauncher(boolean vmLauncher)
Sets a flag indicating if we want to launch new process with VM, otherwise use the OS's shell. Default value of the flag is true.

Parameters:
vmLauncher - true if we want to launch new process with VM, false if we want to use the OS's shell.


setupRedirector

protected void setupRedirector()
Set up properties on the redirector that we needed to store locally.


Copyright B) 2000-2005 Apache Software Foundation. All Rights Reserved.