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 11 kB

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