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

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