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

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