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

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