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 11 kB

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