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.

import.html 9.6 KiB

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