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

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