source: release-kits/lirk3/bin/ant-installer/web/manual1.6.2/manual/CoreTasks/exec.html@ 14982

Last change on this file since 14982 was 14982, checked in by oranfry, 16 years ago

initial import of LiRK3

File size: 15.1 KB
Line 
1<html>
2
3<head>
4<meta http-equiv="Content-Language" content="en-us">
5<title>Exec Task</title>
6<link rel="stylesheet" type="text/css" href="../stylesheets/antmanual.css">
7</head>
8
9<body>
10
11<h2><a name="exec">Exec</a></h2>
12<h3>Description</h3>
13<p>Executes a system command. When the <i>os</i> attribute is specified, then
14the command is only executed when Ant is run on one of the specified operating
15systems.</p>
16
17<p>Note that you cannot interact with the forked program, the only way
18to send input to it is via the input and inputstring attributes. Also note that
19in Ant 1.6, any attempt to read input in the forked program will receive an
20EOF (-1). This is a change from Ant 1.5, where such an attempt would block.</p>
21
22<h4>Cygwin Users</h4>
23<p>In general the &lt;exec&gt; task will not understand paths such as /bin/sh for
24the executable parameter. This is because the Java VM in which Ant is running is a
25Windows executable and is not aware of Cygwin conventions.
26</p>
27
28<h4>OpenVMS Users</h4>
29<p>The command specified using <code>executable</code> and
30<code>&lt;arg&gt;</code> elements is executed exactly as specified
31inside a temporary DCL script. This has some implications:
32<ul>
33<li>paths have to be written in VMS style</li>
34<li>if your <code>executable</code> points to a DCL script remember to
35prefix it with an <code>@</code>-sign
36(e.g. <code>executable="@[FOO]BAR.COM"</code>), just as you would in a
37DCL script</li>
38</ul>
39For <code>&lt;exec&gt;</code> to work in an environment with a Java VM
40older than version 1.4.1-2 it is also <i>required</i> that the logical
41<code>JAVA$FORK_SUPPORT_CHDIR</code> is set to <code>TRUE</code> in
42the job table (see the <i>JDK Release Notes</i>).</p>
43
44<p>Please note that the Java VM provided by HP doesn't follow OpenVMS'
45conventions of exit codes. If you run a Java VM with this task, the
46task may falsely claim that an error occured (or silently ignore an
47error). Don't use this task to run <code>JAVA.EXE</code>, use a
48<code>&lt;java&gt;</code> task with the <code>fork</code> attribute
49set to <code>true</code> instead as this task will follow the VM's
50interpretation of exit codes.</p>
51
52<h4>RedHat S/390 Users</h4>
53
54<p>It has been <a
55href="http://listserv.uark.edu/scripts/wa.exe?A1=ind0404&L=vmesa-l#33">reported
56on the VMESA-LISTSERV</a> that shell scripts invoked via the Ant Exec
57task must have their interpreter specified, i.e., the scripts must
58start with something like:
59
60<blockquote>
61<pre>
62#!/bin/bash
63</pre>
64</blockquote>
65
66or the task will fail as follows:
67
68<blockquote>
69<pre>
70[exec] Warning: UNIXProcess.forkAndExec native error: Exec format error
71[exec] Result: 255
72</pre>
73</blockquote>
74</p>
75
76<h3>Parameters</h3>
77<table border="1" cellpadding="2" cellspacing="0">
78 <tr>
79 <td valign="top"><b>Attribute</b></td>
80 <td valign="top"><b>Description</b></td>
81 <td align="center" valign="top"><b>Required</b></td>
82 </tr>
83 <tr>
84 <td valign="top">command</td>
85 <td valign="top">the command to execute with all command line
86 arguments. <b>deprecated, use executable and nested
87 <code>&lt;arg&gt;</code> elements instead</b>.</td>
88 <td align="center" rowspan="2">Exactly one of the two.</td>
89 </tr>
90 <tr>
91 <td valign="top">executable</td>
92 <td valign="top">the command to execute without any command line
93 arguments.</td>
94 </tr>
95 <tr>
96 <td valign="top">dir</td>
97 <td valign="top">the directory in which the command should be executed.</td>
98 <td align="center" valign="top">No</td>
99 </tr>
100 <tr>
101 <td valign="top">os</td>
102 <td valign="top">list of Operating Systems on which the command may be
103 executed. If the current OS's name is contained in this list, the command will
104 be executed. The OS's name is determined by the Java Virtual machine and is set
105 in the &quot;os.name&quot; system property.</td>
106 <td align="center" valign="top">No</td>
107 </tr>
108 <tr>
109 <td valign="top">spawn</td>
110 <td valign="top">whether or not you want the command to be spawned<br/>
111 Default is false.<br>
112 If you spawn a command, its output will not be logged by ant.<br/>
113 The input, output, error, and result property settings are not active when spawning a process.<br>
114 <em>since Ant 1.6</em>
115 </td>
116 <td align="center" valign="top">No</td>
117 </tr>
118 <tr>
119 <td valign="top">output</td>
120 <td valign="top">Name of a file to which to write the output. If the error stream
121 is not also redirected to a file or property, it will appear in this output.</td>
122 <td align="center" valign="top">No</td>
123 </tr>
124 <tr>
125 <td valign="top">error</td>
126 <td valign="top">The file to which the standard error of the
127 command should be redirected. <em>since Ant 1.6</em></td>
128 <td align="center" valign="top">No</td>
129 </tr>
130 <tr>
131 <td valign="top">logError</td>
132 <td valign="top">This attribute is used when you wish to see error output in Ant's
133 log and you are redirecting output to a file/property. The error
134 output will not be included in the output file/property. If you
135 redirect error with the &quot;error&quot; or &quot;errorProperty&quot;
136 attributes, this will have no effect. <em>since Ant 1.6</em></td>
137 <td align="center" valign="top">No</td>
138 </tr>
139 <tr>
140 <td valign="top">append</td>
141 <td valign="top">Whether output and error files should be appended to or overwritten.
142 Defaults to false.</td>
143 <td align="center" valign="top">No</td>
144 </tr>
145 <tr>
146 <td valign="top">outputproperty</td>
147 <td valign="top">The name of a property in which the output of the
148 command should be stored. Unless the error stream is redirected to a separate
149 file or stream, this property will include the error output.</td>
150 <td align="center" valign="top">No</td>
151 </tr>
152 <tr>
153 <td valign="top">errorproperty</td>
154 <td valign="top">The name of a property in which the standard error of the
155 command should be stored. <em>since Ant 1.6</em></td>
156 <td align="center" valign="top">No</td>
157 </tr>
158 <tr>
159 <td valign="top">input</td>
160 <td valign="top">A file from which the executed command's standard input
161 is taken. This attribute is mutually exclusive with the
162 inputstring attribute. <em>since Ant 1.6</em></td>
163 <td align="center" valign="top">No</td>
164 </tr>
165 <tr>
166 <td valign="top">inputstring</td>
167 <td valign="top">A string which serves as the input stream for the
168 executed command. This attribute is mutually exclusive with the
169 input attribute. <em>since Ant 1.6</em></td>
170 <td align="center" valign="top">No</td>
171 </tr>
172 <tr>
173 <td valign="top">resultproperty</td>
174 <td valign="top">the name of a property in which the return code of the
175 command should be stored. Only of interest if failonerror=false.</td>
176 <td align="center" valign="top">No</td>
177 </tr>
178 <tr>
179 <td valign="top">timeout</td>
180 <td valign="top">Stop the command if it doesn't finish within the
181 specified time (given in milliseconds).</td>
182 <td align="center" valign="top">No</td>
183 </tr>
184 <tr>
185 <td valign="top">failonerror</td>
186 <td valign="top">Stop the buildprocess if the command exits with a
187 return code signaling failure. Defaults to false.</td>
188 <td align="center" valign="top">No</td>
189 </tr>
190 <tr>
191 <td valign="top">failifexecutionfails</td>
192 <td valign="top">Stop the build if we can't start the program.
193 Defaults to true. </td>
194 <td align="center" valign="top">No</td>
195 </tr> <tr>
196 <td valign="top">newenvironment</td>
197 <td valign="top">Do not propagate old environment when new environment
198 variables are specified.</td>
199 <td align="center" valign="top">No, default is <i>false</i></td>
200 </tr>
201 <tr>
202 <td valign="top">vmlauncher</td>
203 <td valign="top">Run command using the Java VM's execution facilities
204 where available. If set to false the underlying OS's shell,
205 either directly or through the antRun scripts, will be used.
206 Under some operating systems, this gives access to facilities
207 not normally available through the VM including, under Windows,
208 being able to execute scripts, rather than their associated
209 interpreter. If you want to specify the name of the
210 executable as a relative path to the directory given by the
211 dir attribute, it may become necessary to set vmlauncher to
212 false as well.</td>
213 <td align="center" valign="top">No, default is <i>true</i></td>
214 </tr>
215 <tr>
216 <td valign="top">resolveExecutable</td>
217 <td valign="top">When this attribute is true, the name of the executable
218 if resolved firstly against the project basedir and
219 if that does not exist, against the execution
220 directory if specified. On Unix systems, if you only
221 want to allow execution of commands in the user's path,
222 set this to false. <em>since Ant 1.6</em></td>
223 <td align="center" valign="top">No, default is <i>false</i></td>
224 </tr>
225</table>
226<h3>Examples</h3>
227<blockquote>
228<pre>
229&lt;exec dir=&quot;${src}&quot; executable=&quot;cmd.exe&quot; os=&quot;Windows 2000&quot; output=&quot;dir.txt&quot;&gt;
230 &lt;arg line=&quot;/c dir&quot;/&gt;
231&lt;/exec&gt;</pre>
232</blockquote>
233<h3>Parameters specified as nested elements</h3>
234<h4>arg</h4>
235<p>Command line arguments should be specified as nested
236<code>&lt;arg&gt;</code> elements. See <a
237href="../using.html#arg">Command line arguments</a>.</p>
238<a name="env"></a><h4>env</h4>
239<p>It is possible to specify environment variables to pass to the
240system command via nested <code>&lt;env&gt;</code> elements.</p>
241<table border="1" cellpadding="2" cellspacing="0">
242 <tr>
243 <td valign="top"><b>Attribute</b></td>
244 <td valign="top"><b>Description</b></td>
245 <td align="center" valign="top"><b>Required</b></td>
246 </tr>
247 <tr>
248 <td valign="top">key</td>
249 <td valign="top">The name of the environment variable.</td>
250 <td align="center" valign="top">Yes</td>
251 </tr>
252 <tr>
253 <td valign="top">value</td>
254 <td valign="top">The literal value for the environment variable.</td>
255 <td align="center" rowspan="3">Exactly one of these.</td>
256 </tr>
257 <tr>
258 <td valign="top">path</td>
259 <td valign="top">The value for a PATH like environment
260 variable. You can use ; or : as path separators and Ant will
261 convert it to the platform's local conventions.</td>
262 </tr>
263 <tr>
264 <td valign="top">file</td>
265 <td valign="top">The value for the environment variable. Will be
266 replaced by the absolute filename of the file by Ant.</td>
267 </tr>
268</table>
269<a name="redirector"></a><h4>redirector</h4>
270<i><b>Since Ant 1.6.2</b></i>
271<p>A nested <a href="../CoreTypes/redirector.html">I/O Redirector</a>
272can be specified. In general, the attributes of the redirector behave
273as the corresponding attributes available at the task level. The most
274notable peculiarity stems from the retention of the &lt;exec&gt;
275attributes for backwards compatibility. Any file mapping is done
276using a <CODE>null</CODE> sourcefile; therefore not all
277<a href="../CoreTypes/mapper.html">Mapper</a> types will return
278results. When no results are returned, redirection specifications
279will fall back to the task level attributes. In practice this means that
280defaults can be specified for input, output, and error output files.
281</p>
282<h3>Errors and return codes</h3>
283By default the return code of a &lt;exec&gt; is ignored; when you set
284<code>failonerror="true"</code> then any return code signaling failure
285(OS specific) causes the build to fail. Alternatively, you can set
286<code>resultproperty</code> to the name of a property and have it assigned to
287the result code (barring immutability, of course).
288<p>
289If the attempt to start the program fails with an OS dependent error code,
290then &lt;exec&gt; halts the build unless <code>failifexecutionfails</code>
291is set to <code>false</code>. You can use that to run a program if it exists, but
292otherwise do nothing.
293<p>
294What do those error codes mean? Well, they are OS dependent. On Windows
295boxes you have to look in include\error.h in your windows compiler or wine files;
296error code 2 means 'no such program', which usually means it is not on the path.
297Any time you see such an error from any ant task, it is usually not an ant bug,
298but some configuration problem on your machine.
299
300<h3>Examples</h3>
301<blockquote><pre>
302&lt;exec executable=&quot;emacs&quot;&gt;
303 &lt;env key=&quot;DISPLAY&quot; value=&quot;:1.0&quot;/&gt;
304&lt;/exec&gt;
305</pre></blockquote>
306<p>starts <code>emacs</code> on display 1 of the X Window System.</p>
307
308<blockquote><pre>
309&lt;exec ... &gt;
310 &lt;env key=&quot;PATH&quot; path=&quot;${java.library.path}:${basedir}/bin&quot;/&gt;
311&lt;/exec&gt;
312</pre></blockquote>
313<p>adds <code>${basedir}/bin</code> to the <code>PATH</code> of the
314system command.</p>
315
316<blockquote><pre>
317&lt;property name="browser" location="C:/Programme/Internet Explorer/iexplore.exe"/&gt;
318&lt;property name="file" location="ant/docs/manual/index.html"/&gt;
319
320&lt;exec executable="${browser}" spawn="true"&gt;
321 &lt;arg value="${file}"/&gt;
322&lt;/exec&gt;
323</pre></blockquote>
324<p>Starts the <i>${browser}</i> with the specified <i>${file}</i> and end the
325ant process. The browser will let be open.</p>
326
327<blockquote><pre>
328&lt;exec executable=&quot;cat&quot;&gt;
329 &lt;redirector outputproperty=&quot;redirector.out&quot;
330 errorproperty=&quot;redirector.err&quot;
331 inputstring=&quot;blah before blah&quot;&gt;
332 &lt;inputfilterchain&gt;
333 &lt;replacestring from=&quot;before&quot; to=&quot;after&quot; /&gt;
334 &lt;/inputfilterchain&gt;
335 &lt;outputmapper type=&quot;merge&quot; to=&quot;redirector.out&quot; /&gt;
336 &lt;errormapper type=&quot;merge&quot; to=&quot;redirector.err&quot; /&gt;
337 &lt;/redirector&gt;
338&lt;/exec&gt;
339</pre></blockquote>
340
341Sends the string &quot;blah before blah&quot; to the &quot;cat&quot; executable,
342using an <a href="../CoreTypes/filterchain.html">&lt;inputfilterchain&gt;</a>
343to replace &quot;before&quot; with &quot;after&quot; on the way in.
344Output is sent to the file &quot;redirector.out&quot; and stored
345in a property of the same name. Similarly, error output is sent to
346a file and a property, both named &quot;redirector.err&quot;.
347
348
349<p><b>Note:</b> Although it may work for you to specify arguments using
350a simple arg-element and separate them by spaces it may fail if you switch to
351a newer version of the JDK. JDK &lt; 1.2 will pass these as separate arguments
352to the program you are calling, JDK &gt;= 1.2 will pass them as a single
353argument and cause most calls to fail.</p>
354<p><b>Note2:</b> If you are using Ant on Windows and a new DOS-Window pops up
355for every command which is executed this may be a problem of the JDK you are using.
356This problem may occur with all JDK's &lt; 1.2.</p>
357<p>
358<b>Timeouts: </b> If a timeout is specified, when it is reached the
359sub process is killed and a message printed to the log. The return
360value of the execution will be "-1", which will halt the build if
361<tt>failonerror=true</tt>, but be ignored otherwise.
362
363<hr>
364<p align="center">Copyright &copy; 2000-2004 The Apache Software Foundation. All rights
365Reserved.</p>
366
367</body>
368</html>
369
Note: See TracBrowser for help on using the repository browser.