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.

subant.html 13 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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>Subant Task</title>
  20. </head>
  21. <body>
  22. <h2>Subant Task</h2>
  23. <h3 id="description">Description</h3>
  24. <p>
  25. Calls a given target for all defined sub-builds.
  26. This is an extension
  27. of Ant for bulk project execution.
  28. <strong>This task must not be used outside of a
  29. <code>target</code> if it invokes the same build file it is
  30. part of.</strong>
  31. </p>
  32. <p><em>Since Apache Ant 1.6</em></p>
  33. <p>
  34. <code>subant</code> uses <code>ant</code> internally so many things
  35. said in <a href="ant.html"><code>ant</code>'s manual page</a> apply
  36. here as well.
  37. </p>
  38. <h3 id="Use with directories">Use with directories</h3>
  39. <p>
  40. subant can be used with directory sets to execute a build from different directories.
  41. 2 different options are offered :
  42. </p>
  43. <ul>
  44. <li>
  45. to run the same build file <code>/somepath/otherpath/mybuild.xml</code>
  46. with different base directories, use the genericantfile attribute
  47. </li>
  48. <li>if you want to run <code>directory1/mybuild.xml</code>, <code>directory2/mybuild.xml</code>, <code>....</code>,
  49. use the antfile attribute. The subant task does not set the base directory for you in this case, because you can specify it in each build file.
  50. </li>
  51. </ul>
  52. <h3 id="attributes">Parameters</h3>
  53. <table>
  54. <tr>
  55. <td valign="top" align="left">
  56. <b>Attribute</b>
  57. </td>
  58. <td valign="top" align="left">
  59. <b>Description</b>
  60. </td>
  61. <td valign="top" align="left">
  62. <b>Type</b>
  63. </td>
  64. <td valign="top" align="left">
  65. <b>Requirement</b>
  66. </td>
  67. </tr>
  68. <tr>
  69. <td valign="top" align="left">
  70. antfile
  71. </td>
  72. <td valign="top" align="left">
  73. Build file name, to use in conjunction with directories.<br> Defaults to "build.xml".<br> If <code>genericantfile</code> is set, this attribute is ignored.
  74. </td>
  75. <td valign="top" align="left">
  76. String
  77. </td>
  78. <td valign="top" align="left" rowspan="10">
  79. Optional
  80. </td>
  81. </tr>
  82. <tr>
  83. <td valign="top" align="left">
  84. buildpath
  85. </td>
  86. <td valign="top" align="left">
  87. Set the buildpath to be used to find sub-projects.
  88. </td>
  89. <td valign="top" align="left">
  90. Path
  91. </td>
  92. </tr>
  93. <tr>
  94. <td valign="top" align="left">
  95. buildpathref
  96. </td>
  97. <td valign="top" align="left">
  98. Buildpath to use, by reference.
  99. </td>
  100. <td valign="top" align="left">
  101. Reference
  102. </td>
  103. </tr>
  104. <tr>
  105. <td valign="top" align="left">
  106. failonerror
  107. </td>
  108. <td valign="top" align="left">
  109. Sets whether to fail with a build exception on error, or go on.
  110. </td>
  111. <td valign="top" align="left">
  112. boolean
  113. </td>
  114. </tr>
  115. <tr>
  116. <td valign="top" align="left">
  117. genericantfile
  118. </td>
  119. <td valign="top" align="left">
  120. Build file path, to use in conjunction with directories.<br> Use <code>genericantfile</code>, in order to run the same build file with different basedirs.<br> If this attribute is set, <code>antfile</code> is ignored.
  121. </td>
  122. <td valign="top" align="left">
  123. File
  124. </td>
  125. </tr>
  126. <tr>
  127. <td valign="top" align="left">
  128. inheritall
  129. </td>
  130. <td valign="top" align="left">
  131. Corresponds to
  132. <code>&lt;ant&gt;</code>'s
  133. <code>inheritall</code> attribute but defaults
  134. to false in this task..
  135. </td>
  136. <td valign="top" align="left">
  137. boolean
  138. </td>
  139. </tr>
  140. <tr>
  141. <td valign="top" align="left">
  142. inheritrefs
  143. </td>
  144. <td valign="top" align="left">
  145. Corresponds to <code>&lt;ant&gt;</code>'s <code>inheritrefs</code> attribute.
  146. </td>
  147. <td valign="top" align="left">
  148. boolean
  149. </td>
  150. </tr>
  151. <tr>
  152. <td valign="top" align="left">
  153. output
  154. </td>
  155. <td valign="top" align="left">
  156. Corresponds to <code>&lt;ant&gt;</code>'s <code>output</code> attribute.
  157. </td>
  158. <td valign="top" align="left">
  159. String
  160. </td>
  161. </tr>
  162. <tr>
  163. <td valign="top" align="left">
  164. target
  165. </td>
  166. <td valign="top" align="left">&nbsp;</td>
  167. <td valign="top" align="left">
  168. String
  169. </td>
  170. </tr>
  171. <tr>
  172. <td valign="top" align="left">
  173. verbose
  174. </td>
  175. <td valign="top" align="left">
  176. Enable/ disable log messages showing when each sub-build path is entered/ exited.
  177. The default value is false.
  178. </td>
  179. <td valign="top" align="left">
  180. boolean
  181. </td>
  182. </tr>
  183. </table>
  184. <h3 id="elements">Parameters as nested elements</h3>
  185. <h4>any filesystem based <a href="../Types/resources.html#collection">resource collection</a></h4>
  186. This includes <code>&lt;fileset&gt;</code>,
  187. <code>&lt;dirset&gt;</code> and <code>&lt;filelist&gt;</code>
  188. which are the nested resource collections supported prior
  189. to Ant 1.7.
  190. <h4><strong>dirset</strong> (org.apache.tools.ant.types.DirSet)</h4>
  191. Adds a directory set to the implicit build path.
  192. <p>
  193. <em>Note that the directories will be added to the build path in no particular order, so if order is significant, one should use a file list instead!</em>
  194. </p>
  195. <h4><strong>filelist</strong> (org.apache.tools.ant.types.FileList)</h4>
  196. Adds an ordered file list to the implicit build path.
  197. <p>
  198. <em>Note that contrary to file and directory sets, file lists can reference non-existent files or directories!</em>
  199. </p>
  200. <h4><strong>fileset</strong> (org.apache.tools.ant.types.FileSet)</h4>
  201. Adds a file set to the implicit build path.
  202. <p>
  203. <em>Note that the directories will be added to the build path in no particular order, so if order is significant, one should use a file list instead!</em>
  204. </p>
  205. <h4><strong>property</strong> (org.apache.tools.ant.taskdefs.Property)</h4>
  206. Corresponds to <code>&lt;ant&gt;</code>'s nested <code>&lt;property&gt;</code> element.
  207. <p>
  208. When more than one nested <code>&lt;property&gt;</code> element
  209. would set a property of the same name, the one declared last will
  210. win. This is for backwards compatibility reasons even so it is
  211. different from the way <code>&lt;property&gt;</code> tasks in build
  212. files behave.
  213. </p>
  214. <h4><strong>propertyset</strong> (org.apache.tools.ant.types.PropertySet)</h4>
  215. Corresponds to <code>&lt;ant&gt;</code>'s nested <code>&lt;propertyset&gt;</code> element.
  216. <h4><strong>buildpath</strong> (org.apache.tools.ant.types.Path)</h4>
  217. Creates a nested build path, and add it to the implicit build path.
  218. <h4><strong>buildpathelement</strong> (org.apache.tools.ant.types.Path.PathElement)</h4>
  219. Creates a nested <code>&lt;buildpathelement&gt;</code>, and add it to the implicit build path.
  220. <h4><strong>target</strong> (org.apache.tools.ant.taskdefs.Ant.TargetElement)</h4>
  221. You can specify multiple targets using nested <code>&lt;target&gt;</code> elements
  222. instead of using the target attribute. These will be executed as if
  223. Ant had been invoked with a single target whose dependencies are the
  224. targets so specified, in the order specified.
  225. <table>
  226. <tr>
  227. <td valign="top"><b>Attribute</b></td>
  228. <td valign="top"><b>Description</b></td>
  229. <td align="center" valign="top"><b>Required</b></td>
  230. </tr>
  231. <tr>
  232. <td valign="top">name</td>
  233. <td valign="top">The name of the called target.</td>
  234. <td valign="top" align="center">Yes</td>
  235. </tr>
  236. </table>
  237. <p><em>since Ant 1.7</em>.</p>
  238. <h3 id="examples">Examples</h3>
  239. <pre>
  240. &lt;project name="subant" default="subant1"&gt;
  241. &lt;property name="build.dir" value="subant.build"/&gt;
  242. &lt;target name="subant1"&gt;
  243. &lt;subant target=""&gt;
  244. &lt;property name="build.dir" value="subant1.build"/&gt;
  245. &lt;property name="not.overloaded" value="not.overloaded"/&gt;
  246. &lt;fileset dir="." includes="*/build.xml"/&gt;
  247. &lt;/subant&gt;
  248. &lt;/target&gt;
  249. &lt;/project&gt;
  250. </pre>
  251. <p>
  252. this snippet build file will run ant in each subdirectory of the project directory,
  253. where a file called build.xml can be found.
  254. The property build.dir will have the value subant1.build in the ant projects called by subant.
  255. </p>
  256. <pre>
  257. &lt;subant target=""&gt;
  258. &lt;propertyset&gt;
  259. &lt;propertyref prefix="toplevel"/&gt;
  260. &lt;mapper type="glob" from="foo*" to="bar*"/&gt;
  261. &lt;/propertyset&gt;
  262. &lt;fileset dir="." includes="*/build.xml"/&gt;
  263. &lt;/subant&gt;
  264. </pre>
  265. <p>
  266. this snippet build file will run ant in each subdirectory of the project directory,
  267. where a file called build.xml can be found.
  268. All properties whose name starts with "foo" are passed, their names are changed to start with "bar" instead
  269. </p>
  270. <pre>
  271. &lt;subant target="compile" genericantfile="/opt/project/build1.xml"&gt;
  272. &lt;dirset dir="." includes="projects*"/&gt;
  273. &lt;/subant&gt;
  274. </pre>
  275. <p>
  276. assuming the subdirs of the project dir are called projects1, projects2, projects3
  277. this snippet will execute the compile target of /opt/project/build1.xml,
  278. setting the basedir to projects1, projects2, projects3
  279. </p>
  280. <p>
  281. Now a little more complex - but useful - scenario. Assume that we have
  282. a directory structure like this:
  283. </p>
  284. <pre>
  285. root
  286. | common.xml
  287. | build.xml
  288. |
  289. +-- modules
  290. +-- modA
  291. | +-- src
  292. +-- modB
  293. +-- src
  294. <u><b>common.xml:</b></u><br>
  295. &lt;project&gt;
  296. &lt;property name="src.dir" value="src"/&gt;
  297. &lt;property name="build.dir" value="build"/&gt;
  298. &lt;property name="classes.dir" value="${build.dir}/classes"/&gt;
  299. &lt;target name="compile"&gt;
  300. &lt;mkdir dir="${classes.dir}"/&gt;
  301. &lt;javac srcdir="${src.dir}" destdir="${classes.dir}"/&gt;
  302. &lt;/target&gt;
  303. &lt;!-- more targets --&gt;
  304. &lt;/project&gt;
  305. <u><b>build.xml:</b></u><br>
  306. &lt;project&gt;
  307. &lt;macrodef name="iterate"&gt;
  308. &lt;attribute name="target"/&gt;
  309. &lt;sequential&gt;
  310. &lt;subant target="@{target}"&gt;
  311. &lt;fileset dir="modules" includes="*/build.xml"/&gt;
  312. &lt;/subant&gt;
  313. &lt;/sequential&gt;
  314. &lt;/macrodef&gt;
  315. &lt;target name="compile"&gt;
  316. &lt;iterate target="compile"/&gt;
  317. &lt;/target&gt;
  318. &lt;!-- more targets --&gt;
  319. &lt;/project&gt;
  320. <u><b>modules/modA/build.xml:</b></u><br>
  321. &lt;project name="modA"&gt;
  322. &lt;import file="../../common.xml"/&gt;
  323. &lt;/project&gt;
  324. </pre>
  325. <p>
  326. This results in very small buildfiles in the modules, maintainable
  327. buildfile (common.xml) and a clear project structure. Additionally
  328. the root buildfile is capable to run the whole build over all
  329. modules.
  330. </p>
  331. <pre>
  332. &lt;subant failonerror="false"&gt;
  333. &lt;fileset dir="." includes="**/build.xml" excludes="build.xml"/&gt;
  334. &lt;target name="clean"/&gt;
  335. &lt;target name="build"/&gt;
  336. &lt;/subant&gt;
  337. </pre>
  338. <p>Does a &quot;clean build&quot; for each subproject.</p>
  339. <p>
  340. <b>Hint:</b> because buildfiles are plain xml, you could generate the
  341. masterbuildfile from the common buildfile by using a XSLT transformation:
  342. </p>
  343. <pre>
  344. &lt;xslt in=&quot;common.xml&quot;
  345. out=&quot;master.xml&quot;
  346. style=&quot;${ant.home}/etc/common2master.xsl&quot;/&gt;
  347. </pre>
  348. </body>
  349. </html>