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.3 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. <h4>Special Properties</h4>
  61. <p>Imported files are treated as they are present in the main
  62. buildfile. This makes it easy to understand, but it makes it impossible
  63. for them to reference files and resources relative to their path.
  64. Because of this, for every imported file, Ant adds a property that
  65. contains the path to the imported buildfile. With this path, the
  66. imported buildfile can keep resources and be able to reference them
  67. relative to its position.</p>
  68. <p>So if I import for example a <i>docsbuild.xml</i> file named <b>builddocs</b>,
  69. I can get its path as <b>ant.file.builddocs</b>, similarly to the <b>ant.file</b>
  70. property of the main buildfile.</p>
  71. <p>Note that &quot;builddocs&quot; is not the filename, but the name attribute
  72. present in the imported project tag.</p>
  73. <p>
  74. If import file does not have a name attribute, the ant.file.projectname
  75. property will not be set.
  76. </p>
  77. <h4>Resolving files against the imported file</h4>
  78. <p>Suppose your main build file called <code>importing.xml</code>
  79. imports a build file <code>imported.xml</code>, located anywhere on
  80. the file system, and <code>imported.xml</code> reads a set of
  81. properties from <code>imported.properties</code>:</p>
  82. <pre>&lt;!-- importing.xml --&gt;
  83. &lt;project name="importing" basedir="." default="..."&gt;
  84. &nbsp; &lt;import file="${path_to_imported}/imported.xml"/&gt;
  85. &lt;/project&gt;
  86. &lt;!-- imported.xml --&gt;
  87. &lt;project name="imported" basedir="." default="..."&gt;
  88. &nbsp; &lt;property file="imported.properties"/&gt;
  89. &lt;/project&gt;
  90. </pre>
  91. <p>This snippet however will resolve <code>imported.properties</code>
  92. against the basedir of <code>importing.xml</code>, because the basedir
  93. of <code>imported.xml</code> is ignored by Ant. The right way to use
  94. <code>imported.properties</code> is:</p>
  95. <pre>
  96. &lt;!-- imported.xml --&gt;
  97. &lt;project name="imported" basedir="." default="..."&gt;
  98. &nbsp; &lt;dirname property="imported.basedir" file="${ant.file.imported}"/&gt;
  99. &nbsp; &lt;property file="${imported.basedir}/imported.properties"/&gt;
  100. &lt;/project&gt;
  101. </pre>
  102. <p>As explained above <code>${ant.file.imported}</code> stores the
  103. path of the build script, that defines the project called
  104. <code>imported</code>, (in short it stores the path to
  105. <code>imported.xml</code>) and <a
  106. href="dirname.html"><code>&lt;dirname&gt;</code></a> takes its
  107. directory. This technique also allows <code>imported.xml</code> to be
  108. used as a standalone file (without being imported in other
  109. project).</p>
  110. <h3>Parameters</h3>
  111. <table border="1" cellpadding="2" cellspacing="0">
  112. <tbody>
  113. <tr>
  114. <td valign="top"><b>Attribute</b></td>
  115. <td valign="top"><b>Description</b></td>
  116. <td align="center" valign="top"><b>Required</b></td>
  117. </tr>
  118. <tr>
  119. <td valign="top">
  120. file
  121. </td>
  122. <td valign="top">
  123. The file to import. If this is a relative file name, the file name will be resolved
  124. relative to the <i>importing</i> file. <b>Note</b>, this is unlike most other
  125. ant file attributes, where relative files are resolved relative to ${basedir}.
  126. </td>
  127. <td valign="top" align="center">Yes</td>
  128. </tr>
  129. <tr>
  130. <td valign="top">
  131. optional
  132. </td>
  133. <td valign="top">
  134. If true, do not stop the build if the file does not exist,
  135. default is false.
  136. </td>
  137. <td valign="top" align="center">No</td>
  138. </tr>
  139. </tbody>
  140. </table>
  141. <h3>Examples</h3>
  142. <pre>&nbsp; &lt;import file=&quot;../common-targets.xml&quot;/&gt;
  143. </pre>
  144. <p>Imports targets from the common-targets.xml file that is in a parent
  145. directory.</p>
  146. <pre>&nbsp; &lt;import file=&quot;${deploy-platform}.xml&quot;/&gt;
  147. </pre>
  148. <p>Imports the project defined by the property deploy-platform</p>
  149. </body>
  150. </html>