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

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