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

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