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.

apply.html 19 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. <!DOCTYPE html>
  2. <!--
  3. Licensed to the Apache Software Foundation (ASF) under one or more
  4. contributor license agreements. See the NOTICE file distributed with
  5. this work for additional information regarding copyright ownership.
  6. The ASF licenses this file to You under the Apache License, Version 2.0
  7. (the "License"); you may not use this file except in compliance with
  8. the License. You may obtain a copy of the License at
  9. https://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing, software
  11. distributed under the License is distributed on an "AS IS" BASIS,
  12. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. See the License for the specific language governing permissions and
  14. limitations under the License.
  15. -->
  16. <html lang="en">
  17. <head>
  18. <link rel="stylesheet" type="text/css" href="../stylesheets/style.css">
  19. <title>Apply Task</title>
  20. </head>
  21. <body>
  22. <h2 id="apply">Apply</h2>
  23. <p><em>The name <code>execon</code> is <u>deprecated</u> and only kept for backwards
  24. compatibility.</em></p>
  25. <h3>Description</h3>
  26. <p>Executes a system command. When the <var>os</var> attribute is specified, then the command is
  27. only executed when Apache Ant is run on one of the specified operating systems.</p>
  28. <p>The files and/or directories of a number of <a href="../Types/resources.html#collection">Resource
  29. Collection</a>s &ndash;- including but not restricted
  30. to <a href="../Types/fileset.html">FileSet</a>s, <a href="../Types/dirset.html">DirSet</a>s
  31. (<em>since Ant 1.6</em>) or <a href="../Types/filelist.html">FileList</a>s (<em>since Ant 1.6</em>)
  32. &ndash;- are passed as arguments to the system command.</p>
  33. <p>If you specify a nested <a href="../Types/mapper.html">mapper</a>, the timestamp of each source
  34. file is compared to the timestamp of a target file which is defined by the
  35. nested <code>mapper</code> element and searched for in the given <var>dest</var>, if specified.</p>
  36. <p>At least one <code>fileset</code> or <code>filelist</code> is required, and you must not specify
  37. more than one <code>mapper</code>.</p>
  38. <p>Note that you cannot interact with the forked program, the only way to send input to it is via
  39. the <var>input</var> and <var>inputstring</var> attributes.</p>
  40. <h4 id="background">Running Ant as a background process on Unix(-like) systems</h4>
  41. <p>If you run Ant as a background process (like <kbd>ant &amp;</kbd>) and use
  42. the <code>&lt;apply&gt;</code> task with <var>spawn</var> set to <q>false</q>, you must provide
  43. explicit input to the forked process or Ant will be suspended because it tries to read from the
  44. standard input.</p>
  45. <h3>Parameters</h3>
  46. <table class="attr">
  47. <tr>
  48. <th scope="col">Attribute</th>
  49. <th scope="col">Description</th>
  50. <th scope="col">Required</th>
  51. </tr>
  52. <tr>
  53. <td>executable</td>
  54. <td>the command to execute without any command line arguments.</td>
  55. <td>Yes</td>
  56. </tr>
  57. <tr>
  58. <td>dest</td>
  59. <td>the directory where the command is expected to place target files when it is executed.</td>
  60. <td>No; ignored unless a nested mapper is specified; by default, the target filenames returned
  61. by the mapper will be interpreted as absolute paths</td>
  62. </tr>
  63. <tr>
  64. <td>spawn</td>
  65. <td>whether or not you want the commands to be spawned.<br/> If you spawn a command, its output
  66. will not be logged by Ant.<br/> The input, output, error, and result property settings are not
  67. active when spawning a process.<br/> <em>since Ant 1.6</em></td>
  68. <td>No; default is <q>false</q></td>
  69. </tr>
  70. <tr>
  71. <td>dir</td>
  72. <td>the directory in which the command should be executed.</td>
  73. <td>No; if <var>vmlauncher</var> is <q>true</q>, defaults to the current working directory,
  74. otherwise the project's <var>basedir</var></td>
  75. </tr>
  76. <tr>
  77. <td>relative</td>
  78. <td>whether the filenames should be passed on the command line as relative pathnames (relative
  79. to the base directory of the corresponding fileset/list for source files or
  80. the <var>dest</var> attribute for target files).</td>
  81. <td>No; default is <q>false</q></td>
  82. </tr>
  83. <tr>
  84. <td>forwardslash</td>
  85. <td>whether the file names should be passed with forward slashes even if the operating system
  86. requires other file separator. The option is ignored if the system file separator is a forward
  87. slash.</td>
  88. <td>No; default is <q>false</q></td>
  89. </tr>
  90. <tr>
  91. <td>os</td>
  92. <td>list of Operating Systems on which the command may be executed.</td>
  93. <td>No</td>
  94. </tr>
  95. <tr>
  96. <td>osfamily</td>
  97. <td>OS family as used in the <code>&lt;os&gt;</code> condition.
  98. <em>since Ant 1.7</em></td>
  99. <td>No</td>
  100. </tr>
  101. <tr>
  102. <td>output</td>
  103. <td>the file to which the output of the command should be redirected. If the error stream is
  104. not also redirected to a file or property, it will appear in this output.</td>
  105. <td>No</td>
  106. </tr>
  107. <tr>
  108. <td>error</td>
  109. <td>The file to which the standard error of the command should be redirected. <em>since Ant
  110. 1.6</em></td>
  111. <td>No</td>
  112. </tr>
  113. <tr>
  114. <td>logError</td>
  115. <td>This attribute is used when you wish to see error output in Ant's log and you are
  116. redirecting output to a file/property. The error output will not be included in the output
  117. file/property. If you redirect error with the <var>error</var> or <var>errorProperty</var>
  118. attributes, this will have no effect. <em>since Ant 1.6</em></td>
  119. <td>No</td>
  120. </tr>
  121. <tr>
  122. <td>append</td>
  123. <td>whether output should be appended to or overwrite an existing file. If you
  124. set <var>parallel</var> to <q>false</q>, you will probably want to set this one
  125. to <q>true</q>.</td>
  126. <td>No; default is <q>false</q></td>
  127. </tr>
  128. <tr>
  129. <td>outputproperty</td>
  130. <td>the name of a property in which the output of the command should be stored. Unless the
  131. error stream is redirected to a separate file or stream, this property will include the error
  132. output.</td>
  133. <td>No</td>
  134. </tr>
  135. <tr>
  136. <td>errorproperty</td>
  137. <td>The name of a property in which the standard error of the command should be
  138. stored. <em>since Ant 1.6</em></td>
  139. <td>No</td>
  140. </tr>
  141. <tr>
  142. <td>input</td>
  143. <td>A file from which the executed command's standard input is taken. This attribute is mutually
  144. exclusive with the <var>inputstring</var> attribute. <em>since Ant 1.6</em></td>
  145. <td>No</td>
  146. </tr>
  147. <tr>
  148. <td>inputstring</td>
  149. <td>A string which serves as the input stream for the executed command. This attribute is
  150. mutually exclusive with the <var>input</var> attribute. <em>since Ant 1.6</em></td>
  151. <td>No</td>
  152. </tr>
  153. <tr>
  154. <td>resultproperty</td>
  155. <td>the name of a property in which the return code of the command should be stored. Only of
  156. interest if <var>failonerror</var> is <q>false</q>. If you set <var>parallel</var>
  157. to <q>false</q>, only the result of the first execution will be stored.</td>
  158. <td>No</td>
  159. </tr>
  160. <tr>
  161. <td>timeout</td>
  162. <td>Stop the command if it doesn't finish within the specified time (given in
  163. milliseconds).</td>
  164. <td>No</td>
  165. </tr>
  166. <tr>
  167. <td>failonerror</td>
  168. <td>Stop the build process if the command exits with a return code other than <q>0</q>.</td>
  169. <td>No; defaults to <q>false</q></td>
  170. </tr>
  171. <tr>
  172. <td>failifexecutionfails</td>
  173. <td>Stop the build if we can't start the program.</td>
  174. <td>No; defaults to <q>true</q></td>
  175. </tr>
  176. <tr>
  177. <td>skipemptyfilesets</td>
  178. <td>Don't run the command, if no source files have been found or are newer than their
  179. corresponding target files. Despite its name, this attribute applies to filelists as
  180. well.</td>
  181. <td>No; default is <q>false</q></td>
  182. </tr>
  183. <tr>
  184. <td>parallel</td>
  185. <td>Run the command only once, appending all files as arguments. If <q>false</q>, command will
  186. be executed once for every file.</td>
  187. <td>No; default is <q>false</q></td>
  188. </tr>
  189. <tr>
  190. <td>type</td>
  191. <td>One of <q>file</q>, <q>dir</q> or <q>both</q>. If set to <q>file</q>, only the names of
  192. plain files will be sent to the command. If set to <q>dir</q>, only the names of directories
  193. are considered.<br/>
  194. <strong>Note</strong>: The <var>type</var> attribute does not apply to
  195. nested <code>dirset</code>s&mdash;<code>dirset</code>s always implicitly assume type to
  196. be <q>dir</q>.</td>
  197. <td>No; default is <q>file</q></td>
  198. </tr>
  199. <tr>
  200. <td>newenvironment</td>
  201. <td>Do not propagate old environment when new environment variables are specified.</td>
  202. <td>No; default is <q>false</q></td>
  203. </tr>
  204. <tr>
  205. <td>vmlauncher</td>
  206. <td>Run command using the JVM's execution facilities where available. If set to <q>false</q> the
  207. underlying OS's shell, either directly or through the <kbd>antRun</kbd> scripts, will be
  208. used. Under some operating systems, this gives access to facilities not normally available
  209. through JVM including, under Windows, being able to execute scripts, rather than their
  210. associated interpreter. If you want to specify the name of the executable as a relative path
  211. to the directory given by the <var>dir</var> attribute, it may become necessary to
  212. set <var>vmlauncher</var> to <q>false</q> as well.</td>
  213. <td>No; default is <q>true</q></td>
  214. </tr>
  215. <tr>
  216. <td>resolveExecutable</td>
  217. <td>When this attribute is <q>true</q>, the name of the executable if resolved firstly against
  218. the project <var>basedir</var> and if that does not exist, against the execution directory if
  219. specified. On Unix systems, if you only want to allow execution of commands in the user's
  220. path, set this to <q>false</q>.
  221. <em>since Ant 1.6</em></td>
  222. <td>No; default is <q>false</q></td>
  223. </tr>
  224. <tr>
  225. <td>maxparallel</td>
  226. <td>Limit the amount of parallelism by passing at most this many sourcefiles at once. Set it to
  227. negative integer for unlimited. <em>Since Ant 1.6</em>.</td>
  228. <td>No, unlimited by default</td>
  229. </tr>
  230. <tr>
  231. <td>addsourcefile</td>
  232. <td>Whether source file names should be added to the command automatically. <em>Since Ant
  233. 1.6</em>.</td>
  234. <td>No; default is <q>true</q></td>
  235. </tr>
  236. <tr>
  237. <td>verbose</td>
  238. <td>Whether to print a summary after execution or not. <em>Since Ant 1.6</em>.</td>
  239. <td>No; default is <q>false</q></td>
  240. </tr>
  241. <tr>
  242. <td>ignoremissing</td>
  243. <td>Whether to ignore nonexistent files specified via filelists. <em>Since Ant 1.6.2</em>.</td>
  244. <td>No; default is <q>true</q></td>
  245. </tr>
  246. <tr>
  247. <td>force</td>
  248. <td>Whether to bypass timestamp comparisons for target files. <em>Since Ant 1.6.3</em>.</td>
  249. <td>No; default is <q>false</q></td>
  250. </tr>
  251. <tr>
  252. <td>discardOutput</td>
  253. <td>Whether output should completely be discarded. This setting is
  254. incompatible with any setting that redirects output to files or
  255. properties.<br/>
  256. If you set this to <q>true</q> error output will be discared as
  257. well unless you redirect error output to files, properties or
  258. enable <q>logError</q>.
  259. <em>Since Ant 1.10.10</em>
  260. </td>
  261. <td>No; defaults to <q>false</q></td>
  262. </tr>
  263. <tr>
  264. <td>discardError</td>
  265. <td>Whether error output should completely be discarded. This
  266. setting is incompatible with any setting that redirects error
  267. output to files or properties as well as <q>logError</q>.
  268. <em>Since Ant 1.10.10</em>
  269. </td>
  270. <td>No; defaults to <q>false</q></td>
  271. </tr>
  272. </table>
  273. <h3>Parameters specified as nested elements</h3>
  274. <h4>fileset</h4>
  275. <p>You can use any number of nested <code>&lt;fileset&gt;</code> elements to define the files for
  276. this task and refer to <code>&lt;fileset&gt;</code>s defined elsewhere.</p>
  277. <h4>filelist</h4>
  278. <p><em>Since Ant 1.6</em></p>
  279. <p>You can use any number of nested <code>&lt;filelist&gt;</code> elements to define the files for
  280. this task and refer to <code>&lt;filelist&gt;</code>s defined elsewhere.</p>
  281. <h4>dirset</h4>
  282. <p><em>Since Ant 1.6</em></p>
  283. <p>You can use any number of nested <code>&lt;dirset&gt;</code> elements to define the directories
  284. for this task and refer to <code>&lt;dirset&gt;</code>s defined elsewhere.</p>
  285. <h4>Any other <a href="../Types/resources.html#collection">resource collection</a></h4>
  286. <p><em>Since Ant 1.7</em></p>
  287. <p>You can use any number of nested resource collections.</p>
  288. <h4>mapper</h4>
  289. <p>A single <code>&lt;mapper&gt;</code> specifies the target files relative to the <var>dest</var>
  290. attribute for dependency checking. If the <var>dest</var> attribute is specified it will be used as
  291. a base directory for resolving relative pathnames returned by the mapper. At least one
  292. <code>&lt;fileset&gt;</code> or <code>&lt;filelist&gt;</code> is required.</p>
  293. <h4>arg</h4>
  294. <p>Command line arguments should be specified as nested <code>&lt;arg&gt;</code>
  295. elements. See <a href="../using.html#arg">Command line arguments</a>.</p>
  296. <h4>srcfile</h4>
  297. <p>By default the file names of the source files will be added to the end of the command line
  298. (unless you set <var>addsourcefile</var> to <q>false</q>). If you need to place it somewhere
  299. different, use a nested <code>&lt;srcfile&gt;</code> element between your <code>&lt;arg&gt;</code>
  300. elements to mark the insertion point.</p>
  301. <table class="attr">
  302. <tr>
  303. <th scope="col">Attribute</th>
  304. <th scope="col">Description</th>
  305. <th scope="col">Required</th>
  306. </tr>
  307. <tr>
  308. <td>prefix</td>
  309. <td>a prefix to place in front of the file name when building the command line
  310. argument. <em>Since Ant 1.8.0</em></td>
  311. <td>No</td>
  312. </tr>
  313. <tr>
  314. <td>suffix</td>
  315. <td>a suffix to append to the file name when building the command line argument. <em>Since Ant
  316. 1.8.0</em></td>
  317. <td>No</td>
  318. </tr>
  319. </table>
  320. <h4>targetfile</h4>
  321. <p><code>&lt;targetfile&gt;</code> is similar to <code>&lt;srcfile&gt;</code> and marks the position
  322. of the target filename on the command line. If omitted, the target filenames will not be added to
  323. the command line at all. This element can only be specified if you also define a nested mapper.</p>
  324. <table class="attr">
  325. <tr>
  326. <th scope="col">Attribute</th>
  327. <th scope="col">Description</th>
  328. <th scope="col">Required</th>
  329. </tr>
  330. <tr>
  331. <td>prefix</td>
  332. <td>a prefix to place in front of the file name when building the command line
  333. argument. <em>Since Ant 1.8.0</em></td>
  334. <td>No</td>
  335. </tr>
  336. <tr>
  337. <td>suffix</td>
  338. <td>a suffix to append to the file name when building the command line argument. <em>Since Ant
  339. 1.8.0</em></td>
  340. <td>No</td>
  341. </tr>
  342. </table>
  343. <h4>env</h4>
  344. <p>It is possible to specify environment variables to pass to the system command via
  345. nested <code>&lt;env&gt;</code> elements. See the description in the section
  346. about <a href="exec.html#env">exec</a></p>
  347. <h4>redirector</h4>
  348. <em>Since Ant 1.6.2</em>
  349. <p>A nested <a href="../Types/redirector.html">I/O Redirector</a> can be specified. &lt;apply&gt;'s
  350. behavior is like that of <a href="exec.html#redirector">exec</a> with regard to redirectors, with
  351. the exception that, in non-<var>parallel</var> mode, file mapping will take place with each
  352. iteration. This grants the user the capacity to receive input from, and send output to, different
  353. files for each sourcefile.</p>
  354. <p>In <var>parallel</var> mode the redirector will be reset for each batch of executions
  355. (with <var>maxparallel</var> &gt; 0) and null will be used a source file just like it is in the case
  356. of <code>exec</code>.</p>
  357. <h3>Examples</h3>
  358. <p>Invoke <kbd>ls -l</kbd>, adding the absolute filenames of all files below <samp>/tmp</samp> not
  359. ending in <samp>.txt</samp> and all files of the FileSet with <var>id</var> <samp>other.files</samp>
  360. to the command line.</p>
  361. <pre>
  362. &lt;apply executable=&quot;ls&quot;&gt;
  363. &lt;arg value=&quot;-l&quot;/&gt;
  364. &lt;fileset dir=&quot;/tmp&quot;&gt;
  365. &lt;patternset&gt;
  366. &lt;exclude name=&quot;**/*.txt&quot;/&gt;
  367. &lt;/patternset&gt;
  368. &lt;/fileset&gt;
  369. &lt;fileset refid=&quot;other.files&quot;/&gt;
  370. &lt;/apply&gt;</pre>
  371. <p>Invoke <kbd>somecommand arg1 SOURCEFILENAME arg2</kbd> for each file in <samp>/tmp</samp>
  372. replacing <code>SOURCEFILENAME</code> with the absolute filename of each file in
  373. turn. If <var>parallel</var> had been set to <q>true</q>, <code>SOURCEFILENAME</code> would be
  374. replaced with the absolute filenames of all files separated by spaces.</p>
  375. <pre>
  376. &lt;apply executable=&quot;somecommand&quot; parallel=&quot;false&quot;&gt;
  377. &lt;arg value=&quot;arg1&quot;/&gt;
  378. &lt;srcfile/&gt;
  379. &lt;arg value=&quot;arg2&quot;/&gt;
  380. &lt;fileset dir=&quot;/tmp&quot;/&gt;
  381. &lt;/apply&gt;
  382. </pre>
  383. <p>Invoke <kbd>cc -c -o TARGETFILE SOURCEFILE</kbd> for each <samp>.c</samp> file that is newer than
  384. the corresponding <samp>.o</samp>, replacing <code>TARGETFILE</code> with the absolute filename of
  385. the <samp>.o</samp> and <code>SOURCEFILE</code> with the absolute name of the <samp>.c</samp>
  386. file.</p>
  387. <pre>
  388. &lt;apply executable=&quot;cc&quot; dest=&quot;src/C&quot; parallel=&quot;false&quot;&gt;
  389. &lt;arg value=&quot;-c&quot;/&gt;
  390. &lt;arg value=&quot;-o&quot;/&gt;
  391. &lt;targetfile/&gt;
  392. &lt;srcfile/&gt;
  393. &lt;fileset dir=&quot;src/C&quot; includes=&quot;*.c&quot;/&gt;
  394. &lt;mapper type=&quot;glob&quot; from=&quot;*.c&quot; to=&quot;*.o&quot;/&gt;
  395. &lt;/apply&gt;</pre>
  396. <p>Apply the fictitious <kbd>processfile</kbd> executable to all files matching <samp>*.file</samp>
  397. in the <samp>src</samp> directory. The <samp>out</samp> <code>&lt;mapper&gt;</code> has been set up
  398. to map <samp>*.file</samp> to <samp>*.out</samp>, then this <code>&lt;mapper&gt;</code> is used to
  399. specify <code>targetfile</code>s for this <code>&lt;apply&gt;</code> task. A reference
  400. to <samp>out</samp> is then used as an <code>&lt;outputmapper&gt;</code> nested in
  401. a <code>&lt;redirector&gt;</code>, which in turn is nested beneath this <code>&lt;apply&gt;</code>
  402. instance. This allows us to perform dependency checking against output files&mdash;the target files
  403. in this case.</p>
  404. <pre>
  405. &lt;mapper id=&quot;out&quot; type=&quot;glob&quot;
  406. from=&quot;src${file.separator}*.file&quot;
  407. to=&quot;dest${file.separator}*.out&quot;/&gt;
  408. &lt;apply executable=&quot;processfile&quot; dest=&quot;dest&quot;&gt;
  409. &lt;fileset dir=&quot;src&quot; includes=&quot;*.file&quot;/&gt;
  410. &lt;mapper refid=&quot;out&quot;/&gt;
  411. &lt;redirector&gt;
  412. &lt;outputmapper refid=&quot;out&quot;/&gt;
  413. &lt;/redirector&gt;
  414. &lt;/apply&gt;</pre>
  415. <p>Apply the <kbd>ls</kbd> executable to all directories in the <code>PATH</code>, effectively
  416. listing all executables that are available on the <code>PATH</code>.</p>
  417. <pre>
  418. &lt;apply executable="ls" parallel="true"
  419. force="true" dest="${basedir}" append="true" type="both"&gt;
  420. &lt;path&gt;
  421. &lt;pathelement path="${env.PATH}"/&gt;
  422. &lt;/path&gt;
  423. &lt;identitymapper/&gt;
  424. &lt;/apply&gt;</pre>
  425. <p>Convert all JavaScript files in the <samp>src</samp> directory using the command <kbd>jsmin &lt;
  426. src/a.js &gt; dest/a.js</kbd>. Because the filename itself should not be passed to
  427. the <code>jsmin</code> program, the <var>addsourcefile</var> is set to <q>false</q>.</p>
  428. <pre>
  429. &lt;apply executable="jsmin" addsourcefile="false"&gt;
  430. &lt;!-- Collect the JS-files --&gt;
  431. &lt;fileset dir="src" includes="*.js"/&gt;
  432. &lt;redirector&gt;
  433. &lt;!-- redirect STDIN; fileset collects relative to its dir, but we need --&gt;
  434. &lt;!-- relative to basedir --&gt;
  435. &lt;inputmapper type="glob" from="*" to="src/*"/&gt;
  436. &lt;!-- redirect STDOUT to file in dest-dir --&gt;
  437. &lt;outputmapper id="out" type="glob" from="*.js" to="dest/*.js"/&gt;
  438. &lt;/redirector&gt;
  439. &lt;/apply&gt;</pre>
  440. </body>
  441. </html>