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.

targets.html 12 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. <html>
  16. <head>
  17. <meta http-equiv="Content-Language" content="en-us"/>
  18. <link rel="stylesheet" type="text/css" href="stylesheets/style.css"/>
  19. <title>Targets and Extension-Points</title>
  20. </head>
  21. <body>
  22. <h1 id="targets">Targets</h1>
  23. <p>A target is a container of tasks that cooperate to reach a
  24. desired state during the build process.</p>
  25. <p>Targets can depend on other targets and Apache Ant ensures that these
  26. other targets have been executed before the current target. For
  27. example you might have a target for compiling and a
  28. target for creating a distributable. You can only build a
  29. distributable when you have compiled first, so the distribute
  30. target <i>depends on</i> the compile target.</p>
  31. <p>Ant tries to execute the targets in the <code>depends</code>
  32. attribute in the order they appear (from left to right). Keep in
  33. mind that it is possible that a target can get executed earlier
  34. when an earlier target depends on it:</p>
  35. <pre>&lt;target name=&quot;A&quot;/&gt;
  36. &lt;target name=&quot;B&quot; depends=&quot;A&quot;/&gt;
  37. &lt;target name=&quot;C&quot; depends=&quot;B&quot;/&gt;
  38. &lt;target name=&quot;D&quot; depends=&quot;C,B,A&quot;/&gt;</pre>
  39. <p>Suppose we want to execute target D. From its
  40. <code>depends</code> attribute, you might think that first target
  41. C, then B and then A is executed. Wrong! C depends on B, and B
  42. depends on A, so first A is executed, then B, then C, and finally
  43. D.</p>
  44. <pre><b>Call-Graph:</b> A --> B --> C --> D</pre>
  45. <p>In a chain of dependencies stretching back from a given target
  46. such as D above, each target gets executed only once, even when
  47. more than one target depends on it. Thus, executing the D target
  48. will first result in C being called, which in turn will first call
  49. B, which in turn will first call A. After A, then B, then C have
  50. executed, execution returns to the dependency list of D, which
  51. will <u>not</u> call B and A, since they were already called in
  52. process of dependency resolution for C and B respectively as
  53. dependencies of D. Had no such dependencies been discovered in
  54. processing C and B, B and A would have been executed after C in
  55. processing D's dependency list.</p>
  56. <p>A target also has the ability to perform its execution if (or
  57. unless) a property has been set. This allows, for example, better
  58. control on the building process depending on the state of the
  59. system (java version, OS, command-line property defines, etc.).
  60. To make a target <i>sense</i> this property, you should add
  61. the <code>if</code> (or <code>unless</code>) attribute with the
  62. name of the property that the target should react
  63. to. <strong>Note:</strong> In the most simple case Ant will only
  64. check whether the property has been set, the value doesn't matter,
  65. but using property expansions you can build more complex
  66. conditions. See
  67. <a href="properties.html#if+unless">the properties page</a> for
  68. more details. For example:</p>
  69. <pre>&lt;target name=&quot;build-module-A&quot; if=&quot;module-A-present&quot;/&gt;</pre>
  70. <pre>&lt;target name=&quot;build-own-fake-module-A&quot; unless=&quot;module-A-present&quot;/&gt;</pre>
  71. <p>In the first example, if the <code>module-A-present</code>
  72. property is set (to any value, e.g. <i>false</i>), the target will
  73. be run. In the second example, if
  74. the <code>module-A-present</code> property is set (again, to any
  75. value), the target will not be run.</p>
  76. <p>Only one propertyname can be specified in the if/unless
  77. clause. If you want to check multiple conditions, you can use a
  78. dependent target for computing the result for the check:</p>
  79. <pre>
  80. &lt;target name="myTarget" depends="myTarget.check" if="myTarget.run"&gt;
  81. &lt;echo&gt;Files foo.txt and bar.txt are present.&lt;/echo&gt;
  82. &lt/target&gt;
  83. &lt;target name="myTarget.check"&gt;
  84. &lt;condition property="myTarget.run"&gt;
  85. &lt;and&gt;
  86. &lt;available file="foo.txt"/&gt;
  87. &lt;available file="bar.txt"/&gt;
  88. &lt;/and&gt;
  89. &lt;/condition&gt;
  90. &lt/target&gt;
  91. </pre>
  92. <pre><b>Call-Graph:</b> myTarget.check --> maybe(myTarget)</pre>
  93. <p>If no <code>if</code> and no <code>unless</code> attribute is
  94. present, the target will always be executed.</p>
  95. <p><b>Important</b>: the <code>if</code> and <code>unless</code>
  96. attributes only enable or disable the target to which they are
  97. attached. They do not control whether or not targets that a
  98. conditional target depends upon get executed. In fact, they do
  99. not even get evaluated until the target is about to be executed,
  100. and all its predecessors have already run.
  101. <p>The optional <code>description</code> attribute can be used to
  102. provide a one-line description of this target, which is printed by
  103. the <code>-projecthelp</code> command-line option. Targets without
  104. such a description are deemed internal and will not be listed,
  105. unless either the <code>-verbose</code> or <code>-debug</code>
  106. option is used.</p>
  107. <p>It is a good practice to place
  108. your <a href="Tasks/tstamp.html">tstamp</a> tasks in a
  109. so-called <i>initialization</i> target, on which all other targets
  110. depend. Make sure that target is always the first one in the
  111. depends list of the other targets. In this manual, most
  112. initialization targets have the name <code>&quot;init&quot;</code>.</p>
  113. <pre>
  114. &lt;project&gt;
  115. &lt;target name=&quot;init&quot;&gt;
  116. &lt;tstamp/&gt;
  117. &lt;/target&gt;
  118. &lt;target name=&quot;otherTarget&quot; depends=&quot;init&quot;&gt;
  119. ...
  120. &lt;/target&gt;
  121. &lt;/project&gt;
  122. </pre>
  123. <p>Especially if you only have a few tasks you also could place these
  124. tasks directly under the project tag (since Ant 1.6.0):</p>
  125. <pre>
  126. &lt;project&gt;
  127. &lt;tstamp/&gt;
  128. &lt;/project&gt;
  129. </pre>
  130. <p>If the depends attribute and the if/unless attribute are set, the
  131. depends attribute is executed first.</p>
  132. <p>A target has the following attributes:</p>
  133. <table>
  134. <tr>
  135. <td valign="top"><b>Attribute</b></td>
  136. <td valign="top"><b>Description</b></td>
  137. <td align="center" valign="top"><b>Required</b></td>
  138. </tr>
  139. <tr>
  140. <td valign="top">name</td>
  141. <td valign="top">the name of the target.</td>
  142. <td align="center" valign="top">Yes</td>
  143. </tr>
  144. <tr>
  145. <td valign="top">depends</td>
  146. <td valign="top">a comma-separated list of names of targets on
  147. which this target depends.</td>
  148. <td align="center" valign="top">No</td>
  149. </tr>
  150. <tr>
  151. <td valign="top">if</td>
  152. <td valign="top">the name of the property that must be set in
  153. order for this target to execute,
  154. or <a href="properties.html#if+unless">something evaluating to
  155. true</a>.</td>
  156. <td align="center" valign="top">No</td>
  157. </tr>
  158. <tr>
  159. <td valign="top">unless</td>
  160. <td valign="top">the name of the property that must not be set
  161. in order for this target to execute,
  162. or <a href="properties.html#if+unless">something evaluating to
  163. false</a>.</td>
  164. <td align="center" valign="top">No</td>
  165. </tr>
  166. <tr>
  167. <td valign="top">description</td>
  168. <td valign="top">a short description of this target's function.</td>
  169. <td align="center" valign="top">No</td>
  170. </tr>
  171. <tr>
  172. <td valign="top">extensionOf</td>
  173. <td valign="top">Adds the current target to the depends list of
  174. the named <a href="#extension-points">extension-point</a>.
  175. <em>since Ant 1.8.0.</em></td>
  176. <td align="center" valign="top">No</td>
  177. </tr>
  178. <tr>
  179. <td valign="top">onMissingExtensionPoint</td>
  180. <td valign="top">What to do if this target tries to extend a
  181. missing
  182. <a href="#extension-points">extension-point</a>. ("fail",
  183. "warn", "ignore").
  184. <em>since Ant 1.8.2.</em></td>
  185. <td align="center" valign="top">No. Not allowed unless
  186. <code>extensionOf</code> is present. Defaults to <code>fail</code>.
  187. </td>
  188. </tr>
  189. </table>
  190. <p>A target name can be any alphanumeric string valid in the
  191. encoding of the XML file. The empty string &quot;&quot; is in this
  192. set, as is comma &quot;,&quot; and space &quot; &quot;. Please
  193. avoid using these, as they will not be supported in future Ant
  194. versions because of all the confusion they cause on command line and IDE. IDE support of
  195. unusual target names, or any target name containing spaces, varies
  196. with the IDE.</p>
  197. <p>Targets beginning with a hyphen such
  198. as <code>&quot;-restart&quot;</code> are valid, and can be used to
  199. name targets that should not be called directly from the command
  200. line. <br>
  201. For Ants main class every option starting with hyphen is an
  202. option for Ant itself and not a target. For that reason calling these
  203. target from command line is not possible. On the other hand IDEs usually
  204. don't use Ants main class as entry point and calling them from the IDE
  205. is usually possible.</p>
  206. <h1 id="extension-points">Extension-Points</h1>
  207. <p><em>since Ant 1.8.0.</em></p>
  208. <p>Extension-Points are similar to targets in that they have a name and
  209. a depends list and can be executed from the command line. Just
  210. like targets they represent a state during the build process.</p>
  211. <p>Unlike targets they don't contain any tasks, their main purpose
  212. is to collect targets that contribute to the desired state in
  213. their depends list.</p>
  214. <p>Targets can add themselves to an extension-point's depends list via
  215. their extensionOf attribute. The targets that add themselves will be
  216. added after the targets of the explicit depends attribute of the
  217. extension-point, if multiple targets add themselves, their relative
  218. order is not defined.</p>
  219. <p>The main purpose of an extension-point is to act as an extension
  220. point for build files designed to
  221. be <a href="Tasks/import.html">imported</a>. In the imported
  222. file, an extension-point defines a state that must be reached and
  223. targets from other build files can join the depends list of said
  224. extension-point in order to contribute to that state.</p>
  225. <p>For example your imported build file may need to compile code, it
  226. might look like:</p>
  227. <pre>
  228. &lt;target name="create-directory-layout"&gt;
  229. ...
  230. &lt;/target&gt;
  231. &lt;extension-point name="ready-to-compile"
  232. depends="create-directory-layout"/&gt;
  233. &lt;target name="compile" depends="ready-to-compile"&gt;
  234. ...
  235. &lt;/target&gt;
  236. </pre>
  237. <pre><b>Call-Graph:</b> create-directory-layout --> 'empty slot' --> compile</pre>
  238. <p>And you need to generate some source before compilation, then in
  239. your main build file you may use something like</p>
  240. <pre>
  241. &lt;target name="generate-sources"
  242. extensionOf="ready-to-compile"&gt;
  243. ...
  244. &lt;/target&gt;
  245. </pre>
  246. <pre><b>Call-Graph:</b> create-directory-layout --> generate-sources --> compile</pre>
  247. <p>This will ensure that the <em>generate-sources</em> target is
  248. executed before the <em>compile</em> target.</p>
  249. <p>Don't rely on the order of the depends list,
  250. if <em>generate-sources</em> depends
  251. on <em>create-directory-layout</em> then it must explicitly depend
  252. on it via its own depends attribute.</p>
  253. </body>
  254. </html>