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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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>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. </table>
  252. <h3>Parameters specified as nested elements</h3>
  253. <h4>fileset</h4>
  254. <p>You can use any number of nested <code>&lt;fileset&gt;</code> elements to define the files for
  255. this task and refer to <code>&lt;fileset&gt;</code>s defined elsewhere.</p>
  256. <h4>filelist</h4>
  257. <p><em>Since Ant 1.6</em></p>
  258. <p>You can use any number of nested <code>&lt;filelist&gt;</code> elements to define the files for
  259. this task and refer to <code>&lt;filelist&gt;</code>s defined elsewhere.</p>
  260. <h4>dirset</h4>
  261. <p><em>Since Ant 1.6</em></p>
  262. <p>You can use any number of nested <code>&lt;dirset&gt;</code> elements to define the directories
  263. for this task and refer to <code>&lt;dirset&gt;</code>s defined elsewhere.</p>
  264. <h4>Any other <a href="../Types/resources.html#collection">resource collection</a></h4>
  265. <p><em>Since Ant 1.7</em></p>
  266. <p>You can use any number of nested resource collections.</p>
  267. <h4>mapper</h4>
  268. <p>A single <code>&lt;mapper&gt;</code> specifies the target files relative to the <var>dest</var>
  269. attribute for dependency checking. If the <var>dest</var> attribute is specified it will be used as
  270. a base directory for resolving relative pathnames returned by the mapper. At least one
  271. <code>&lt;fileset&gt;</code> or <code>&lt;filelist&gt;</code> is required.</p>
  272. <h4>arg</h4>
  273. <p>Command line arguments should be specified as nested <code>&lt;arg&gt;</code>
  274. elements. See <a href="../using.html#arg">Command line arguments</a>.</p>
  275. <h4>srcfile</h4>
  276. <p>By default the file names of the source files will be added to the end of the command line
  277. (unless you set <var>addsourcefile</var> to <q>false</q>). If you need to place it somewhere
  278. different, use a nested <code>&lt;srcfile&gt;</code> element between your <code>&lt;arg&gt;</code>
  279. elements to mark the insertion point.</p>
  280. <table class="attr">
  281. <tr>
  282. <th scope="col">Attribute</th>
  283. <th scope="col">Description</th>
  284. <th scope="col">Required</th>
  285. </tr>
  286. <tr>
  287. <td>prefix</td>
  288. <td>a prefix to place in front of the file name when building the command line
  289. argument. <em>Since Ant 1.8.0</em></td>
  290. <td>No</td>
  291. </tr>
  292. <tr>
  293. <td>suffix</td>
  294. <td>a suffix to append to the file name when building the command line argument. <em>Since Ant
  295. 1.8.0</em></td>
  296. <td>No</td>
  297. </tr>
  298. </table>
  299. <h4>targetfile</h4>
  300. <p><code>&lt;targetfile&gt;</code> is similar to <code>&lt;srcfile&gt;</code> and marks the position
  301. of the target filename on the command line. If omitted, the target filenames will not be added to
  302. the command line at all. This element can only be specified if you also define a nested mapper.</p>
  303. <table class="attr">
  304. <tr>
  305. <th scope="col">Attribute</th>
  306. <th scope="col">Description</th>
  307. <th scope="col">Required</th>
  308. </tr>
  309. <tr>
  310. <td>prefix</td>
  311. <td>a prefix to place in front of the file name when building the command line
  312. argument. <em>Since Ant 1.8.0</em></td>
  313. <td>No</td>
  314. </tr>
  315. <tr>
  316. <td>suffix</td>
  317. <td>a suffix to append to the file name when building the command line argument. <em>Since Ant
  318. 1.8.0</em></td>
  319. <td>No</td>
  320. </tr>
  321. </table>
  322. <h4>env</h4>
  323. <p>It is possible to specify environment variables to pass to the system command via
  324. nested <code>&lt;env&gt;</code> elements. See the description in the section
  325. about <a href="exec.html#env">exec</a></p>
  326. <h4>redirector</h4>
  327. <em>Since Ant 1.6.2</em>
  328. <p>A nested <a href="../Types/redirector.html">I/O Redirector</a> can be specified. &lt;apply&gt;'s
  329. behavior is like that of <a href="exec.html#redirector">exec</a> with regard to redirectors, with
  330. the exception that, in non-<var>parallel</var> mode, file mapping will take place with each
  331. iteration. This grants the user the capacity to receive input from, and send output to, different
  332. files for each sourcefile.</p>
  333. <p>In <var>parallel</var> mode the redirector will be reset for each batch of executions
  334. (with <var>maxparallel</var> &gt; 0) and null will be used a source file just like it is in the case
  335. of <code>exec</code>.</p>
  336. <h3>Examples</h3>
  337. <p>Invoke <kbd>ls -l</kbd>, adding the absolute filenames of all files below <samp>/tmp</samp> not
  338. ending in <samp>.txt</samp> and all files of the FileSet with <var>id</var> <samp>other.files</samp>
  339. to the command line.</p>
  340. <pre>
  341. &lt;apply executable=&quot;ls&quot;&gt;
  342. &lt;arg value=&quot;-l&quot;/&gt;
  343. &lt;fileset dir=&quot;/tmp&quot;&gt;
  344. &lt;patternset&gt;
  345. &lt;exclude name=&quot;**/*.txt&quot;/&gt;
  346. &lt;/patternset&gt;
  347. &lt;/fileset&gt;
  348. &lt;fileset refid=&quot;other.files&quot;/&gt;
  349. &lt;/apply&gt;</pre>
  350. <p>Invoke <kbd>somecommand arg1 SOURCEFILENAME arg2</kbd> for each file in <samp>/tmp</samp>
  351. replacing <code>SOURCEFILENAME</code> with the absolute filename of each file in
  352. turn. If <var>parallel</var> had been set to <q>true</q>, <code>SOURCEFILENAME</code> would be
  353. replaced with the absolute filenames of all files separated by spaces.</p>
  354. <pre>
  355. &lt;apply executable=&quot;somecommand&quot; parallel=&quot;false&quot;&gt;
  356. &lt;arg value=&quot;arg1&quot;/&gt;
  357. &lt;srcfile/&gt;
  358. &lt;arg value=&quot;arg2&quot;/&gt;
  359. &lt;fileset dir=&quot;/tmp&quot;/&gt;
  360. &lt;/apply&gt;
  361. </pre>
  362. <p>Invoke <kbd>cc -c -o TARGETFILE SOURCEFILE</kbd> for each <samp>.c</samp> file that is newer than
  363. the corresponding <samp>.o</samp>, replacing <code>TARGETFILE</code> with the absolute filename of
  364. the <samp>.o</samp> and <code>SOURCEFILE</code> with the absolute name of the <samp>.c</samp>
  365. file.</p>
  366. <pre>
  367. &lt;apply executable=&quot;cc&quot; dest=&quot;src/C&quot; parallel=&quot;false&quot;&gt;
  368. &lt;arg value=&quot;-c&quot;/&gt;
  369. &lt;arg value=&quot;-o&quot;/&gt;
  370. &lt;targetfile/&gt;
  371. &lt;srcfile/&gt;
  372. &lt;fileset dir=&quot;src/C&quot; includes=&quot;*.c&quot;/&gt;
  373. &lt;mapper type=&quot;glob&quot; from=&quot;*.c&quot; to=&quot;*.o&quot;/&gt;
  374. &lt;/apply&gt;</pre>
  375. <p>Apply the fictitious <kbd>processfile</kbd> executable to all files matching <samp>*.file</samp>
  376. in the <samp>src</samp> directory. The <samp>out</samp> <code>&lt;mapper&gt;</code> has been set up
  377. to map <samp>*.file</samp> to <samp>*.out</samp>, then this <code>&lt;mapper&gt;</code> is used to
  378. specify <code>targetfile</code>s for this <code>&lt;apply&gt;</code> task. A reference
  379. to <samp>out</samp> is then used as an <code>&lt;outputmapper&gt;</code> nested in
  380. a <code>&lt;redirector&gt;</code>, which in turn is nested beneath this <code>&lt;apply&gt;</code>
  381. instance. This allows us to perform dependency checking against output files&mdash;the target files
  382. in this case.</p>
  383. <pre>
  384. &lt;mapper id=&quot;out&quot; type=&quot;glob&quot;
  385. from=&quot;src${file.separator}*.file&quot;
  386. to=&quot;dest${file.separator}*.out&quot;/&gt;
  387. &lt;apply executable=&quot;processfile&quot; dest=&quot;dest&quot;&gt;
  388. &lt;fileset dir=&quot;src&quot; includes=&quot;*.file&quot;/&gt;
  389. &lt;mapper refid=&quot;out&quot;/&gt;
  390. &lt;redirector&gt;
  391. &lt;outputmapper refid=&quot;out&quot;/&gt;
  392. &lt;/redirector&gt;
  393. &lt;/apply&gt;</pre>
  394. <p>Apply the <kbd>ls</kbd> executable to all directories in the <code>PATH</code>, effectively
  395. listing all executables that are available on the <code>PATH</code>.</p>
  396. <pre>
  397. &lt;apply executable="ls" parallel="true"
  398. force="true" dest="${basedir}" append="true" type="both"&gt;
  399. &lt;path&gt;
  400. &lt;pathelement path="${env.PATH}"/&gt;
  401. &lt;/path&gt;
  402. &lt;identitymapper/&gt;
  403. &lt;/apply&gt;</pre>
  404. <p>Convert all JavaScript files in the <samp>src</samp> directory using the command <kbd>jsmin &lt;
  405. src/a.js &gt; dest/a.js</kbd>. Because the filename itself should not be passed to
  406. the <code>jsmin</code> program, the <var>addsourcefile</var> is set to <q>false</q>.</p>
  407. <pre>
  408. &lt;apply executable="jsmin" addsourcefile="false"&gt;
  409. &lt;!-- Collect the JS-files --&gt;
  410. &lt;fileset dir="src" includes="*.js"/&gt;
  411. &lt;redirector&gt;
  412. &lt;!-- redirect STDIN; fileset collects relative to its dir, but we need --&gt;
  413. &lt;!-- relative to basedir --&gt;
  414. &lt;inputmapper type="glob" from="*" to="src/*"/&gt;
  415. &lt;!-- redirect STDOUT to file in dest-dir --&gt;
  416. &lt;outputmapper id="out" type="glob" from="*.js" to="dest/*.js"/&gt;
  417. &lt;/redirector&gt;
  418. &lt;/apply&gt;</pre>
  419. </body>
  420. </html>