source: other-projects/trunk/realistic-books/packages/AntInstaller/web/manual1.7.0/manual/CoreTasks/exec.html@ 19253

Last change on this file since 19253 was 19253, checked in by davidb, 15 years ago

Establishing a source code repository for Veronica's Realistic Book's software

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