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 18 KiB

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