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

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