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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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>Cygwin Users</h4>
  17. <p>In general the &lt;exec&gt; task will not understand paths such as /bin/sh for
  18. the executable parameter. This is because the Java VM in which Ant is running is a
  19. Windows executable and is not aware of Cygwin conventions.
  20. </p>
  21. <h4>OpenVMS Users</h4>
  22. <p>The command specified using <code>executable</code> and
  23. <code>&lt;arg&gt;</code> elements is executed exactly as specified
  24. inside a temporary DCL script. This has some implications:
  25. <ul>
  26. <li>paths have to be written in VMS style</li>
  27. <li>if your <code>executable</code> points to a DCL script remember to
  28. prefix it with an <code>@</code>-sign
  29. (e.g. <code>executable="@[FOO]BAR.COM"</code>), just as you would in a
  30. DCL script</li>
  31. </ul>
  32. For <code>&lt;exec&gt;</code> to work in an environment with a Java VM
  33. older than version 1.4.1-2 it is also <i>required</i> that the logical
  34. <code>JAVA$FORK_SUPPORT_CHDIR</code> is set to <code>TRUE</code> in
  35. the job table (see the <i>JDK Release Notes</i>).</p>
  36. <p>Please note that the Java VM provided by HP doesn't follow OpenVMS'
  37. conventions of exit codes. If you run a Java VM with this task, the
  38. task may falsely claim that an error occured (or silently ignore an
  39. error). Don't use this task to run <code>JAVA.EXE</code>, use a
  40. <code>&lt;java&gt;</code> task with the <code>fork</code> attribute
  41. set to <code>true</code> instead as this task will follow the VM's
  42. interpretation of exit codes.</p>
  43. <h3>Parameters</h3>
  44. <table border="1" cellpadding="2" cellspacing="0">
  45. <tr>
  46. <td valign="top"><b>Attribute</b></td>
  47. <td valign="top"><b>Description</b></td>
  48. <td align="center" valign="top"><b>Required</b></td>
  49. </tr>
  50. <tr>
  51. <td valign="top">command</td>
  52. <td valign="top">the command to execute with all command line
  53. arguments. <b>deprecated, use executable and nested
  54. <code>&lt;arg&gt;</code> elements instead</b>.</td>
  55. <td align="center" rowspan="2">Exactly one of the two.</td>
  56. </tr>
  57. <tr>
  58. <td valign="top">executable</td>
  59. <td valign="top">the command to execute without any command line
  60. arguments.</td>
  61. </tr>
  62. <tr>
  63. <td valign="top">dir</td>
  64. <td valign="top">the directory in which the command should be executed.</td>
  65. <td align="center" valign="top">No</td>
  66. </tr>
  67. <tr>
  68. <td valign="top">os</td>
  69. <td valign="top">list of Operating Systems on which the command may be
  70. executed. If the current OS's name is contained in this list, the command will
  71. be executed. The OS's name is determined by the Java Virtual machine and is set
  72. in the &quot;os.name&quot; system property.</td>
  73. <td align="center" valign="top">No</td>
  74. </tr>
  75. <tr>
  76. <td valign="top">spawn</td>
  77. <td valign="top">whether or not you want the command to be spawned<br/>
  78. Default is false.<br>
  79. If you spawn a command, its output will not be logged by ant.<br/>
  80. The input, output, error, and result property settings are not active when spawning a process.<br>
  81. <em>since Ant 1.6</em>
  82. </td>
  83. <td align="center" valign="top">No</td>
  84. </tr>
  85. <tr>
  86. <td valign="top">output</td>
  87. <td valign="top">Name of a file to which to write the output. If the error stream
  88. is not also redirected to a file or property, it will appear in this output.</td>
  89. <td align="center" valign="top">No</td>
  90. </tr>
  91. <tr>
  92. <td valign="top">error</td>
  93. <td valign="top">The file to which the standard error of the
  94. command should be redirected. <em>since Ant 1.6</em></td>
  95. <td align="center" valign="top">No</td>
  96. </tr>
  97. <tr>
  98. <td valign="top">logError</td>
  99. <td valign="top">This attribute is used when you wish to see error output in Ant's
  100. log and you are redirecting output to a file/property. The error
  101. output will not be included in the output file/property. If you
  102. redirect error with the &quot;error&quot; or &quot;errorProperty&quot;
  103. attributes, this will have no effect. <em>since Ant 1.6</em></td>
  104. <td align="center" valign="top">No</td>
  105. </tr>
  106. <tr>
  107. <td valign="top">append</td>
  108. <td valign="top">Whether output and error files should be appended to or overwritten.
  109. Defaults to false.</td>
  110. <td align="center" valign="top">No</td>
  111. </tr>
  112. <tr>
  113. <td valign="top">outputproperty</td>
  114. <td valign="top">The name of a property in which the output of the
  115. command should be stored. Unless the error stream is redirected to a separate
  116. file or stream, this property will include the error output.</td>
  117. <td align="center" valign="top">No</td>
  118. </tr>
  119. <tr>
  120. <td valign="top">errorproperty</td>
  121. <td valign="top">The name of a property in which the standard error of the
  122. command should be stored. <em>since Ant 1.6</em></td>
  123. <td align="center" valign="top">No</td>
  124. </tr>
  125. <tr>
  126. <td valign="top">input</td>
  127. <td valign="top">A file from which the executed command's standard input
  128. is taken. This attribute is mutually exclusive with the
  129. inputstring attribute. <em>since Ant 1.6</em></td>
  130. <td align="center" valign="top">No</td>
  131. </tr>
  132. <tr>
  133. <td valign="top">inputstring</td>
  134. <td valign="top">A string which serves as the input stream for the
  135. executed command. This attribute is mutually exclusive with the
  136. input attribute. <em>since Ant 1.6</em></td>
  137. <td align="center" valign="top">No</td>
  138. </tr>
  139. <tr>
  140. <td valign="top">resultproperty</td>
  141. <td valign="top">the name of a property in which the return code of the
  142. command should be stored. Only of interest if failonerror=false.</td>
  143. <td align="center" valign="top">No</td>
  144. </tr>
  145. <tr>
  146. <td valign="top">timeout</td>
  147. <td valign="top">Stop the command if it doesn't finish within the
  148. specified time (given in milliseconds).</td>
  149. <td align="center" valign="top">No</td>
  150. </tr>
  151. <tr>
  152. <td valign="top">failonerror</td>
  153. <td valign="top">Stop the buildprocess if the command exits with a
  154. return code signaling failure. Defaults to false.</td>
  155. <td align="center" valign="top">No</td>
  156. </tr>
  157. <tr>
  158. <td valign="top">failifexecutionfails</td>
  159. <td valign="top">Stop the build if we can't start the program.
  160. Defaults to true. </td>
  161. <td align="center" valign="top">No</td>
  162. </tr> <tr>
  163. <td valign="top">newenvironment</td>
  164. <td valign="top">Do not propagate old environment when new environment
  165. variables are specified.</td>
  166. <td align="center" valign="top">No, default is <i>false</i></td>
  167. </tr>
  168. <tr>
  169. <td valign="top">vmlauncher</td>
  170. <td valign="top">Run command using the Java VM's execution facilities
  171. where available. If set to false the underlying OS's shell,
  172. either directly or through the antRun scripts, will be used.
  173. Under some operating systems, this gives access to facilities
  174. not normally available through the VM including, under Windows,
  175. being able to execute scripts, rather than their associated
  176. interpreter. If you want to specify the name of the
  177. executable as a relative path to the directory given by the
  178. dir attribute, it may become necessary to set vmlauncher to
  179. false as well.</td>
  180. <td align="center" valign="top">No, default is <i>true</i></td>
  181. </tr>
  182. <tr>
  183. <td valign="top">resolveExecutable</td>
  184. <td valign="top">When this attribute is true, the name of the executable
  185. if resolved firstly against the project basedir and
  186. if that does not exist, against the execution
  187. directory if specified. On Unix systems, if you only
  188. want to allow execution of commands in the user's path,
  189. set this to false. <em>since Ant 1.6</em></td>
  190. <td align="center" valign="top">No, default is <i>false</i></td>
  191. </tr>
  192. </table>
  193. <h3>Examples</h3>
  194. <blockquote>
  195. <pre>
  196. &lt;exec dir=&quot;${src}&quot; executable=&quot;cmd.exe&quot; os=&quot;Windows 2000&quot; output=&quot;dir.txt&quot;&gt;
  197. &lt;arg line=&quot;/c dir&quot;/&gt;
  198. &lt;/exec&gt;</pre>
  199. </blockquote>
  200. <h3>Parameters specified as nested elements</h3>
  201. <h4>arg</h4>
  202. <p>Command line arguments should be specified as nested
  203. <code>&lt;arg&gt;</code> elements. See <a
  204. href="../using.html#arg">Command line arguments</a>.</p>
  205. <h4><a name="env">env</a></h4>
  206. <p>It is possible to specify environment variables to pass to the
  207. system command via nested <code>&lt;env&gt;</code> elements.</p>
  208. <table border="1" cellpadding="2" cellspacing="0">
  209. <tr>
  210. <td valign="top"><b>Attribute</b></td>
  211. <td valign="top"><b>Description</b></td>
  212. <td align="center" valign="top"><b>Required</b></td>
  213. </tr>
  214. <tr>
  215. <td valign="top">key</td>
  216. <td valign="top">The name of the environment variable.</td>
  217. <td align="center" valign="top">Yes</td>
  218. </tr>
  219. <tr>
  220. <td valign="top">value</td>
  221. <td valign="top">The literal value for the environment variable.</td>
  222. <td align="center" rowspan="3">Exactly one of these.</td>
  223. </tr>
  224. <tr>
  225. <td valign="top">path</td>
  226. <td valign="top">The value for a PATH like environment
  227. variable. You can use ; or : as path separators and Ant will
  228. convert it to the platform's local conventions.</td>
  229. </tr>
  230. <tr>
  231. <td valign="top">file</td>
  232. <td valign="top">The value for the environment variable. Will be
  233. replaced by the absolute filename of the file by Ant.</td>
  234. </tr>
  235. </table>
  236. <h3>Errors and return codes</h3>
  237. By default the return code of a &lt;exec&gt; is ignored; when you set
  238. <code>failonerror="true"</code> then any return code signaling failure
  239. (OS specific) causes the build to fail. Alternatively, you can set
  240. <code>resultproperty</code> to the name of a property and have it assigned to
  241. the result code (barring immutability, of course).
  242. <p>
  243. If the attempt to start the program fails with an OS dependent error code,
  244. then &lt;exec&gt; halts the build unless <code>failifexecutionfails</code>
  245. is set to <code>false</code>. You can use that to run a program if it exists, but
  246. otherwise do nothing.
  247. <p>
  248. What do those error codes mean? Well, they are OS dependent. On Windows
  249. boxes you have to look in include\error.h in your windows compiler or wine files;
  250. error code 2 means 'no such program', which usually means it is not on the path.
  251. Any time you see such an error from any ant task, it is usually not an ant bug,
  252. but some configuration problem on your machine.
  253. <h3>Examples</h3>
  254. <blockquote><pre>
  255. &lt;exec executable=&quot;emacs&quot;&gt;
  256. &lt;env key=&quot;DISPLAY&quot; value=&quot;:1.0&quot;/&gt;
  257. &lt;/exec&gt;
  258. </pre></blockquote>
  259. <p>starts <code>emacs</code> on display 1 of the X Window System.</p>
  260. <blockquote><pre>
  261. &lt;exec ... &gt;
  262. &lt;env key=&quot;PATH&quot; path=&quot;${java.library.path}:${basedir}/bin&quot;/&gt;
  263. &lt;/exec&gt;
  264. </pre></blockquote>
  265. <p>adds <code>${basedir}/bin</code> to the <code>PATH</code> of the
  266. system command.</p>
  267. <blockquote><pre>
  268. &lt;property name="browser" location="C:/Programme/Internet Explorer/iexplore.exe"/&gt;
  269. &lt;property name="file" location="ant/docs/manual/index.html"/&gt;
  270. &lt;exec executable="${browser}" spawn="true"&gt;
  271. &lt;arg value="${file}"/&gt;
  272. &lt;/exec&gt;
  273. </pre></blockquote>
  274. <p>Starts the <i>${browser}</i> with the specified <i>${file}</i> and end the
  275. ant process. The browser will let be open.</p>
  276. <p><b>Note:</b> Although it may work for you to specify arguments using
  277. a simple arg-element and separate them by spaces it may fail if you switch to
  278. a newer version of the JDK. JDK &lt; 1.2 will pass these as separate arguments
  279. to the program you are calling, JDK &gt;= 1.2 will pass them as a single
  280. argument and cause most calls to fail.</p>
  281. <p><b>Note2:</b> If you are using Ant on Windows and a new DOS-Window pops up
  282. for every command which is executed this may be a problem of the JDK you are using.
  283. This problem may occur with all JDK's &lt; 1.2.</p>
  284. <p>
  285. <b>Timeouts: </b> If a timeout is specified, when it is reached the
  286. sub process is killed and a message printed to the log. The return
  287. value of the execution will be "-1", which will halt the build if
  288. <tt>failonerror=true</tt>, but be ignored otherwise.
  289. <hr>
  290. <p align="center">Copyright &copy; 2000-2003 Apache Software Foundation. All rights
  291. Reserved.</p>
  292. </body>
  293. </html>