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.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. <h4>Special Properties</h4>
  66. <p>Included files are treated as they are present in the main
  67. buildfile. This makes it easy to understand, but it makes it impossible
  68. for them to reference files and resources relative to their path.
  69. Because of this, for every included file, Ant adds a property that
  70. contains the path to the included buildfile. With this path, the
  71. included buildfile can keep resources and be able to reference them
  72. relative to its position.</p>
  73. <p>So if I include for example a <i>docsbuild.xml</i> file named <b>builddocs</b>,
  74. I can get its path as <b>ant.file.builddocs</b>, similarly to the <b>ant.file</b>
  75. property of the main buildfile.</p>
  76. <p>Note that &quot;builddocs&quot; is not the filename, but the name attribute
  77. present in the included project tag.</p>
  78. <p>
  79. If the included file does not have a name attribute, the ant.file.projectname
  80. property will not be set.
  81. </p>
  82. <h4>Resolving files against the included file</h4>
  83. <p>Suppose your main build file called <code>including.xml</code>
  84. includes a build file <code>included.xml</code>, located anywhere on
  85. the file system, and <code>included.xml</code> reads a set of
  86. properties from <code>included.properties</code>:</p>
  87. <pre>&lt;!-- including.xml --&gt;
  88. &lt;project name="including" basedir="." default="..."&gt;
  89. &nbsp; &lt;include file="${path_to_included}/included.xml"/&gt;
  90. &lt;/project&gt;
  91. &lt;!-- included.xml --&gt;
  92. &lt;project name="included" basedir="." default="..."&gt;
  93. &nbsp; &lt;property file="included.properties"/&gt;
  94. &lt;/project&gt;
  95. </pre>
  96. <p>This snippet however will resolve <code>included.properties</code>
  97. against the basedir of <code>including.xml</code>, because the basedir
  98. of <code>included.xml</code> is ignored by Ant. The right way to use
  99. <code>included.properties</code> is:</p>
  100. <pre>
  101. &lt;!-- included.xml --&gt;
  102. &lt;project name="included" basedir="." default="..."&gt;
  103. &nbsp; &lt;dirname property="included.basedir" file="${ant.file.included}"/&gt;
  104. &nbsp; &lt;property file="${included.basedir}/included.properties"/&gt;
  105. &lt;/project&gt;
  106. </pre>
  107. <p>As explained above <code>${ant.file.included}</code> stores the
  108. path of the build script, that defines the project called
  109. <code>included</code>, (in short it stores the path to
  110. <code>included.xml</code>) and <a
  111. href="dirname.html"><code>&lt;dirname&gt;</code></a> takes its
  112. directory. This technique also allows <code>included.xml</code> to be
  113. used as a standalone file (without being included in other
  114. project).</p>
  115. <h3>Parameters</h3>
  116. <table border="1" cellpadding="2" cellspacing="0">
  117. <tbody>
  118. <tr>
  119. <td valign="top"><b>Attribute</b></td>
  120. <td valign="top"><b>Description</b></td>
  121. <td align="center" valign="top"><b>Required</b></td>
  122. </tr>
  123. <tr>
  124. <td valign="top">
  125. file
  126. </td>
  127. <td valign="top">
  128. The file to include. If this is a relative file name, the file name will be resolved
  129. relative to the <i>including</i> file. <b>Note</b>, this is unlike most other
  130. ant file attributes, where relative files are resolved relative to ${basedir}.
  131. </td>
  132. <td valign="top" align="center">Yes</td>
  133. </tr>
  134. <tr>
  135. <td valign="top">
  136. optional
  137. </td>
  138. <td valign="top">
  139. If true, do not stop the build if the file does not exist,
  140. default is false.
  141. </td>
  142. <td valign="top" align="center">No</td>
  143. </tr>
  144. <tr>
  145. <td valign="top">
  146. as
  147. </td>
  148. <td valign="top">
  149. Specifies the prefix prepended to the target names. If
  150. ommitted, the name attribute of the project tag of the
  151. imported file will be used.
  152. </td>
  153. <td valign="top" align="center">Yes, if the included file's
  154. project tag doesn't specify a name attribute.</td>
  155. </tr>
  156. </tbody>
  157. </table>
  158. <h3>Examples</h3>
  159. <pre>&nbsp; &lt;include file=&quot;../common-targets.xml&quot;/&gt;
  160. </pre>
  161. <p>Includes targets from the common-targets.xml file that is in a parent
  162. directory.</p>
  163. <pre>&nbsp; &lt;include file=&quot;${deploy-platform}.xml&quot;/&gt;
  164. </pre>
  165. <p>Includes the project defined by the property deploy-platform</p>
  166. <h3>How is <a href="import.html">&lt;import&gt;</a> different
  167. from &lt;include&gt;?</h3>
  168. <p>When using import the imported targets are available by up to two
  169. names. Their "normal" name without any prefix and potentially with
  170. a prefixed name (the value of the as attribute or the imported
  171. project's name attribute, if any).</p>
  172. <p>When using include the included targets are only available in the
  173. prefixed form.</p>
  174. <p>When using import, the imported target's depends attribute
  175. remains unchanged, i.e. it uses "normal" names and allows you to
  176. override targets in the dependency list.</p>
  177. <p>When using include, the included target's depends attribute is
  178. rewritten so that prefixed names are used. This allows writers of
  179. the included file to control which target is invoked as part of the
  180. dependencies.</p>
  181. <p>It is possible to include the same file more than once by using
  182. different prefixes, it is not possible to import the same file more
  183. than once.</p>
  184. <p>Use import if you intend to override a target, otherwise use include.</p>
  185. <p><i>nested.xml</i> shall be:</p>
  186. <pre>
  187. &lt;project&gt;
  188. &lt;target name="setUp"&gt;
  189. &lt;property name="prop" value="in nested"/&gt;
  190. &lt;/target&gt;
  191. &lt;target name="echo" depends="setUp"&gt;
  192. &lt;echo&gt;prop has the value ${prop}&lt;/echo&gt;
  193. &lt;/target&gt;
  194. &lt;/project&gt;
  195. </pre>
  196. <p>When using import like in</p>
  197. <pre>
  198. &lt;project&gt;
  199. &lt;target name="setUp"&gt;
  200. &lt;property name="prop" value="in importing"/&gt;
  201. &lt;/target&gt;
  202. &lt;import file="nested.xml" as="nested"/&gt;
  203. &lt;/project&gt;
  204. </pre>
  205. <p>Running the target <i>nested.echo</i> will emit:
  206. <pre>
  207. setUp:
  208. nested.echo:
  209. [echo] prop has the value in importing
  210. </pre>
  211. <p>When using include like in</p>
  212. <pre>
  213. &lt;project&gt;
  214. &lt;target name="setUp"&gt;
  215. &lt;property name="prop" value="in importing"/&gt;
  216. &lt;/target&gt;
  217. &lt;include file="nested.xml" as="nested"/&gt;
  218. &lt;/project&gt;
  219. </pre>
  220. <p>Running the target <i>nested.echo</i> will emit:
  221. <pre>
  222. nested.setUp:
  223. nested.echo:
  224. [echo] prop has the value in nested
  225. </pre>
  226. <p>and there won't be any target named "echo" on the including build file.</p>
  227. </body>
  228. </html>