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

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