You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

exec.html 16 KiB

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