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 7.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. On execution it will read another Ant file into
  30. the same Project. This means that it basically works like the
  31. <a href="http://ant.apache.org/faq.html#xml-entity-include">Entity
  32. Includes as explained in the Ant FAQ</a>, as if the imported file was
  33. contained in the importing file, minus the top <code>&lt;project&gt;</code>
  34. tag.
  35. </p>
  36. <p>
  37. The import task may only be used as a top-level task. This means that
  38. it may not be used in a target.
  39. </p>
  40. <p>
  41. There are two further functional aspects that pertain to this task and
  42. that are not possible with entity includes:
  43. <ul>
  44. <li>target overriding</li>
  45. <li>special properties</li>
  46. </ul>
  47. </p>
  48. <h4>Target overriding</h4>
  49. <p>If a target in the main file is also present in at least one of the
  50. imported files, the one from the main file takes precedence.</p>
  51. <p>So if I import for example a <i>docsbuild.xml</i> file named <b>builddocs</b>,
  52. that contains a &quot;<b>docs</b>&quot; target, I can redefine it in my main
  53. buildfile and that is the one that will be called. This makes it easy to
  54. keep the same target name, so that the overriding target is still called
  55. by any other targets--in either the main or imported buildfile(s)--for which
  56. it is a dependency, with a different implementation. The target from <i>docsbuild.xml</i> is
  57. made available by the name &quot;<b>builddocs</b><b>.docs</b>&quot;.
  58. This enables the new implementation to call the old target, thus
  59. <i>enhancing</i> it with tasks called before or after it.</p>
  60. <p>If you use the <i>as</i> attribute of the task, its value will be
  61. used to prefix the overriden target's name instead of the name
  62. attribute of the project tag.</p>
  63. <h4>Special Properties</h4>
  64. <p>Imported files are treated as they are present in the main
  65. buildfile. This makes it easy to understand, but it makes it impossible
  66. for them to reference files and resources relative to their path.
  67. Because of this, for every imported file, Ant adds a property that
  68. contains the path to the imported buildfile. With this path, the
  69. imported buildfile can keep resources and be able to reference them
  70. relative to its position.</p>
  71. <p>So if I import for example a <i>docsbuild.xml</i> file named <b>builddocs</b>,
  72. I can get its path as <b>ant.file.builddocs</b>, similarly to the <b>ant.file</b>
  73. property of the main buildfile.</p>
  74. <p>Note that &quot;builddocs&quot; is not the filename, but the name attribute
  75. present in the imported project tag.</p>
  76. <p>
  77. If import file does not have a name attribute, the ant.file.projectname
  78. property will not be set.
  79. </p>
  80. <h4>Resolving files against the imported file</h4>
  81. <p>Suppose your main build file called <code>importing.xml</code>
  82. imports a build file <code>imported.xml</code>, located anywhere on
  83. the file system, and <code>imported.xml</code> reads a set of
  84. properties from <code>imported.properties</code>:</p>
  85. <pre>&lt;!-- importing.xml --&gt;
  86. &lt;project name="importing" basedir="." default="..."&gt;
  87. &nbsp; &lt;import file="${path_to_imported}/imported.xml"/&gt;
  88. &lt;/project&gt;
  89. &lt;!-- imported.xml --&gt;
  90. &lt;project name="imported" basedir="." default="..."&gt;
  91. &nbsp; &lt;property file="imported.properties"/&gt;
  92. &lt;/project&gt;
  93. </pre>
  94. <p>This snippet however will resolve <code>imported.properties</code>
  95. against the basedir of <code>importing.xml</code>, because the basedir
  96. of <code>imported.xml</code> is ignored by Ant. The right way to use
  97. <code>imported.properties</code> is:</p>
  98. <pre>
  99. &lt;!-- imported.xml --&gt;
  100. &lt;project name="imported" basedir="." default="..."&gt;
  101. &nbsp; &lt;dirname property="imported.basedir" file="${ant.file.imported}"/&gt;
  102. &nbsp; &lt;property file="${imported.basedir}/imported.properties"/&gt;
  103. &lt;/project&gt;
  104. </pre>
  105. <p>As explained above <code>${ant.file.imported}</code> stores the
  106. path of the build script, that defines the project called
  107. <code>imported</code>, (in short it stores the path to
  108. <code>imported.xml</code>) and <a
  109. href="dirname.html"><code>&lt;dirname&gt;</code></a> takes its
  110. directory. This technique also allows <code>imported.xml</code> to be
  111. used as a standalone file (without being imported in other
  112. project).</p>
  113. <h3>Parameters</h3>
  114. <table border="1" cellpadding="2" cellspacing="0">
  115. <tbody>
  116. <tr>
  117. <td valign="top"><b>Attribute</b></td>
  118. <td valign="top"><b>Description</b></td>
  119. <td align="center" valign="top"><b>Required</b></td>
  120. </tr>
  121. <tr>
  122. <td valign="top">
  123. file
  124. </td>
  125. <td valign="top">
  126. The file to import. If this is a relative file name, the file name will be resolved
  127. relative to the <i>importing</i> file. <b>Note</b>, this is unlike most other
  128. ant file attributes, where relative files are resolved relative to ${basedir}.
  129. </td>
  130. <td valign="top" align="center">Yes</td>
  131. </tr>
  132. <tr>
  133. <td valign="top">
  134. optional
  135. </td>
  136. <td valign="top">
  137. If true, do not stop the build if the file does not exist,
  138. default is false.
  139. </td>
  140. <td valign="top" align="center">No</td>
  141. </tr>
  142. <tr>
  143. <td valign="top">
  144. as
  145. </td>
  146. <td valign="top">
  147. Specifies the prefix prepended to the target names. If
  148. ommitted, the name attribute of the project tag of the
  149. imported file will be used.
  150. </td>
  151. <td valign="top" align="center">No</td>
  152. </tr>
  153. </tbody>
  154. </table>
  155. <h3>Examples</h3>
  156. <pre>&nbsp; &lt;import file=&quot;../common-targets.xml&quot;/&gt;
  157. </pre>
  158. <p>Imports targets from the common-targets.xml file that is in a parent
  159. directory.</p>
  160. <pre>&nbsp; &lt;import file=&quot;${deploy-platform}.xml&quot;/&gt;
  161. </pre>
  162. <p>Imports the project defined by the property deploy-platform</p>
  163. </body>
  164. </html>