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.

using.html 40 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889
  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>Writing a Simple Buildfile</title>
  20. </head>
  21. <body>
  22. <h1>Using Ant</h1>
  23. <h2><a name="buildfile">Writing a Simple Buildfile</a></h2>
  24. <p>Ant's buildfiles are written in XML. Each buildfile contains one project
  25. and at least one (default) target. Targets contain task elements.
  26. Each task element of the buildfile can have an <code>id</code> attribute and
  27. can later be referred to by the value supplied to this. The value has
  28. to be unique. (For additional information, see the
  29. <a href="#tasks"> Tasks</a> section below.)</p>
  30. <h3><a name="projects">Projects</a></h3>
  31. <p>A <i>project</i> has three attributes:</p>
  32. <table border="1" cellpadding="2" cellspacing="0">
  33. <tr>
  34. <td valign="top"><b>Attribute</b></td>
  35. <td valign="top"><b>Description</b></td>
  36. <td align="center" valign="top"><b>Required</b></td>
  37. </tr>
  38. <tr>
  39. <td valign="top">name</td>
  40. <td valign="top">the name of the project.</td>
  41. <td align="center" valign="top">No</td>
  42. </tr>
  43. <tr>
  44. <td valign="top">default</td>
  45. <td valign="top">the default target to use when no target is supplied.</td>
  46. <td align="center" valign="top">No; however, <b>since Ant 1.6.0</b>,
  47. every project includes an implicit target that contains any and
  48. all top-level tasks and/or types. This target will always be
  49. executed as part of the project's initialization, even when Ant is
  50. run with the <a href="running.html#options">-projecthelp</a> option.
  51. </td>
  52. </tr>
  53. <tr>
  54. <td valign="top">basedir</td>
  55. <td valign="top">the base directory from which all path calculations are
  56. done. This attribute might be overridden by setting
  57. the &quot;basedir&quot;
  58. property beforehand. When this is done, it must be omitted in the
  59. project tag. If neither the attribute nor the property have
  60. been set, the parent directory of the buildfile will be used.</td>
  61. <td align="center" valign="top">No</td>
  62. </tr>
  63. </table>
  64. <p>Optionally, a description for the project can be provided as a
  65. top-level <code>&lt;description&gt;</code> element (see the <a
  66. href="CoreTypes/description.html">description</a> type).</p>
  67. <p>Each project defines one or more <i>targets</i>.
  68. A target is a set of <i>tasks</i> you want
  69. to be executed. When starting Ant, you can select which target(s) you
  70. want to have executed. When no target is given,
  71. the project's default is used.</p>
  72. <h3><a name="targets">Targets</a></h3>
  73. <p>A target can depend on other targets. You might have a target for compiling,
  74. for example, and a target for creating a distributable. You can only build a
  75. distributable when you have compiled first, so the distribute target
  76. <i>depends on</i> the compile target. Ant resolves these dependencies.</p>
  77. <p>It should be noted, however, that Ant's <code>depends</code> attribute
  78. only specifies the <i>order</i> in which targets should be executed - it
  79. does not affect whether the target that specifies the dependency(s) gets
  80. executed if the dependent target(s) did not (need to) run.
  81. </p>
  82. <p>Ant tries to execute the targets in the <code>depends</code>
  83. attribute in the order
  84. they appear (from left to right). Keep in mind that it is possible that a target
  85. can get executed earlier when an earlier target depends on it:</p>
  86. <blockquote>
  87. <pre>&lt;target name=&quot;A&quot;/&gt;
  88. &lt;target name=&quot;B&quot; depends=&quot;A&quot;/&gt;
  89. &lt;target name=&quot;C&quot; depends=&quot;B&quot;/&gt;
  90. &lt;target name=&quot;D&quot; depends=&quot;C,B,A&quot;/&gt;</pre>
  91. </blockquote>
  92. <p>Suppose we want to execute target D. From its
  93. <code>depends</code> attribute, you
  94. might think that first target C, then B and then A is executed.
  95. Wrong! C depends on B, and B depends on A, so first A is executed, then B, then C, and finally D.</p>
  96. <p>In a chain of dependencies stretching back from a given target such
  97. as D above, each target gets executed only once, even when more than
  98. one target depends on it. Thus, executing the D target will first
  99. result in C being called, which in turn will first call B, which in
  100. turn will first call A. After A, then B, then C have executed,
  101. execution returns to the dependency list of D, which will <u>not</u>
  102. call B and A, since they were already called in process of dependency
  103. resolution for C and B respectively as dependencies of D. Had no such
  104. dependencies been discovered in processing C and B, B and A would
  105. have been executed after C in processing D's dependency list.</p>
  106. <p>A target also has the ability to perform its execution if (or
  107. unless) a property has been set. This allows, for example, better
  108. control on the building process depending on the state of the system
  109. (java version, OS, command-line property defines, etc.). To make a target
  110. <i>sense</i> this property, you should add the <code>if</code> (or
  111. <code>unless</code>) attribute with the name of the property that the target
  112. should react to. <strong>Note:</strong> Ant will only check whether
  113. the property has been set, the value doesn't matter. A property set
  114. to the empty string is still an existing property. For example:</p>
  115. <blockquote>
  116. <pre>&lt;target name=&quot;build-module-A&quot; if=&quot;module-A-present&quot;/&gt;</pre>
  117. <pre>&lt;target name=&quot;build-own-fake-module-A&quot; unless=&quot;module-A-present&quot;/&gt;</pre>
  118. </blockquote>
  119. <p>In the first example, if the <code>module-A-present</code>
  120. property is set (to any value, e.g. <i>false</i>), the target will be run. In the second
  121. example, if the <code>module-A-present</code> property is set
  122. (again, to any value), the target will not be run.
  123. </p>
  124. <p>Only one propertyname can be specified in the if/unless clause. If you want to check
  125. multiple conditions, you can use a dependend target for computing the result for the check:</p>
  126. <blockquote><pre>
  127. &lt;target name="myTarget" depends="myTarget.check" if="myTarget.run"&gt;
  128. &lt;echo&gt;Files foo.txt and bar.txt are present.&lt;/echo&gt;
  129. &lt/target&gt;
  130. &lt;target name="myTarget.check"&gt;
  131. &lt;condition property="myTarget.run"&gt;
  132. &lt;and&gt;
  133. &lt;available file="foo.txt"/&gt;
  134. &lt;available file="bar.txt"/&gt;
  135. &lt;/and&gt;
  136. &lt;/condition&gt;
  137. &lt/target&gt;
  138. </pre></blockquote>
  139. <p>If no <code>if</code> and no <code>unless</code> attribute is present,
  140. the target will always be executed.</p>
  141. <p>
  142. <b>Important:</b> the <code>if</code> and <code>unless</code> attributes only
  143. enable or disable the target to which they are attached. They do not control
  144. whether or not targets that a conditional target depends upon get executed.
  145. In fact, they do not even get evaluated until the target is about to be executed,
  146. and all its predecessors have already run.
  147. <p>The optional <code>description</code> attribute can be used to provide a one-line description of this target, which is printed by the
  148. <nobr><code>-projecthelp</code></nobr> command-line option. Targets
  149. without such a description are deemed internal and will not be listed,
  150. unless either the <nobr><code>-verbose</code></nobr> or
  151. <nobr><code>-debug</code></nobr> option is used.
  152. </p>
  153. <p>It is a good practice to place your <a
  154. href="CoreTasks/tstamp.html">tstamp</a> tasks in a so-called
  155. <i>initialization</i> target, on which
  156. all other targets depend. Make sure that target is always the first one in
  157. the depends list of the other targets. In this manual, most initialization targets
  158. have the name <code>&quot;init&quot;</code>.</p>
  159. <p>If the depends attribute and the if/unless attribute are set, the depends attribute is
  160. executed first.</p>
  161. <p>A target has the following attributes:</p>
  162. <table border="1" cellpadding="2" cellspacing="0">
  163. <tr>
  164. <td valign="top"><b>Attribute</b></td>
  165. <td valign="top"><b>Description</b></td>
  166. <td align="center" valign="top"><b>Required</b></td>
  167. </tr>
  168. <tr>
  169. <td valign="top">name</td>
  170. <td valign="top">the name of the target.</td>
  171. <td align="center" valign="top">Yes</td>
  172. </tr>
  173. <tr>
  174. <td valign="top">depends</td>
  175. <td valign="top">a comma-separated list of names of targets on which this
  176. target depends.</td>
  177. <td align="center" valign="top">No</td>
  178. </tr>
  179. <tr>
  180. <td valign="top">if</td>
  181. <td valign="top">the name of the property that must be set in order for this
  182. target to execute.</td>
  183. <td align="center" valign="top">No</td>
  184. </tr>
  185. <tr>
  186. <td valign="top">unless</td>
  187. <td valign="top">the name of the property that must not be set in order
  188. for this target to execute.</td>
  189. <td align="center" valign="top">No</td>
  190. </tr>
  191. <tr>
  192. <td valign="top">description</td>
  193. <td valign="top">a short description of this target's function.</td>
  194. <td align="center" valign="top">No</td>
  195. </tr>
  196. </table>
  197. </p>
  198. <p>A target name can be any alphanumeric string valid in the encoding of the XML
  199. file. The empty string &quot;&quot; is in this set, as is
  200. comma &quot;,&quot; and space &quot; &quot;.
  201. Please avoid using these, as they will not be supported in future Ant versions
  202. because of all the confusion they cause. IDE support of unusual target names,
  203. or any target name containing spaces, varies with the IDE.</p>
  204. <p>Targets beginning with a hyphen such as <code>&quot;-restart&quot;</code>
  205. are valid, and can be used
  206. to name targets that should not be called directly from the command line.</p>
  207. <h3><a name="tasks">Tasks</a></h3>
  208. <p>A task is a piece of code that can be executed.</p>
  209. <p>A task can have multiple attributes (or arguments, if you prefer). The value
  210. of an attribute might contain references to a property. These references will be
  211. resolved before the task is executed.</p>
  212. <p>Tasks have a common structure:</p>
  213. <blockquote>
  214. <pre>&lt;<i>name</i> <i>attribute1</i>=&quot;<i>value1</i>&quot; <i>attribute2</i>=&quot;<i>value2</i>&quot; ... /&gt;</pre>
  215. </blockquote>
  216. <p>where <i>name</i> is the name of the task,
  217. <i>attributeN</i> is the attribute name, and
  218. <i>valueN</i> is the value for this attribute.</p>
  219. <p>There is a set of <a href="coretasklist.html" target="navFrame">built-in tasks</a>, along with a
  220. number of
  221. <a href="optionaltasklist.html" target="navFrame"> optional tasks</a>, but it is also very
  222. easy to <a href="develop.html#writingowntask">write your own</a>.</p>
  223. <p>All tasks share a task name attribute. The value of
  224. this attribute will be used in the logging messages generated by
  225. Ant.</p>
  226. Tasks can be assigned an <code>id</code> attribute:
  227. <blockquote>
  228. <pre>&lt;<i>taskname</i> id="<i>taskID</i>" ... /&gt;</pre>
  229. </blockquote>
  230. where <i>taskname</i> is the name of the task, and <i>taskID</i> is
  231. a unique identifier for this task.
  232. You can refer to the
  233. corresponding task object in scripts or other tasks via this name.
  234. For example, in scripts you could do:
  235. <blockquote>
  236. <pre>
  237. &lt;script ... &gt;
  238. task1.setFoo("bar");
  239. &lt;/script&gt;
  240. </pre>
  241. </blockquote>
  242. to set the <code>foo</code> attribute of this particular task instance.
  243. In another task (written in Java), you can access the instance via
  244. <code>project.getReference("task1")</code>.
  245. <p>
  246. Note<sup>1</sup>: If &quot;task1&quot; has not been run yet, then
  247. it has not been configured (ie., no attributes have been set), and if it is
  248. going to be configured later, anything you've done to the instance may
  249. be overwritten.
  250. </p>
  251. <p>
  252. Note<sup>2</sup>: Future versions of Ant will most likely <i>not</i>
  253. be backward-compatible with this behaviour, since there will likely be no
  254. task instances at all, only proxies.
  255. </p>
  256. <h3><a name="properties">Properties</a></h3>
  257. <p>A project can have a set of properties. These might be set in the buildfile
  258. by the <a href="CoreTasks/property.html">property</a> task, or might be set outside Ant. A
  259. property has a name and a value; the name is case-sensitive. Properties may be used in the value of
  260. task attributes. This is done by placing the property name between
  261. &quot;<code>${</code>&quot; and &quot;<code>}</code>&quot; in the
  262. attribute value. For example,
  263. if there is a &quot;builddir&quot; property with the value
  264. &quot;build&quot;, then this could be used in an attribute like this:
  265. <code>${builddir}/classes</code>.
  266. This is resolved at run-time as <code>build/classes</code>.</p>
  267. <p>In the event you should need to include this construct literally
  268. (i.e. without property substitutions), simply "escape" the '$' character
  269. by doubling it. To continue the previous example:
  270. <pre> &lt;echo&gt;$${builddir}=${builddir}&lt;/echo&gt;</pre>
  271. will echo this message:
  272. <pre> ${builddir}=build/classes</pre></p>
  273. <p>In order to maintain backward compatibility with older Ant releases,
  274. a single '$' character encountered apart from a property-like construct
  275. (including a matched pair of french braces) will be interpreted literally;
  276. that is, as '$'. The "correct" way to specify this literal character,
  277. however, is by using the escaping mechanism unconditionally, so that "$$"
  278. is obtained by specifying "$$$$". Mixing the two approaches yields
  279. unpredictable results, as "$$$" results in "$$".</p>
  280. <h3><a name="built-in-props">Built-in Properties</a></h3>
  281. <p>Ant provides access to all system properties as if they had been
  282. defined using a <code>&lt;property&gt;</code> task.
  283. For example, <code>${os.name}</code> expands to the
  284. name of the operating system.</p>
  285. <p>For a list of system properties see
  286. <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/System.html#getProperties()">the Javadoc of System.getProperties</a>.
  287. </p>
  288. <p>In addition, Ant has some built-in properties:</p>
  289. <pre>
  290. basedir the absolute path of the project's basedir (as set
  291. with the basedir attribute of <a href="#projects">&lt;project&gt;)</a>.
  292. ant.file the absolute path of the buildfile.
  293. ant.version the version of Ant
  294. ant.project.name the name of the project that is currently executing;
  295. it is set in the name attribute of &lt;project&gt;.
  296. ant.project.default-target
  297. the name of the currently executing project's
  298. default target; it is set via the default
  299. attribute of &lt;project&gt;.
  300. ant.project.invoked-targets
  301. a comma separated list of the targets that have
  302. been specified on the command line (the IDE,
  303. an &lt;ant&gt; task ...) when invoking the current
  304. project.
  305. ant.java.version the JVM version Ant detected; currently it can hold
  306. the values &quot;1.2&quot;, &quot;1.3&quot;,
  307. &quot;1.4&quot;, &quot;1.5&quot; and &quot;1.6&quot;.
  308. ant.core.lib the absolute path of the <code>ant.jar</code> file.
  309. </pre>
  310. <p>There is also another property, but this is set by the launcher script and therefore
  311. maybe not set inside IDEs:</p>
  312. <pre>
  313. ant.home home directory of Ant
  314. </pre>
  315. <p>The following property is only set if Ant is started via the
  316. Launcher class (which means it may not be set inside IDEs
  317. either):</p>
  318. <pre>
  319. ant.library.dir the directory that has been used to load Ant's
  320. jars from. In most cases this is ANT_HOME/lib.
  321. </pre>
  322. <a name="propertyHelper"><h3>Property Helpers</h3></a>
  323. Ant's property handling is accomplished by an instance of
  324. <code>org.apache.tools.ant.PropertyHelper</code> associated with the current Project.
  325. You can learn more about this class by examining Ant's Java API. In Ant 1.8 the
  326. PropertyHelper class was much reworked and now itself employs a number of helper
  327. classes (actually instances of the <code>org.apache.tools.ant.PropertyHelper$Delegate</code>
  328. marker interface) to take care of discrete tasks such as property setting, retrieval,
  329. parsing, etc. This makes Ant's property handling highly extensible; also of interest is the
  330. new <a href="CoreTasks/propertyhelper.html">propertyhelper</a> task used to manipulate the
  331. PropertyHelper and its delegates from the context of the Ant buildfile.
  332. <p>There are three sub-interfaces of <code>Delegate</code> that may be
  333. useful to implement.</p>
  334. <ul>
  335. <li><code>org.apache.tools.ant.property.PropertyExpander</code> is
  336. responsible for finding the property name inside a string in the
  337. first place (the default extracts <code>foo</code>
  338. from <code>${foo}</code>).
  339. <p>This is the interface you'd implement if you wanted to invent
  340. your own property syntax - or allow nested property expansions
  341. since the default implementation doesn't balance braces
  342. (see <a href="http://svn.apache.org/viewvc/ant/sandbox/antlibs/props/trunk/src/main/org/apache/ant/props/NestedPropertyExpander.java?view=log"><code>NestedPropertyExpander</code>
  343. in the "props" Antlib in Ant's sandbox</a> for an
  344. example).</p>
  345. </li>
  346. <li><code>org.apache.tools.ant.PropertyHelper$PropertyEvaluator</code>
  347. is used to expand <code>${some-string}</code> into
  348. an <code>Object</code>.
  349. <p>This is the interface you'd implement if you want to provide
  350. your own storage independent of Ant's project instance - the
  351. interface represents the reading end. An example for this would
  352. be <code>org.apache.tools.ant.property.LocalProperties</code>
  353. which implements storage
  354. for <a href="CoreTasks/local.html">local properties</a>.</p>
  355. <p>Another reason to implement this interface is if you wanted to
  356. provide your own "property protocol" like
  357. expanding <code>toString:foo</code> by looking up the project
  358. reference foo and invoking <code>toString()</code> on it (which
  359. is already implemented in Ant, see below).</p>
  360. </li>
  361. <li><code>org.apache.tools.ant.PropertyHelper$PropertySetter</code>
  362. is responsible for setting properties.
  363. <p>This is the interface you'd implement if you want to provide
  364. your own storage independent of Ant's project instance - the
  365. interface represents the reading end. An example for this would
  366. be <code>org.apache.tools.ant.property.LocalProperties</code>
  367. which implements storage
  368. for <a href="CoreTasks/local.html">local properties</a>.</p>
  369. </li>
  370. </ul>
  371. <p>The default <code>PropertyExpander</code> looks similar to:</p>
  372. <pre>
  373. public class DefaultExpander implements PropertyExpander {
  374. public String parsePropertyName(String s, ParsePosition pos,
  375. ParseNextProperty notUsed) {
  376. int index = pos.getIndex();
  377. if (s.indexOf("${", index) == index) {
  378. int end = s.indexOf('}', index);
  379. if (end < 0) {
  380. throw new BuildException("Syntax error in property: " + s);
  381. }
  382. int start = index + 2;
  383. pos.setIndex(end + 1);
  384. return s.substring(start, end);
  385. }
  386. return null;
  387. }
  388. }
  389. </pre>
  390. <p>The logic that replaces <code>${toString:some-id}</code> with the
  391. stringified representation of the object with
  392. id <code>some-id</code> inside the current build is contained in a
  393. PropertyEvaluator similar to the following code:</p>
  394. <pre>
  395. public class ToStringEvaluator implements PropertyHelper.PropertyEvaluator {
  396. private static final String prefix = "toString:";
  397. public Object evaluate(String property, PropertyHelper propertyHelper) {
  398. Object o = null;
  399. if (property.startsWith(prefix) && propertyHelper.getProject() != null) {
  400. o = propertyHelper.getProject().getReference(property.substring(prefix.length()));
  401. }
  402. return o == null ? null : o.toString();
  403. }
  404. }
  405. </pre>
  406. <a name="example"><h3>Example Buildfile</h3></a>
  407. <pre>
  408. &lt;project name=&quot;MyProject&quot; default=&quot;dist&quot; basedir=&quot;.&quot;&gt;
  409. &lt;description&gt;
  410. simple example build file
  411. &lt;/description&gt;
  412. &lt;!-- set global properties for this build --&gt;
  413. &lt;property name=&quot;src&quot; location=&quot;src&quot;/&gt;
  414. &lt;property name=&quot;build&quot; location=&quot;build&quot;/&gt;
  415. &lt;property name=&quot;dist&quot; location=&quot;dist&quot;/&gt;
  416. &lt;target name=&quot;init&quot;&gt;
  417. &lt;!-- Create the time stamp --&gt;
  418. &lt;tstamp/&gt;
  419. &lt;!-- Create the build directory structure used by compile --&gt;
  420. &lt;mkdir dir=&quot;${build}&quot;/&gt;
  421. &lt;/target&gt;
  422. &lt;target name=&quot;compile&quot; depends=&quot;init&quot;
  423. description=&quot;compile the source &quot; &gt;
  424. &lt;!-- Compile the java code from ${src} into ${build} --&gt;
  425. &lt;javac srcdir=&quot;${src}&quot; destdir=&quot;${build}&quot;/&gt;
  426. &lt;/target&gt;
  427. &lt;target name=&quot;dist&quot; depends=&quot;compile&quot;
  428. description=&quot;generate the distribution&quot; &gt;
  429. &lt;!-- Create the distribution directory --&gt;
  430. &lt;mkdir dir=&quot;${dist}/lib&quot;/&gt;
  431. &lt;!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file --&gt;
  432. &lt;jar jarfile=&quot;${dist}/lib/MyProject-${DSTAMP}.jar&quot; basedir=&quot;${build}&quot;/&gt;
  433. &lt;/target&gt;
  434. &lt;target name=&quot;clean&quot;
  435. description=&quot;clean up&quot; &gt;
  436. &lt;!-- Delete the ${build} and ${dist} directory trees --&gt;
  437. &lt;delete dir=&quot;${build}&quot;/&gt;
  438. &lt;delete dir=&quot;${dist}&quot;/&gt;
  439. &lt;/target&gt;
  440. &lt;/project&gt;
  441. </pre>
  442. <p>Notice that we are declaring properties outside any target. As of
  443. Ant 1.6 all tasks can be declared outside targets (earlier version
  444. only allowed <tt>&lt;property&gt;</tt>,<tt>&lt;typedef&gt;</tt> and
  445. <tt>&lt;taskdef&gt;</tt>). When you do this they are evaluated before
  446. any targets are executed. Some tasks will generate build failures if
  447. they are used outside of targets as they may cause infinite loops
  448. otherwise (<code>&lt;antcall&gt;</code> for example).</p>
  449. <p>
  450. We have given some targets descriptions; this causes the <tt>projecthelp</tt>
  451. invocation option to list them as public targets with the descriptions; the
  452. other target is internal and not listed.
  453. <p>
  454. Finally, for this target to work the source in the <tt>src</tt> subdirectory
  455. should be stored in a directory tree which matches the package names. Check the
  456. <tt>&lt;javac&gt;</tt> task for details.
  457. <a name="filters"><h3>Token Filters</h3></a>
  458. <p>A project can have a set of tokens that might be automatically expanded if
  459. found when a file is copied, when the filtering-copy behavior is selected in the
  460. tasks that support this. These might be set in the buildfile
  461. by the <a href="CoreTasks/filter.html">filter</a> task.</p>
  462. <p>Since this can potentially be a very harmful behavior,
  463. the tokens in the files <b>must</b>
  464. be of the form <code>@</code><i>token</i><code>@</code>, where
  465. <i>token</i> is the token name that is set
  466. in the <code>&lt;filter&gt;</code> task. This token syntax matches the syntax of other build systems
  467. that perform such filtering and remains sufficiently orthogonal to most
  468. programming and scripting languages, as well as with documentation systems.</p>
  469. <p>Note: If a token with the format <code>@</code><i>token</i><code>@</code>
  470. is found in a file, but no
  471. filter is associated with that token, no changes take place;
  472. therefore, no escaping
  473. method is available - but as long as you choose appropriate names for your
  474. tokens, this should not cause problems.</p>
  475. <p><b>Warning:</b> If you copy binary files with filtering turned on, you can corrupt the
  476. files. This feature should be used with text files <em>only</em>.</p>
  477. <h3><a name="path">Path-like Structures</a></h3>
  478. <p>You can specify <code>PATH</code>- and <code>CLASSPATH</code>-type
  479. references using both
  480. &quot;<code>:</code>&quot; and &quot;<code>;</code>&quot; as separator
  481. characters. Ant will
  482. convert the separator to the correct character of the current operating
  483. system.</p>
  484. <p>Wherever path-like values need to be specified, a nested element can
  485. be used. This takes the general form of:</p>
  486. <pre>
  487. &lt;classpath&gt;
  488. &lt;pathelement path=&quot;${classpath}&quot;/&gt;
  489. &lt;pathelement location=&quot;lib/helper.jar&quot;/&gt;
  490. &lt;/classpath&gt;
  491. </pre>
  492. <p>The <code>location</code> attribute specifies a single file or
  493. directory relative to the project's base directory (or an absolute
  494. filename), while the <code>path</code> attribute accepts colon-
  495. or semicolon-separated lists of locations. The <code>path</code>
  496. attribute is intended to be used with predefined paths - in any other
  497. case, multiple elements with <code>location</code> attributes should be
  498. preferred.</p>
  499. <p>As a shortcut, the <code>&lt;classpath&gt;</code> tag
  500. supports <code>path</code> and
  501. <code>location</code> attributes of its own, so:</p>
  502. <pre>
  503. &lt;classpath&gt;
  504. &lt;pathelement path=&quot;${classpath}&quot;/&gt;
  505. &lt;/classpath&gt;
  506. </pre>
  507. <p>can be abbreviated to:</p>
  508. <pre>
  509. &lt;classpath path=&quot;${classpath}&quot;/&gt;
  510. </pre>
  511. <p>In addition, one or more
  512. <a href="CoreTypes/resources.html#collection">Resource Collection</a>s
  513. can be specified as nested elements (these must consist of
  514. <a href="CoreTypes/resources.html#file">file</a>-type resources only).
  515. Additionally, it should be noted that although resource collections are
  516. processed in the order encountered, certain resource collection types
  517. such as <a href="CoreTypes/fileset.html">fileset</a>,
  518. <a href="CoreTypes/dirset.html">dirset</a> and
  519. <a href="CoreTypes/resources.html#files">files</a>
  520. are undefined in terms of order.</p>
  521. <pre>
  522. &lt;classpath&gt;
  523. &lt;pathelement path=&quot;${classpath}&quot;/&gt;
  524. &lt;fileset dir=&quot;lib&quot;&gt;
  525. &lt;include name=&quot;**/*.jar&quot;/&gt;
  526. &lt;/fileset&gt;
  527. &lt;pathelement location=&quot;classes&quot;/&gt;
  528. &lt;dirset dir=&quot;${build.dir}&quot;&gt;
  529. &lt;include name=&quot;apps/**/classes&quot;/&gt;
  530. &lt;exclude name=&quot;apps/**/*Test*&quot;/&gt;
  531. &lt;/dirset&gt;
  532. &lt;filelist refid=&quot;third-party_jars&quot;/&gt;
  533. &lt;/classpath&gt;
  534. </pre>
  535. <p>This builds a path that holds the value of <code>${classpath}</code>,
  536. followed by all jar files in the <code>lib</code> directory,
  537. the <code>classes</code> directory, all directories named
  538. <code>classes</code> under the <code>apps</code> subdirectory of
  539. <code>${build.dir}</code>, except those
  540. that have the text <code>Test</code> in their name, and
  541. the files specified in the referenced FileList.</p>
  542. <p>If you want to use the same path-like structure for several tasks,
  543. you can define them with a <code>&lt;path&gt;</code> element at the
  544. same level as <i>target</i>s, and reference them via their
  545. <i>id</i> attribute--see <a href="#references">References</a> for an
  546. example.</p>
  547. <p>By default a path like structure will re-evaluate all nested
  548. resource collections whenever it is used, which may lead to
  549. unnecessary re-scanning of the filesystem. Since Ant 1.8.0 path has
  550. an optional <i>cache</i> attribute, if it is set to true, the path
  551. instance will only scan its nested resource collections once and
  552. assume it doesn't change during the build anymore (the default
  553. for <i>cache</i> still is <i>false</i>). Even if you are using the
  554. path only in a single task it may improve overall performance to set
  555. <i>cache</i> to <i>true</i> if you are using complex nested
  556. constructs.</p>
  557. <p>A path-like structure can include a reference to another path-like
  558. structure (a path being itself a resource collection)
  559. via nested <code>&lt;path&gt;</code> elements:</p>
  560. <pre>
  561. &lt;path id=&quot;base.path&quot;&gt;
  562. &lt;pathelement path=&quot;${classpath}&quot;/&gt;
  563. &lt;fileset dir=&quot;lib&quot;&gt;
  564. &lt;include name=&quot;**/*.jar&quot;/&gt;
  565. &lt;/fileset&gt;
  566. &lt;pathelement location=&quot;classes&quot;/&gt;
  567. &lt;/path&gt;
  568. &lt;path id=&quot;tests.path&quot; cache=&quot;true&quot;&gt;
  569. &lt;path refid=&quot;base.path&quot;/&gt;
  570. &lt;pathelement location=&quot;testclasses&quot;/&gt;
  571. &lt;/path&gt;
  572. </pre>
  573. The shortcuts previously mentioned for <code>&lt;classpath&gt;</code> are also valid for <code>&lt;path&gt;</code>.For example:
  574. <pre>
  575. &lt;path id=&quot;base.path&quot;&gt;
  576. &lt;pathelement path=&quot;${classpath}&quot;/&gt;
  577. &lt;/path&gt;
  578. </pre>
  579. can be written as:
  580. <pre>
  581. &lt;path id=&quot;base.path&quot; path=&quot;${classpath}&quot;/&gt;
  582. </pre>
  583. <h4><a name="pathshortcut">Path Shortcut</a></h4>
  584. <p>
  585. In Ant 1.6 a shortcut for converting paths to OS specific strings
  586. in properties has been added. One can use the expression
  587. ${toString:<em>pathreference</em>} to convert a path element
  588. reference to a string that can be used for a path argument.
  589. For example:
  590. </p>
  591. <pre>
  592. &lt;path id="lib.path.ref"&gt;
  593. &lt;fileset dir="lib" includes="*.jar"/&gt;
  594. &lt;/path&gt;
  595. &lt;javac srcdir="src" destdir="classes"&gt;
  596. &lt;compilerarg arg="-Xbootstrap/p:${toString:lib.path.ref}"/&gt;
  597. &lt;/javac&gt;
  598. </pre>
  599. <h3><a name="arg">Command-line Arguments</a></h3>
  600. <p>Several tasks take arguments that will be passed to another
  601. process on the command line. To make it easier to specify arguments
  602. that contain space characters, nested <code>arg</code> elements can be used.</p>
  603. <table border="1" cellpadding="2" cellspacing="0">
  604. <tr>
  605. <td width="12%" valign="top"><b>Attribute</b></td>
  606. <td width="78%" valign="top"><b>Description</b></td>
  607. <td width="10%" valign="top"><b>Required</b></td>
  608. </tr>
  609. <tr>
  610. <td valign="top">value</td>
  611. <td valign="top">a single command-line argument; can contain space
  612. characters.</td>
  613. <td align="center" rowspan="5">Exactly one of these.</td>
  614. </tr>
  615. <tr>
  616. <td valign="top">file</td>
  617. <td valign="top">The name of a file as a single command-line
  618. argument; will be replaced with the absolute filename of the file.</td>
  619. </tr>
  620. <tr>
  621. <td valign="top">path</td>
  622. <td valign="top">A string that will be treated as a path-like
  623. string as a single command-line argument; you can use <code>;</code>
  624. or <code>:</code> as
  625. path separators and Ant will convert it to the platform's local
  626. conventions.</td>
  627. </tr>
  628. <tr>
  629. <td valign="top">pathref</td>
  630. <td valign="top"><a href="#references">Reference</a> to a path
  631. defined elsewhere. Ant will convert it to the platform's local
  632. conventions.</td>
  633. </tr>
  634. <tr>
  635. <td valign="top">line</td>
  636. <td valign="top">a space-delimited list of command-line arguments.</td>
  637. </tr>
  638. <tr>
  639. <td valign="top">prefix</td>
  640. <td valign="top">A fixed string to be placed in front of the
  641. argument. In the case of a line broken into parts, it will be
  642. placed in front of every part. <em>Since Ant 1.8.</em></td>
  643. <td valign="top" align="center">No</td>
  644. </tr>
  645. <tr>
  646. <td valign="top">suffix</td>
  647. <td valign="top">A fixed string to be placed immediately after the
  648. argument. In the case of a line broken into parts, it will be
  649. placed after every part. <em>Since Ant 1.8.</em></td>
  650. <td valign="top" align="center">No</td>
  651. </tr>
  652. </table>
  653. <p>It is highly recommended to avoid the <code>line</code> version
  654. when possible. Ant will try to split the command line in a way
  655. similar to what a (Unix) shell would do, but may create something that
  656. is very different from what you expect under some circumstances.</p>
  657. <h4>Examples</h4>
  658. <blockquote><pre>
  659. &lt;arg value=&quot;-l -a&quot;/&gt;
  660. </pre></blockquote>
  661. <p>is a single command-line argument containing a space character,
  662. <i>not</i> separate commands "-l" and "-a".</p>
  663. <blockquote><pre>
  664. &lt;arg line=&quot;-l -a&quot;/&gt;
  665. </pre></blockquote>
  666. <p>This is a command line with two separate arguments, "-l" and "-a".</p>
  667. <blockquote><pre>
  668. &lt;arg path=&quot;/dir;/dir2:\dir3&quot;/&gt;
  669. </pre></blockquote>
  670. <p>is a single command-line argument with the value
  671. <code>\dir;\dir2;\dir3</code> on DOS-based systems and
  672. <code>/dir:/dir2:/dir3</code> on Unix-like systems.</p>
  673. <h3><a name="references">References</a></h3>
  674. <p>Any project element can be assigned an identifier using its
  675. <code>id</code> attribute. In most cases the element can subsequently
  676. be referenced by specifying the <code>refid</code> attribute on an
  677. element of the same type. This can be useful if you are going to
  678. replicate the same snippet of XML over and over again--using a
  679. <code>&lt;classpath&gt;</code> structure more than once, for example.</p>
  680. <p>The following example:</p>
  681. <blockquote><pre>
  682. &lt;project ... &gt;
  683. &lt;target ... &gt;
  684. &lt;rmic ...&gt;
  685. &lt;classpath&gt;
  686. &lt;pathelement location=&quot;lib/&quot;/&gt;
  687. &lt;pathelement path=&quot;${java.class.path}/&quot;/&gt;
  688. &lt;pathelement path=&quot;${additional.path}&quot;/&gt;
  689. &lt;/classpath&gt;
  690. &lt;/rmic&gt;
  691. &lt;/target&gt;
  692. &lt;target ... &gt;
  693. &lt;javac ...&gt;
  694. &lt;classpath&gt;
  695. &lt;pathelement location=&quot;lib/&quot;/&gt;
  696. &lt;pathelement path=&quot;${java.class.path}/&quot;/&gt;
  697. &lt;pathelement path=&quot;${additional.path}&quot;/&gt;
  698. &lt;/classpath&gt;
  699. &lt;/javac&gt;
  700. &lt;/target&gt;
  701. &lt;/project&gt;
  702. </pre></blockquote>
  703. <p>could be rewritten as:</p>
  704. <blockquote><pre>
  705. &lt;project ... &gt;
  706. &lt;path id=&quot;project.class.path&quot;&gt;
  707. &lt;pathelement location=&quot;lib/&quot;/&gt;
  708. &lt;pathelement path=&quot;${java.class.path}/&quot;/&gt;
  709. &lt;pathelement path=&quot;${additional.path}&quot;/&gt;
  710. &lt;/path&gt;
  711. &lt;target ... &gt;
  712. &lt;rmic ...&gt;
  713. &lt;classpath refid=&quot;project.class.path&quot;/&gt;
  714. &lt;/rmic&gt;
  715. &lt;/target&gt;
  716. &lt;target ... &gt;
  717. &lt;javac ...&gt;
  718. &lt;classpath refid=&quot;project.class.path&quot;/&gt;
  719. &lt;/javac&gt;
  720. &lt;/target&gt;
  721. &lt;/project&gt;
  722. </pre></blockquote>
  723. <p>All tasks that use nested elements for
  724. <a href="CoreTypes/patternset.html">PatternSet</a>s,
  725. <a href="CoreTypes/fileset.html">FileSet</a>s,
  726. <a href="CoreTypes/zipfileset.html">ZipFileSet</a>s or
  727. <a href="#path">path-like structures</a> accept references to these structures
  728. as shown in the examples. Using <code>refid</code> on a task will ordinarily
  729. have the same effect (referencing a task already declared), but the user
  730. should be aware that the interpretation of this attribute is dependent on the
  731. implementation of the element upon which it is specified. Some tasks (the
  732. <a href="CoreTasks/property.html">property</a> task is a handy example)
  733. deliberately assign a different meaning to <code>refid</code>.</p>
  734. <h3><a name="toString">Getting the value of a Reference with ${toString:}</a></h3>
  735. <p>
  736. Any Ant type which has been declared with a reference can also its string
  737. value extracted by using the <code>${toString:}</code> operation,
  738. with the name of the reference listed after the <code>toString:</code> text.
  739. The <code>toString()</code> method of the Java class instance that is
  740. referenced is invoked -all built in types strive to produce useful and relevant
  741. output in such an instance.
  742. </p>
  743. <p>
  744. For example, here is how to get a listing of the files in a fileset,
  745. <p>
  746. <pre>
  747. &lt;fileset id=&quot;sourcefiles&quot; dir=&quot;src&quot; includes=&quot;**/*.java&quot; /&gt;
  748. &lt;echo&gt; sourcefiles = ${toString:sourcefiles} &lt;/echo&gt;
  749. </pre>
  750. <p>
  751. There is no guarantee that external types provide meaningful information in such
  752. a situation</p>
  753. <h3><a name="ant.refid">Getting the value of a Reference with
  754. ${ant.refid:}</a></h3>
  755. <p>Any Ant type which has been declared with a reference can also be
  756. used as a property by using the <code>${ant.refid:}</code>
  757. operation, with the name of the reference listed after
  758. the <code>ant.refid:</code> text. The difference between this
  759. operation and <a href="#toString"><code>${toString:}</code></a> is
  760. that <code>${ant.refid:}</code> will expand to the referenced object
  761. itself. In most circumstances the toString method will be invoked
  762. anyway, for example if the <code>${ant.refid:}</code> is surrounded
  763. by other text.</p>
  764. <p>This syntax is most useful when using a task with attribute setters
  765. that accept objects other than String. For example if the setter
  766. accepts a Resource object as in</p>
  767. <pre>
  768. public void setAttr(Resource r) { ... }
  769. </pre>
  770. <p>then the syntax can be used to pass in resource subclasses
  771. preciously defined as references like</p>
  772. <pre>
  773. &lt;url url="http://ant.apache.org/" id="anturl"/&gt;
  774. &lt;my:task attr="${ant.refid:anturl}"/&gt;
  775. </pre>
  776. <h3><a name="external-tasks">Use of external tasks</a></h3>
  777. Ant supports a plugin mechanism for using third party tasks. For using them you
  778. have to do two steps:
  779. <ol>
  780. <li>place their implementation somewhere where Ant can find them</li>
  781. <li>declare them.</li>
  782. </ol>
  783. Don't add anything to the CLASSPATH environment variable - this is often the
  784. reason for very obscure errors. Use Ant's own <a href="install.html#optionalTasks">mechanisms</a>
  785. for adding libraries:
  786. <ul>
  787. <li>via command line argument <code>-lib</code></li>
  788. <li>adding to <code>${user.home}/.ant/lib</code></li>
  789. <li>adding to <code>${ant.home}/lib</code></li>
  790. </ul>
  791. For the declaration there are several ways:
  792. <ul>
  793. <li>declare a single task per using instruction using
  794. <code>&lt;<a href="CoreTasks/taskdef.html">taskdef</a> name=&quot;taskname&quot;
  795. classname=&quot;ImplementationClass&quot;/&gt;</code>
  796. <br>
  797. <code>&lt;taskdef name=&quot;for&quot; classname=&quot;net.sf.antcontrib.logic.For&quot; /&gt;
  798. &lt;for ... /&gt;</code>
  799. </li>
  800. <li>declare a bundle of tasks using a properties-file holding these
  801. taskname-ImplementationClass-pairs and <code>&lt;taskdef&gt;</code>
  802. <br>
  803. <code>&lt;taskdef resource=&quot;net/sf/antcontrib/antcontrib.properties&quot; /&gt;
  804. &lt;for ... /&gt;</code>
  805. </li>
  806. <li>declare a bundle of tasks using a <a href="CoreTypes/antlib.html">xml-file</a> holding these
  807. taskname-ImplementationClass-pairs and <code>&lt;taskdef&gt;</code>
  808. <br>
  809. <code>&lt;taskdef resource=&quot;net/sf/antcontrib/antlib.xml&quot; /&gt;
  810. &lt;for ... /&gt;</code>
  811. </li>
  812. <li>declare a bundle of tasks using a xml-file named antlib.xml, XML-namespace and
  813. <a href="CoreTypes/antlib.html#antlibnamespace"><code>antlib:</code> protocoll handler</a>
  814. <br>
  815. <code>&lt;project xmlns:ac=&quot;antlib:net.sf.antconrib&quot;/&gt;
  816. &lt;ac:for ... /&gt;</code>
  817. </li>
  818. </ul>
  819. If you need a special function, you should
  820. <ol>
  821. <li>have a look at this manual, because Ant provides lot of tasks</li>
  822. <li>have a look at the external task page in the <a href="../external.html">manual</a>
  823. (or better <a href="http://ant.apache.org/external.html">online</a>)</li>
  824. <li>have a look at the external task <a href="http://wiki.apache.org/ant/AntExternalTaskdefs">wiki
  825. page</a></li>
  826. <li>ask on the <a href="http://ant.apache.org/mail.html#User%20List">Ant user</a> list</li>
  827. <li><a href="tutorial-writing-tasks.html">implement </a>(and share) your own</li>
  828. </ol>
  829. </body>
  830. </html>