source: other-projects/trunk/gs3-release-maker/apache-ant-1.6.5/docs/manual/CoreTasks/exec.html@ 14627

Last change on this file since 14627 was 14627, checked in by oranfry, 17 years ago

initial import of the gs3-release-maker

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