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 KiB

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 <em>depends on</em> the compile target.</p>
  31. <p>Ant tries to execute the targets in the <var>depends</var>
  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. <var>depends</var> 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 &rarr; B &rarr; C &rarr; 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 <strong>not</strong> 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 <em>sense</em> this property, you should add
  61. the <var>if</var> (or <var>unless</var>) 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. <q>false</q>), 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 property name can be specified in
  77. the <var>if</var>/<var>unless</var> attribute. If you want to
  78. check multiple conditions, you can use a dependent target for
  79. computing the result for the check:</p>
  80. <pre>
  81. &lt;target name="myTarget" depends="myTarget.check" if="myTarget.run"&gt;
  82. &lt;echo&gt;Files foo.txt and bar.txt are present.&lt;/echo&gt;
  83. &lt/target&gt;
  84. &lt;target name="myTarget.check"&gt;
  85. &lt;condition property="myTarget.run"&gt;
  86. &lt;and&gt;
  87. &lt;available file="foo.txt"/&gt;
  88. &lt;available file="bar.txt"/&gt;
  89. &lt;/and&gt;
  90. &lt;/condition&gt;
  91. &lt/target&gt;</pre>
  92. <pre><b>Call-Graph:</b> myTarget.check &rarr; maybe(myTarget)</pre>
  93. <p>If no <var>if</var> and no <var>unless</var> attribute is
  94. present, the target will always be executed.</p>
  95. <p><strong>Important</strong>: the <var>if</var> and <var>unless</var>
  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 <var>description</var> attribute can be used to
  102. provide a one-line description of this target, which is printed by
  103. the <kbd>-projecthelp</kbd> command-line option. Targets without
  104. such a description are deemed internal and will not be listed,
  105. unless either the <kbd>-verbose</kbd> or <kbd>-debug</kbd>
  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 <em>initialization</em> 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;</pre>
  122. <p>Especially if you only have a few tasks you also could place these
  123. tasks directly under the project tag (<em>since Ant 1.6.0</em>):</p>
  124. <pre>
  125. &lt;project&gt;
  126. &lt;tstamp/&gt;
  127. &lt;/project&gt;</pre>
  128. <p>If the <var>depends</var> attribute and
  129. the <var>if</var>/<var>unless</var> attribute are set,
  130. the <var>depends</var> attribute is executed first.</p>
  131. <p>A target has the following attributes:</p>
  132. <table class="attr">
  133. <tr>
  134. <th scope="col">Attribute</th>
  135. <th scope="col">Description</th>
  136. <th scope="col">Required</th>
  137. </tr>
  138. <tr>
  139. <td>name</td>
  140. <td>the name of the target.</td>
  141. <td>Yes</td>
  142. </tr>
  143. <tr>
  144. <td>depends</td>
  145. <td>a comma-separated list of names of targets on
  146. which this target depends.</td>
  147. <td>No</td>
  148. </tr>
  149. <tr>
  150. <td>if</td>
  151. <td>the name of the property that must be set in
  152. order for this target to execute,
  153. or <a href="properties.html#if+unless">something evaluating to
  154. <q>true</q></a>.</td>
  155. <td>No</td>
  156. </tr>
  157. <tr>
  158. <td>unless</td>
  159. <td>the name of the property that must not be set
  160. in order for this target to execute,
  161. or <a href="properties.html#if+unless">something evaluating to
  162. <q>false</q></a>.</td>
  163. <td>No</td>
  164. </tr>
  165. <tr>
  166. <td>description</td>
  167. <td>a short description of this target's function.</td>
  168. <td>No</td>
  169. </tr>
  170. <tr>
  171. <td>extensionOf</td>
  172. <td>Adds the current target to the depends list of
  173. the named <a href="#extension-points">extension-point</a>.
  174. <em>since Ant 1.8.0</em>.</td>
  175. <td>No</td>
  176. </tr>
  177. <tr>
  178. <td>onMissingExtensionPoint</td>
  179. <td>What to do if this target tries to extend a
  180. missing
  181. <a href="#extension-points">extension-point</a>. (<q>fail</q>,
  182. <q>warn</q>, <q>ignore</q>).
  183. <em>since Ant 1.8.2</em>.</td>
  184. <td>No; not allowed unless
  185. <var>extensionOf</var> is present, defaults to <q>fail</q>.
  186. </td>
  187. </tr>
  188. </table>
  189. <p>A target name can be any alphanumeric string valid in the
  190. encoding of the XML file. The empty string <q></q> is in this set,
  191. as is comma <q>,</q> and space <q>&nbsp;</q>. Please avoid using
  192. these, as they will not be supported in future Ant versions
  193. because of all the confusion they cause on command line and
  194. IDE. IDE support of unusual target names, or any target name
  195. containing spaces, varies with the IDE.</p>
  196. <p>Targets beginning with a hyphen such as <q>-restart</q> are
  197. valid, and can be used to name targets that should not be called
  198. directly from the command line.<br/>
  199. For Ant's main class every option starting with hyphen is an
  200. option for Ant itself and not a target. For that reason calling
  201. these targets from command line is not possible. On the other
  202. hand, IDEs usually don't use Ant's main class as entry point and
  203. calling them from the IDE is usually possible.</p>
  204. <h1 id="extension-points">Extension-Points</h1>
  205. <p><em>Since Ant 1.8.0</em>.</p>
  206. <p>Extension-Points are similar to targets in that they have a name
  207. and a <var>depends</var> list and can be executed from the command
  208. line. Just like targets they represent a state during the build
  209. process.</p>
  210. <p>Unlike targets they don't contain any tasks, their main purpose
  211. is to collect targets that contribute to the desired state in
  212. their <var>depends</var> list.</p>
  213. <p>Targets can add themselves to an
  214. extension-point's <var>depends</var> list via
  215. their <var>extensionOf</var> attribute. The targets that add
  216. themselves will be added after the targets of the
  217. explicit <var>depends</var> attribute of the extension-point, if
  218. multiple targets add themselves, their relative order is not
  219. defined.</p>
  220. <p>The main purpose of an extension-point is to act as an extension
  221. point for build files designed to
  222. be <a href="Tasks/import.html">imported</a>. In the imported
  223. file, an extension-point defines a state that must be reached and
  224. targets from other build files can join the <var>depends</var>
  225. list of said extension-point in order to contribute to that
  226. state.</p>
  227. <p>For example your imported build file may need to compile code, it
  228. might look like:</p>
  229. <pre>
  230. &lt;target name="create-directory-layout"&gt;
  231. ...
  232. &lt;/target&gt;
  233. &lt;extension-point name="ready-to-compile"
  234. depends="create-directory-layout"/&gt;
  235. &lt;target name="compile" depends="ready-to-compile"&gt;
  236. ...
  237. &lt;/target&gt;</pre>
  238. <pre><b>Call-Graph:</b> create-directory-layout &rarr; 'empty slot' &rarr; compile</pre>
  239. <p>And you need to generate some source before compilation, then in
  240. your main build file you may use something like</p>
  241. <pre>
  242. &lt;target name="generate-sources"
  243. extensionOf="ready-to-compile"&gt;
  244. ...
  245. &lt;/target&gt;</pre>
  246. <pre><b>Call-Graph:</b> create-directory-layout &rarr; generate-sources &rarr; compile</pre>
  247. <p>This will ensure that the <q>generate-sources</q> target is
  248. executed before the <q>compile</q> target.</p>
  249. <p>Don't rely on the order of the depends list,
  250. if <q>generate-sources</q> depends
  251. on <q>create-directory-layout</q> then it must explicitly depend
  252. on it via its own depends attribute.</p>
  253. </body>
  254. </html>