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

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