blob: d257c85dd8c4d5db433fb6eedbe33c6ca150118a [file] [log] [blame]
<html>
<head>
<meta http-equiv="Content-Language" content="en-us">
<title>Exec Task</title>
<link rel="stylesheet" type="text/css" href="../stylesheets/antmanual.css">
</head>
<body>
<h2><a name="exec">Exec</a></h2>
<h3>Description</h3>
<p>Executes a system command. When the <i>os</i> attribute is specified, then
the command is only executed when Ant is run on one of the specified operating
systems.</p>
<p>Note that you cannot interact with the forked program, the only way
to send input to it is via the input and inputstring attributes.</p>
<h4>Cygwin Users</h4>
<p>In general the &lt;exec&gt; 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.
</p>
<h4>OpenVMS Users</h4>
<p>The command specified using <code>executable</code> and
<code>&lt;arg&gt;</code> elements is executed exactly as specified
inside a temporary DCL script. This has some implications:
<ul>
<li>paths have to be written in VMS style</li>
<li>if your <code>executable</code> points to a DCL script remember to
prefix it with an <code>@</code>-sign
(e.g. <code>executable="@[FOO]BAR.COM"</code>), just as you would in a
DCL script</li>
</ul>
For <code>&lt;exec&gt;</code> to work in an environment with a Java VM
older than version 1.4.1-2 it is also <i>required</i> that the logical
<code>JAVA$FORK_SUPPORT_CHDIR</code> is set to <code>TRUE</code> in
the job table (see the <i>JDK Release Notes</i>).</p>
<p>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 <code>JAVA.EXE</code>, use a
<code>&lt;java&gt;</code> task with the <code>fork</code> attribute
set to <code>true</code> instead as this task will follow the VM's
interpretation of exit codes.</p>
<h3>Parameters</h3>
<table border="1" cellpadding="2" cellspacing="0">
<tr>
<td valign="top"><b>Attribute</b></td>
<td valign="top"><b>Description</b></td>
<td align="center" valign="top"><b>Required</b></td>
</tr>
<tr>
<td valign="top">command</td>
<td valign="top">the command to execute with all command line
arguments. <b>deprecated, use executable and nested
<code>&lt;arg&gt;</code> elements instead</b>.</td>
<td align="center" rowspan="2">Exactly one of the two.</td>
</tr>
<tr>
<td valign="top">executable</td>
<td valign="top">the command to execute without any command line
arguments.</td>
</tr>
<tr>
<td valign="top">dir</td>
<td valign="top">the directory in which the command should be executed.</td>
<td align="center" valign="top">No</td>
</tr>
<tr>
<td valign="top">os</td>
<td valign="top">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 &quot;os.name&quot; system property.</td>
<td align="center" valign="top">No</td>
</tr>
<tr>
<td valign="top">spawn</td>
<td valign="top">whether or not you want the command to be spawned<br/>
Default is false.<br>
If you spawn a command, its output will not be logged by ant.<br/>
The input, output, error, and result property settings are not active when spawning a process.<br>
<em>since Ant 1.6</em>
</td>
<td align="center" valign="top">No</td>
</tr>
<tr>
<td valign="top">output</td>
<td valign="top">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.</td>
<td align="center" valign="top">No</td>
</tr>
<tr>
<td valign="top">error</td>
<td valign="top">The file to which the standard error of the
command should be redirected. <em>since Ant 1.6</em></td>
<td align="center" valign="top">No</td>
</tr>
<tr>
<td valign="top">logError</td>
<td valign="top">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 &quot;error&quot; or &quot;errorProperty&quot;
attributes, this will have no effect. <em>since Ant 1.6</em></td>
<td align="center" valign="top">No</td>
</tr>
<tr>
<td valign="top">append</td>
<td valign="top">Whether output and error files should be appended to or overwritten.
Defaults to false.</td>
<td align="center" valign="top">No</td>
</tr>
<tr>
<td valign="top">outputproperty</td>
<td valign="top">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.</td>
<td align="center" valign="top">No</td>
</tr>
<tr>
<td valign="top">errorproperty</td>
<td valign="top">The name of a property in which the standard error of the
command should be stored. <em>since Ant 1.6</em></td>
<td align="center" valign="top">No</td>
</tr>
<tr>
<td valign="top">input</td>
<td valign="top">A file from which the executed command's standard input
is taken. This attribute is mutually exclusive with the
inputstring attribute. <em>since Ant 1.6</em></td>
<td align="center" valign="top">No</td>
</tr>
<tr>
<td valign="top">inputstring</td>
<td valign="top">A string which serves as the input stream for the
executed command. This attribute is mutually exclusive with the
input attribute. <em>since Ant 1.6</em></td>
<td align="center" valign="top">No</td>
</tr>
<tr>
<td valign="top">resultproperty</td>
<td valign="top">the name of a property in which the return code of the
command should be stored. Only of interest if failonerror=false.</td>
<td align="center" valign="top">No</td>
</tr>
<tr>
<td valign="top">timeout</td>
<td valign="top">Stop the command if it doesn't finish within the
specified time (given in milliseconds).</td>
<td align="center" valign="top">No</td>
</tr>
<tr>
<td valign="top">failonerror</td>
<td valign="top">Stop the buildprocess if the command exits with a
return code signaling failure. Defaults to false.</td>
<td align="center" valign="top">No</td>
</tr>
<tr>
<td valign="top">failifexecutionfails</td>
<td valign="top">Stop the build if we can't start the program.
Defaults to true. </td>
<td align="center" valign="top">No</td>
</tr> <tr>
<td valign="top">newenvironment</td>
<td valign="top">Do not propagate old environment when new environment
variables are specified.</td>
<td align="center" valign="top">No, default is <i>false</i></td>
</tr>
<tr>
<td valign="top">vmlauncher</td>
<td valign="top">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.</td>
<td align="center" valign="top">No, default is <i>true</i></td>
</tr>
<tr>
<td valign="top">resolveExecutable</td>
<td valign="top">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. <em>since Ant 1.6</em></td>
<td align="center" valign="top">No, default is <i>false</i></td>
</tr>
</table>
<h3>Examples</h3>
<blockquote>
<pre>
&lt;exec dir=&quot;${src}&quot; executable=&quot;cmd.exe&quot; os=&quot;Windows 2000&quot; output=&quot;dir.txt&quot;&gt;
&lt;arg line=&quot;/c dir&quot;/&gt;
&lt;/exec&gt;</pre>
</blockquote>
<h3>Parameters specified as nested elements</h3>
<h4>arg</h4>
<p>Command line arguments should be specified as nested
<code>&lt;arg&gt;</code> elements. See <a
href="../using.html#arg">Command line arguments</a>.</p>
<h4><a name="env">env</a></h4>
<p>It is possible to specify environment variables to pass to the
system command via nested <code>&lt;env&gt;</code> elements.</p>
<table border="1" cellpadding="2" cellspacing="0">
<tr>
<td valign="top"><b>Attribute</b></td>
<td valign="top"><b>Description</b></td>
<td align="center" valign="top"><b>Required</b></td>
</tr>
<tr>
<td valign="top">key</td>
<td valign="top">The name of the environment variable.</td>
<td align="center" valign="top">Yes</td>
</tr>
<tr>
<td valign="top">value</td>
<td valign="top">The literal value for the environment variable.</td>
<td align="center" rowspan="3">Exactly one of these.</td>
</tr>
<tr>
<td valign="top">path</td>
<td valign="top">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.</td>
</tr>
<tr>
<td valign="top">file</td>
<td valign="top">The value for the environment variable. Will be
replaced by the absolute filename of the file by Ant.</td>
</tr>
</table>
<h3>Errors and return codes</h3>
By default the return code of a &lt;exec&gt; is ignored; when you set
<code>failonerror="true"</code> then any return code signaling failure
(OS specific) causes the build to fail. Alternatively, you can set
<code>resultproperty</code> to the name of a property and have it assigned to
the result code (barring immutability, of course).
<p>
If the attempt to start the program fails with an OS dependent error code,
then &lt;exec&gt; halts the build unless <code>failifexecutionfails</code>
is set to <code>false</code>. You can use that to run a program if it exists, but
otherwise do nothing.
<p>
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.
<h3>Examples</h3>
<blockquote><pre>
&lt;exec executable=&quot;emacs&quot;&gt;
&lt;env key=&quot;DISPLAY&quot; value=&quot;:1.0&quot;/&gt;
&lt;/exec&gt;
</pre></blockquote>
<p>starts <code>emacs</code> on display 1 of the X Window System.</p>
<blockquote><pre>
&lt;exec ... &gt;
&lt;env key=&quot;PATH&quot; path=&quot;${java.library.path}:${basedir}/bin&quot;/&gt;
&lt;/exec&gt;
</pre></blockquote>
<p>adds <code>${basedir}/bin</code> to the <code>PATH</code> of the
system command.</p>
<blockquote><pre>
&lt;property name="browser" location="C:/Programme/Internet Explorer/iexplore.exe"/&gt;
&lt;property name="file" location="ant/docs/manual/index.html"/&gt;
&lt;exec executable="${browser}" spawn="true"&gt;
&lt;arg value="${file}"/&gt;
&lt;/exec&gt;
</pre></blockquote>
<p>Starts the <i>${browser}</i> with the specified <i>${file}</i> and end the
ant process. The browser will let be open.</p>
<p><b>Note:</b> 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 &lt; 1.2 will pass these as separate arguments
to the program you are calling, JDK &gt;= 1.2 will pass them as a single
argument and cause most calls to fail.</p>
<p><b>Note2:</b> 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 &lt; 1.2.</p>
<p>
<b>Timeouts: </b> 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
<tt>failonerror=true</tt>, but be ignored otherwise.
<hr>
<p align="center">Copyright &copy; 2000-2003 Apache Software Foundation. All rights
Reserved.</p>
</body>
</html>