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

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