Exec

Description

Executes a system command. When the os attribute is specified, then the command is only executed when Ant is run on one of the specified operating systems.

Note that you cannot interact with the forked program, the only way to send input to it is via the input and inputstring attributes. Also note that in Ant 1.6, any attempt to read input in the forked program will receive an EOF (-1). This is a change from Ant 1.5, where such an attempt would block.

Cygwin Users

In general the <exec> task will not understand paths such as /bin/sh for the executable parameter. This is because the Java VM in which Ant is running is a Windows executable and is not aware of Cygwin conventions.

OpenVMS Users

The command specified using executable and <arg> elements is executed exactly as specified inside a temporary DCL script. This has some implications:

For <exec> to work in an environment with a Java VM older than version 1.4.1-2 it is also required that the logical JAVA$FORK_SUPPORT_CHDIR is set to TRUE in the job table (see the JDK Release Notes).

Please note that the Java VM provided by HP doesn't follow OpenVMS' conventions of exit codes. If you run a Java VM with this task, the task may falsely claim that an error occured (or silently ignore an error). Don't use this task to run JAVA.EXE, use a <java> task with the fork attribute set to true instead as this task will follow the VM's interpretation of exit codes.

Parameters

Attribute Description Required
command the command to execute with all command line arguments. deprecated, use executable and nested <arg> elements instead. Exactly one of the two.
executable the command to execute without any command line arguments.
dir the directory in which the command should be executed. No
os list of Operating Systems on which the command may be executed. If the current OS's name is contained in this list, the command will be executed. The OS's name is determined by the Java Virtual machine and is set in the "os.name" system property. No
spawn whether or not you want the command to be spawned
Default is false.
If you spawn a command, its output will not be logged by ant.
The input, output, error, and result property settings are not active when spawning a process.
since Ant 1.6
No
output Name of a file to which to write the output. If the error stream is not also redirected to a file or property, it will appear in this output. No
error The file to which the standard error of the command should be redirected. since Ant 1.6 No
logError This attribute is used when you wish to see error output in Ant's log and you are redirecting output to a file/property. The error output will not be included in the output file/property. If you redirect error with the "error" or "errorProperty" attributes, this will have no effect. since Ant 1.6 No
append Whether output and error files should be appended to or overwritten. Defaults to false. No
outputproperty The name of a property in which the output of the command should be stored. Unless the error stream is redirected to a separate file or stream, this property will include the error output. No
errorproperty The name of a property in which the standard error of the command should be stored. since Ant 1.6 No
input A file from which the executed command's standard input is taken. This attribute is mutually exclusive with the inputstring attribute. since Ant 1.6 No
inputstring A string which serves as the input stream for the executed command. This attribute is mutually exclusive with the input attribute. since Ant 1.6 No
resultproperty the name of a property in which the return code of the command should be stored. Only of interest if failonerror=false. No
timeout Stop the command if it doesn't finish within the specified time (given in milliseconds). No
failonerror Stop the buildprocess if the command exits with a return code signaling failure. Defaults to false. No
failifexecutionfails Stop the build if we can't start the program. Defaults to true. No
newenvironment Do not propagate old environment when new environment variables are specified. No, default is false
vmlauncher Run command using the Java VM's execution facilities where available. If set to false the underlying OS's shell, either directly or through the antRun scripts, will be used. Under some operating systems, this gives access to facilities not normally available through the VM including, under Windows, being able to execute scripts, rather than their associated interpreter. If you want to specify the name of the executable as a relative path to the directory given by the dir attribute, it may become necessary to set vmlauncher to false as well. No, default is true
resolveExecutable When this attribute is true, the name of the executable if resolved firstly against the project basedir and if that does not exist, against the execution directory if specified. On Unix systems, if you only want to allow execution of commands in the user's path, set this to false. since Ant 1.6 No, default is false

Examples

<exec dir="${src}" executable="cmd.exe" os="Windows 2000" output="dir.txt">
  <arg line="/c dir"/>
</exec>

Parameters specified as nested elements

arg

Command line arguments should be specified as nested <arg> elements. See Command line arguments.

env

It is possible to specify environment variables to pass to the system command via nested <env> elements.

Attribute Description Required
key The name of the environment variable. Yes
value The literal value for the environment variable. Exactly one of these.
path The value for a PATH like environment variable. You can use ; or : as path separators and Ant will convert it to the platform's local conventions.
file The value for the environment variable. Will be replaced by the absolute filename of the file by Ant.

Errors and return codes

By default the return code of a <exec> is ignored; when you set failonerror="true" then any return code signaling failure (OS specific) causes the build to fail. Alternatively, you can set resultproperty to the name of a property and have it assigned to the result code (barring immutability, of course).

If the attempt to start the program fails with an OS dependent error code, then <exec> halts the build unless failifexecutionfails is set to false. You can use that to run a program if it exists, but otherwise do nothing.

What do those error codes mean? Well, they are OS dependent. On Windows boxes you have to look in include\error.h in your windows compiler or wine files; error code 2 means 'no such program', which usually means it is not on the path. Any time you see such an error from any ant task, it is usually not an ant bug, but some configuration problem on your machine.

Examples

<exec executable="emacs">
  <env key="DISPLAY" value=":1.0"/>
</exec>

starts emacs on display 1 of the X Window System.

<exec ... >
  <env key="PATH" path="${java.library.path}:${basedir}/bin"/>
</exec>

adds ${basedir}/bin to the PATH of the system command.

<property name="browser" location="C:/Programme/Internet Explorer/iexplore.exe"/>
<property name="file" location="ant/docs/manual/index.html"/>

<exec executable="${browser}" spawn="true">
    <arg value="${file}"/>
</exec>

Starts the ${browser} with the specified ${file} and end the ant process. The browser will let be open.

Note: Although it may work for you to specify arguments using a simple arg-element and separate them by spaces it may fail if you switch to a newer version of the JDK. JDK < 1.2 will pass these as separate arguments to the program you are calling, JDK >= 1.2 will pass them as a single argument and cause most calls to fail.

Note2: If you are using Ant on Windows and a new DOS-Window pops up for every command which is executed this may be a problem of the JDK you are using. This problem may occur with all JDK's < 1.2.

Timeouts: If a timeout is specified, when it is reached the sub process is killed and a message printed to the log. The return value of the execution will be "-1", which will halt the build if failonerror=true, but be ignored otherwise.


Copyright © 2000-2004 The Apache Software Foundation. All rights Reserved.