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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. <p>Since Ant 1.8.0 the task can also import resources from URLs or
  87. classpath resources (which are URLs, really). If you need to know
  88. whether the current build file's source has been a file or an URL
  89. you can consult the
  90. property <b>ant.file.type.<em>projectname</em></b> (using the same
  91. example as above <b>ant.file.type.builddocs</b>) which either have
  92. the value "file" or "url".</p>
  93. <h4>Resolving files against the imported file</h4>
  94. <p>Suppose your main build file called <code>importing.xml</code>
  95. imports a build file <code>imported.xml</code>, located anywhere on
  96. the file system, and <code>imported.xml</code> reads a set of
  97. properties from <code>imported.properties</code>:</p>
  98. <pre>&lt;!-- importing.xml --&gt;
  99. &lt;project name="importing" basedir="." default="..."&gt;
  100. &nbsp; &lt;import file="${path_to_imported}/imported.xml"/&gt;
  101. &lt;/project&gt;
  102. &lt;!-- imported.xml --&gt;
  103. &lt;project name="imported" basedir="." default="..."&gt;
  104. &nbsp; &lt;property file="imported.properties"/&gt;
  105. &lt;/project&gt;
  106. </pre>
  107. <p>This snippet however will resolve <code>imported.properties</code>
  108. against the basedir of <code>importing.xml</code>, because the basedir
  109. of <code>imported.xml</code> is ignored by Ant. The right way to use
  110. <code>imported.properties</code> is:</p>
  111. <pre>
  112. &lt;!-- imported.xml --&gt;
  113. &lt;project name="imported" basedir="." default="..."&gt;
  114. &nbsp; &lt;dirname property="imported.basedir" file="${ant.file.imported}"/&gt;
  115. &nbsp; &lt;property file="${imported.basedir}/imported.properties"/&gt;
  116. &lt;/project&gt;
  117. </pre>
  118. <p>As explained above <code>${ant.file.imported}</code> stores the
  119. path of the build script, that defines the project called
  120. <code>imported</code>, (in short it stores the path to
  121. <code>imported.xml</code>) and <a
  122. href="dirname.html"><code>&lt;dirname&gt;</code></a> takes its
  123. directory. This technique also allows <code>imported.xml</code> to be
  124. used as a standalone file (without being imported in other
  125. project).</p>
  126. <p>The above description only works for imported files that actually
  127. are imported from files and not from URLs. For files imported from
  128. URLs using resources relative to the imported file requires you to
  129. use tasks that can work on non-file resources in the first place.
  130. To create a relative resource you'd use something like:</p>
  131. <pre>
  132. &lt;loadproperties&gt;
  133. &lt;url baseUrl="${ant.file.imported}"
  134. relativePath="imported.properties"/&gt;
  135. &lt;/loadproperties&gt;
  136. </pre>
  137. <h3>Parameters</h3>
  138. <table border="1" cellpadding="2" cellspacing="0">
  139. <tbody>
  140. <tr>
  141. <td valign="top"><b>Attribute</b></td>
  142. <td valign="top"><b>Description</b></td>
  143. <td align="center" valign="top"><b>Required</b></td>
  144. </tr>
  145. <tr>
  146. <td valign="top">
  147. file
  148. </td>
  149. <td valign="top">
  150. The file to import. If this is a relative file name, the file name will be resolved
  151. relative to the <i>importing</i> file. <b>Note</b>, this is unlike most other
  152. ant file attributes, where relative files are resolved relative to ${basedir}.
  153. </td>
  154. <td valign="top" align="center">Yes or a nested resource collection</td>
  155. </tr>
  156. <tr>
  157. <td valign="top">
  158. optional
  159. </td>
  160. <td valign="top">
  161. If true, do not stop the build if the file does not exist,
  162. default is false.
  163. </td>
  164. <td valign="top" align="center">No</td>
  165. </tr>
  166. <tr>
  167. <td valign="top">
  168. as
  169. </td>
  170. <td valign="top">
  171. Specifies the prefix prepended to the target names. If
  172. ommitted, the name attribute of the project tag of the
  173. imported file will be used.
  174. </td>
  175. <td valign="top" align="center">No</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="../CoreTypes/resources.html">resource</a> or resource
  191. collection</h4>
  192. <p>The specified resources will be imported. <em>Since Ant
  193. 1.8.0</em></p>
  194. <h3>Examples</h3>
  195. <pre>&nbsp; &lt;import file=&quot;../common-targets.xml&quot;/&gt;
  196. </pre>
  197. <p>Imports targets from the common-targets.xml file that is in a parent
  198. directory.</p>
  199. <pre>&nbsp; &lt;import file=&quot;${deploy-platform}.xml&quot;/&gt;
  200. </pre>
  201. <p>Imports the project defined by the property deploy-platform</p>
  202. <pre>
  203. &lt;import&gt;
  204. &lt;javaresource name="common/targets.xml"&gt;
  205. &lt;classpath location="common.jar"/&gt;
  206. &lt;/javaresource&gt;
  207. &lt;/import&gt;
  208. </pre>
  209. <p>Imports targets from the targets.xml file that is inside the
  210. directory common inside the jar file common.jar.</p>
  211. <h3>How is &lt;import&gt; different
  212. from <a href="include.html">&lt;include&gt;</a>?</h3>
  213. <p>The short version: Use import if you intend to override a target,
  214. otherwise use include.</p>
  215. <p>When using import the imported targets are available by up to two
  216. names. Their "normal" name without any prefix and potentially with
  217. a prefixed name (the value of the as attribute or the imported
  218. project's name attribute, if any).</p>
  219. <p>When using include the included targets are only available in the
  220. prefixed form.</p>
  221. <p>When using import, the imported target's depends attribute
  222. remains unchanged, i.e. it uses "normal" names and allows you to
  223. override targets in the dependency list.</p>
  224. <p>When using include, the included targets cannot be overridden and
  225. their depends attributes are rewritten so that prefixed names are
  226. used. This allows writers of the included file to control which
  227. target is invoked as part of the dependencies.</p>
  228. <p>It is possible to include the same file more than once by using
  229. different prefixes, it is not possible to import the same file more
  230. than once.</p>
  231. <h4>Examples</h4>
  232. <p><i>nested.xml</i> shall be:</p>
  233. <pre>
  234. &lt;project&gt;
  235. &lt;target name="setUp"&gt;
  236. &lt;property name="prop" value="in nested"/&gt;
  237. &lt;/target&gt;
  238. &lt;target name="echo" depends="setUp"&gt;
  239. &lt;echo&gt;prop has the value ${prop}&lt;/echo&gt;
  240. &lt;/target&gt;
  241. &lt;/project&gt;
  242. </pre>
  243. <p>When using import like in</p>
  244. <pre>
  245. &lt;project default="test"&gt;
  246. &lt;target name="setUp"&gt;
  247. &lt;property name="prop" value="in importing"/&gt;
  248. &lt;/target&gt;
  249. &lt;import file="nested.xml" as="nested"/&gt;
  250. &lt;target name="test" depends="nested.echo"/&gt;
  251. &lt;/project&gt;
  252. </pre>
  253. <p>Running the build file will emit:
  254. <pre>
  255. setUp:
  256. nested.echo:
  257. [echo] prop has the value in importing
  258. test:
  259. </pre>
  260. <p>When using include like in</p>
  261. <pre>
  262. &lt;project default="test"&gt;
  263. &lt;target name="setUp"&gt;
  264. &lt;property name="prop" value="in importing"/&gt;
  265. &lt;/target&gt;
  266. &lt;include file="nested.xml" as="nested"/&gt;
  267. &lt;target name="test" depends="nested.echo"/&gt;
  268. &lt;/project&gt;
  269. </pre>
  270. <p>Running the target build file will emit:
  271. <pre>
  272. nested.setUp:
  273. nested.echo:
  274. [echo] prop has the value in nested
  275. test:
  276. </pre>
  277. <p>and there won't be any target named "echo" on the including build file.</p>
  278. </body>
  279. </html>