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

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