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.

include.html 11 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  16. <html>
  17. <head>
  18. <meta http-equiv="Content-Language" content="en-us">
  19. <link rel="stylesheet" type="text/css" href="../stylesheets/style.css">
  20. <title>Include Task</title>
  21. </head>
  22. <body>
  23. <h2><a name="include">Include</a></h2>
  24. <h3>Description</h3>
  25. <p>
  26. Include another build file into the current project.
  27. </p>
  28. <p><em>since Apache Ant 1.8.0</em></p>
  29. <p>
  30. <b>Note</b> this task heavily relies on the ProjectHelper
  31. implementation and doesn't really perform any work of its own. If
  32. you have configured Ant to use a ProjectHelper other than Ant's
  33. default, this task may or may not work.
  34. </p>
  35. <p>
  36. On execution it will read another Ant file into the same Project
  37. rewriting the included target names and depends lists. This is
  38. different
  39. from <a href="http://ant.apache.org/faq.html#xml-entity-include">Entity
  40. Includes as explained in the Ant FAQ</a> insofar as the target
  41. names get prefixed by the included project's name or the as
  42. attribute and do not appear as if the file was contained in the
  43. including file.
  44. </p>
  45. <p>
  46. The include task may only be used as a top-level task. This means that
  47. it may not be used in a target.
  48. </p>
  49. <p>
  50. There are two further functional aspects that pertain to this task and
  51. that are not possible with entity includes:
  52. <ul>
  53. <li>target rewriting</li>
  54. <li>special properties</li>
  55. </ul>
  56. </p>
  57. <h4>Target rewriting</h4>
  58. <p>Any target in the included file will be renamed
  59. to <i>prefix.name</i> where <i>name</i> is the original target's
  60. name and <i>prefix</i> is either the value of the <i>as</i>
  61. attribute or the <i>name</i> attribute of the <i>project</i> tag of
  62. the included file.</p>
  63. <p>The depends attribute of all included targets is rewritten so that
  64. all target names are prefixed as well. This makes the included file
  65. self-contained.</p>
  66. <p>Note that prefixes nest, so if a build file includes a file with
  67. prefix "a" and the included file includes another file with prefix
  68. "b", then the targets of that last build file will be prefixed by
  69. "a.b.".</p>
  70. <p><code>&lt;import&gt;</code> contribute to the prefix as well, but
  71. only if their <code>as</code> attribute has been specified.
  72. <h4>Special Properties</h4>
  73. <p>Included files are treated as they are present in the main
  74. buildfile. This makes it easy to understand, but it makes it impossible
  75. for them to reference files and resources relative to their path.
  76. Because of this, for every included file, Ant adds a property that
  77. contains the path to the included buildfile. With this path, the
  78. included buildfile can keep resources and be able to reference them
  79. relative to its position.</p>
  80. <p>So if I include for example a <i>docsbuild.xml</i> file named <b>builddocs</b>,
  81. I can get its path as <b>ant.file.builddocs</b>, similarly to the <b>ant.file</b>
  82. property of the main buildfile.</p>
  83. <p>Note that &quot;builddocs&quot; is not the filename, but the name attribute
  84. present in the included project tag.</p>
  85. <p>
  86. If the included file does not have a name attribute, the ant.file.projectname
  87. property will not be set.
  88. </p>
  89. <p>If you need to know whether the current build file's source has
  90. been a file or an URL you can consult the
  91. property <b>ant.file.type.<em>projectname</em></b> (using the same
  92. example as above <b>ant.file.type.builddocs</b>) which either have
  93. the value "file" or "url".</p>
  94. <h4>Resolving files against the included file</h4>
  95. <p>Suppose your main build file called <code>including.xml</code>
  96. includes a build file <code>included.xml</code>, located anywhere on
  97. the file system, and <code>included.xml</code> reads a set of
  98. properties from <code>included.properties</code>:</p>
  99. <pre>&lt;!-- including.xml --&gt;
  100. &lt;project name="including" basedir="." default="..."&gt;
  101. &nbsp; &lt;include file="${path_to_included}/included.xml"/&gt;
  102. &lt;/project&gt;
  103. &lt;!-- included.xml --&gt;
  104. &lt;project name="included" basedir="." default="..."&gt;
  105. &nbsp; &lt;property file="included.properties"/&gt;
  106. &lt;/project&gt;
  107. </pre>
  108. <p>This snippet however will resolve <code>included.properties</code>
  109. against the basedir of <code>including.xml</code>, because the basedir
  110. of <code>included.xml</code> is ignored by Ant. The right way to use
  111. <code>included.properties</code> is:</p>
  112. <pre>
  113. &lt;!-- included.xml --&gt;
  114. &lt;project name="included" basedir="." default="..."&gt;
  115. &nbsp; &lt;dirname property="included.basedir" file="${ant.file.included}"/&gt;
  116. &nbsp; &lt;property file="${included.basedir}/included.properties"/&gt;
  117. &lt;/project&gt;
  118. </pre>
  119. <p>As explained above <code>${ant.file.included}</code> stores the
  120. path of the build script, that defines the project called
  121. <code>included</code>, (in short it stores the path to
  122. <code>included.xml</code>) and <a
  123. href="dirname.html"><code>&lt;dirname&gt;</code></a> takes its
  124. directory. This technique also allows <code>included.xml</code> to be
  125. used as a standalone file (without being included in other
  126. project).</p>
  127. <p>The above description only works for included files that actually
  128. are included from files and not from URLs. For files included from
  129. URLs using resources relative to the included file requires you to
  130. use tasks that can work on non-file resources in the first place.
  131. To create a relative resource you'd use something like:</p>
  132. <pre>
  133. &lt;loadproperties&gt;
  134. &lt;url baseUrl="${ant.file.included}"
  135. relativePath="included.properties"/&gt;
  136. &lt;/loadproperties&gt;
  137. </pre>
  138. <h3>Parameters</h3>
  139. <table border="1" cellpadding="2" cellspacing="0">
  140. <tbody>
  141. <tr>
  142. <td valign="top"><b>Attribute</b></td>
  143. <td valign="top"><b>Description</b></td>
  144. <td align="center" valign="top"><b>Required</b></td>
  145. </tr>
  146. <tr>
  147. <td valign="top">
  148. file
  149. </td>
  150. <td valign="top">
  151. The file to include. If this is a relative file name, the file name will be resolved
  152. relative to the <i>including</i> file. <b>Note</b>, this is unlike most other
  153. ant file attributes, where relative files are resolved relative to ${basedir}.
  154. </td>
  155. <td valign="top" align="center">Yes or a nested resource collection</td>
  156. </tr>
  157. <tr>
  158. <td valign="top">
  159. optional
  160. </td>
  161. <td valign="top">
  162. If true, do not stop the build if the file does not exist,
  163. default is false.
  164. </td>
  165. <td valign="top" align="center">No</td>
  166. </tr>
  167. <tr>
  168. <td valign="top">
  169. as
  170. </td>
  171. <td valign="top">
  172. Specifies the prefix prepended to the target names. If
  173. omitted, the name attribute of the project tag of the
  174. included file will be used.
  175. </td>
  176. <td valign="top" align="center">Yes, if the included file's
  177. project tag doesn't specify a name attribute.</td>
  178. </tr>
  179. <tr>
  180. <td valign="top">
  181. prefixSeparator
  182. </td>
  183. <td valign="top">
  184. Specifies the separator to be used between the prefix and the
  185. target name. Defaults to ".".
  186. </td>
  187. <td valign="top" align="center">No</td>
  188. </tr>
  189. </tbody>
  190. </table>
  191. <h3>Parameters specified as nested elements</h3>
  192. <h4>any <a href="../Types/resources.html">resource</a> or resource
  193. collection</h4>
  194. <p>The specified resources will be included.</p>
  195. <h3>Examples</h3>
  196. <pre>&nbsp; &lt;include file=&quot;../common-targets.xml&quot;/&gt;
  197. </pre>
  198. <p>Includes targets from the common-targets.xml file that is in a parent
  199. directory.</p>
  200. <pre>&nbsp; &lt;include file=&quot;${deploy-platform}.xml&quot;/&gt;
  201. </pre>
  202. <p>Includes the project defined by the property deploy-platform</p>
  203. <pre>
  204. &lt;include&gt;
  205. &lt;javaresource name="common/targets.xml"&gt;
  206. &lt;classpath location="common.jar"/&gt;
  207. &lt;/javaresource&gt;
  208. &lt;/include&gt;
  209. </pre>
  210. <p>Includes targets from the targets.xml file that is inside the
  211. directory common inside the jar file common.jar.</p>
  212. <h3>How is <a href="import.html">&lt;import&gt;</a> different
  213. from &lt;include&gt;?</h3>
  214. <p>The short version: Use import if you intend to override a target,
  215. otherwise use include.</p>
  216. <p>When using import the imported targets are available by up to two
  217. names. Their "normal" name without any prefix and potentially with
  218. a prefixed name (the value of the as attribute or the imported
  219. project's name attribute, if any).</p>
  220. <p>When using include the included targets are only available in the
  221. prefixed form.</p>
  222. <p>When using import, the imported target's depends attribute
  223. remains unchanged, i.e. it uses "normal" names and allows you to
  224. override targets in the dependency list.</p>
  225. <p>When using include, the included targets cannot be overridden and
  226. their depends attributes are rewritten so that prefixed names are
  227. used. This allows writers of the included file to control which
  228. target is invoked as part of the dependencies.</p>
  229. <p>It is possible to include the same file more than once by using
  230. different prefixes, it is not possible to import the same file more
  231. than once.</p>
  232. <h4>Examples</h4>
  233. <p><i>nested.xml</i> shall be:</p>
  234. <pre>
  235. &lt;project&gt;
  236. &lt;target name="setUp"&gt;
  237. &lt;property name="prop" value="in nested"/&gt;
  238. &lt;/target&gt;
  239. &lt;target name="echo" depends="setUp"&gt;
  240. &lt;echo&gt;prop has the value ${prop}&lt;/echo&gt;
  241. &lt;/target&gt;
  242. &lt;/project&gt;
  243. </pre>
  244. <p>When using import like in</p>
  245. <pre>
  246. &lt;project default="test"&gt;
  247. &lt;target name="setUp"&gt;
  248. &lt;property name="prop" value="in importing"/&gt;
  249. &lt;/target&gt;
  250. &lt;import file="nested.xml" as="nested"/&gt;
  251. &lt;target name="test" depends="nested.echo"/&gt;
  252. &lt;/project&gt;
  253. </pre>
  254. <p>Running the build file will emit:
  255. <pre>
  256. setUp:
  257. nested.echo:
  258. [echo] prop has the value in importing
  259. test:
  260. </pre>
  261. <p>When using include like in</p>
  262. <pre>
  263. &lt;project default="test"&gt;
  264. &lt;target name="setUp"&gt;
  265. &lt;property name="prop" value="in importing"/&gt;
  266. &lt;/target&gt;
  267. &lt;include file="nested.xml" as="nested"/&gt;
  268. &lt;target name="test" depends="nested.echo"/&gt;
  269. &lt;/project&gt;
  270. </pre>
  271. <p>Running the target build file will emit:
  272. <pre>
  273. nested.setUp:
  274. nested.echo:
  275. [echo] prop has the value in nested
  276. test:
  277. </pre>
  278. <p>and there won't be any target named "echo" on the including build file.</p>
  279. </body>
  280. </html>