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 32 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  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;, &quot;1.4&quot; and &quot;1.5&quot;.
  307. </pre>
  308. <p>There is also another property, but this is set by the launcher script and therefore
  309. maybe not set inside IDEs:</p>
  310. <pre>
  311. ant.home home directory of Ant
  312. </pre>
  313. <a name="propertyHelper"><h3>Property Helpers</h3></a>
  314. Ant's property handling is accomplished by an instance of
  315. <code>org.apache.tools.ant.PropertyHelper</code> associated with the current Project.
  316. You can learn more about this class by examining Ant's Java API. In Ant 1.8 the
  317. PropertyHelper class was much reworked and now itself employs a number of helper
  318. classes (actually instances of the <code>org.apache.tools.ant.PropertyHelper$Delegate</code>
  319. marker interface) to take care of discrete tasks such as property setting, retrieval,
  320. parsing, etc. This makes Ant's property handling highly extensible; also of interest is the
  321. new <a href="CoreTasks/propertyhelper.html">propertyhelper</a> task used to manipulate the
  322. PropertyHelper and its delegates from the context of the Ant buildfile.
  323. <a name="example"><h3>Example Buildfile</h3></a>
  324. <pre>
  325. &lt;project name=&quot;MyProject&quot; default=&quot;dist&quot; basedir=&quot;.&quot;&gt;
  326. &lt;description&gt;
  327. simple example build file
  328. &lt;/description&gt;
  329. &lt;!-- set global properties for this build --&gt;
  330. &lt;property name=&quot;src&quot; location=&quot;src&quot;/&gt;
  331. &lt;property name=&quot;build&quot; location=&quot;build&quot;/&gt;
  332. &lt;property name=&quot;dist&quot; location=&quot;dist&quot;/&gt;
  333. &lt;target name=&quot;init&quot;&gt;
  334. &lt;!-- Create the time stamp --&gt;
  335. &lt;tstamp/&gt;
  336. &lt;!-- Create the build directory structure used by compile --&gt;
  337. &lt;mkdir dir=&quot;${build}&quot;/&gt;
  338. &lt;/target&gt;
  339. &lt;target name=&quot;compile&quot; depends=&quot;init&quot;
  340. description=&quot;compile the source &quot; &gt;
  341. &lt;!-- Compile the java code from ${src} into ${build} --&gt;
  342. &lt;javac srcdir=&quot;${src}&quot; destdir=&quot;${build}&quot;/&gt;
  343. &lt;/target&gt;
  344. &lt;target name=&quot;dist&quot; depends=&quot;compile&quot;
  345. description=&quot;generate the distribution&quot; &gt;
  346. &lt;!-- Create the distribution directory --&gt;
  347. &lt;mkdir dir=&quot;${dist}/lib&quot;/&gt;
  348. &lt;!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file --&gt;
  349. &lt;jar jarfile=&quot;${dist}/lib/MyProject-${DSTAMP}.jar&quot; basedir=&quot;${build}&quot;/&gt;
  350. &lt;/target&gt;
  351. &lt;target name=&quot;clean&quot;
  352. description=&quot;clean up&quot; &gt;
  353. &lt;!-- Delete the ${build} and ${dist} directory trees --&gt;
  354. &lt;delete dir=&quot;${build}&quot;/&gt;
  355. &lt;delete dir=&quot;${dist}&quot;/&gt;
  356. &lt;/target&gt;
  357. &lt;/project&gt;
  358. </pre>
  359. <p>Notice that we are declaring properties outside any target. As of
  360. Ant 1.6 all tasks can be declared outside targets (earlier version
  361. only allowed <tt>&lt;property&gt;</tt>,<tt>&lt;typedef&gt;</tt> and
  362. <tt>&lt;taskdef&gt;</tt>). When you do this they are evaluated before
  363. any targets are executed. Some tasks will generate build failures if
  364. they are used outside of targets as they may cause infinite loops
  365. otherwise (<code>&lt;antcall&gt;</code> for example).</p>
  366. <p>
  367. We have given some targets descriptions; this causes the <tt>projecthelp</tt>
  368. invocation option to list them as public targets with the descriptions; the
  369. other target is internal and not listed.
  370. <p>
  371. Finally, for this target to work the source in the <tt>src</tt> subdirectory
  372. should be stored in a directory tree which matches the package names. Check the
  373. <tt>&lt;javac&gt;</tt> task for details.
  374. <a name="filters"><h3>Token Filters</h3></a>
  375. <p>A project can have a set of tokens that might be automatically expanded if
  376. found when a file is copied, when the filtering-copy behavior is selected in the
  377. tasks that support this. These might be set in the buildfile
  378. by the <a href="CoreTasks/filter.html">filter</a> task.</p>
  379. <p>Since this can potentially be a very harmful behavior,
  380. the tokens in the files <b>must</b>
  381. be of the form <code>@</code><i>token</i><code>@</code>, where
  382. <i>token</i> is the token name that is set
  383. in the <code>&lt;filter&gt;</code> task. This token syntax matches the syntax of other build systems
  384. that perform such filtering and remains sufficiently orthogonal to most
  385. programming and scripting languages, as well as with documentation systems.</p>
  386. <p>Note: If a token with the format <code>@</code><i>token</i><code>@</code>
  387. is found in a file, but no
  388. filter is associated with that token, no changes take place;
  389. therefore, no escaping
  390. method is available - but as long as you choose appropriate names for your
  391. tokens, this should not cause problems.</p>
  392. <p><b>Warning:</b> If you copy binary files with filtering turned on, you can corrupt the
  393. files. This feature should be used with text files <em>only</em>.</p>
  394. <h3><a name="path">Path-like Structures</a></h3>
  395. <p>You can specify <code>PATH</code>- and <code>CLASSPATH</code>-type
  396. references using both
  397. &quot;<code>:</code>&quot; and &quot;<code>;</code>&quot; as separator
  398. characters. Ant will
  399. convert the separator to the correct character of the current operating
  400. system.</p>
  401. <p>Wherever path-like values need to be specified, a nested element can
  402. be used. This takes the general form of:</p>
  403. <pre>
  404. &lt;classpath&gt;
  405. &lt;pathelement path=&quot;${classpath}&quot;/&gt;
  406. &lt;pathelement location=&quot;lib/helper.jar&quot;/&gt;
  407. &lt;/classpath&gt;
  408. </pre>
  409. <p>The <code>location</code> attribute specifies a single file or
  410. directory relative to the project's base directory (or an absolute
  411. filename), while the <code>path</code> attribute accepts colon-
  412. or semicolon-separated lists of locations. The <code>path</code>
  413. attribute is intended to be used with predefined paths - in any other
  414. case, multiple elements with <code>location</code> attributes should be
  415. preferred.</p>
  416. <p>As a shortcut, the <code>&lt;classpath&gt;</code> tag
  417. supports <code>path</code> and
  418. <code>location</code> attributes of its own, so:</p>
  419. <pre>
  420. &lt;classpath&gt;
  421. &lt;pathelement path=&quot;${classpath}&quot;/&gt;
  422. &lt;/classpath&gt;
  423. </pre>
  424. <p>can be abbreviated to:</p>
  425. <pre>
  426. &lt;classpath path=&quot;${classpath}&quot;/&gt;
  427. </pre>
  428. <p>In addition, one or more
  429. <a href="CoreTypes/resources.html#collection">Resource Collection</a>s
  430. can be specified as nested elements (these must consist of
  431. <a href="CoreTypes/resources.html#file">file</a>-type resources only).
  432. Additionally, it should be noted that although resource collections are
  433. processed in the order encountered, certain resource collection types
  434. such as <a href="CoreTypes/fileset.html">fileset</a>,
  435. <a href="CoreTypes/dirset.html">dirset</a> and
  436. <a href="CoreTypes/resources.html#files">files</a>
  437. are undefined in terms of order.</p>
  438. <pre>
  439. &lt;classpath&gt;
  440. &lt;pathelement path=&quot;${classpath}&quot;/&gt;
  441. &lt;fileset dir=&quot;lib&quot;&gt;
  442. &lt;include name=&quot;**/*.jar&quot;/&gt;
  443. &lt;/fileset&gt;
  444. &lt;pathelement location=&quot;classes&quot;/&gt;
  445. &lt;dirset dir=&quot;${build.dir}&quot;&gt;
  446. &lt;include name=&quot;apps/**/classes&quot;/&gt;
  447. &lt;exclude name=&quot;apps/**/*Test*&quot;/&gt;
  448. &lt;/dirset&gt;
  449. &lt;filelist refid=&quot;third-party_jars&quot;/&gt;
  450. &lt;/classpath&gt;
  451. </pre>
  452. <p>This builds a path that holds the value of <code>${classpath}</code>,
  453. followed by all jar files in the <code>lib</code> directory,
  454. the <code>classes</code> directory, all directories named
  455. <code>classes</code> under the <code>apps</code> subdirectory of
  456. <code>${build.dir}</code>, except those
  457. that have the text <code>Test</code> in their name, and
  458. the files specified in the referenced FileList.</p>
  459. <p>If you want to use the same path-like structure for several tasks,
  460. you can define them with a <code>&lt;path&gt;</code> element at the
  461. same level as <i>target</i>s, and reference them via their
  462. <i>id</i> attribute--see <a href="#references">References</a> for an
  463. example.</p>
  464. <p>A path-like structure can include a reference to another path-like
  465. structure (a path being itself a resource collection)
  466. via nested <code>&lt;path&gt;</code> elements:</p>
  467. <pre>
  468. &lt;path id=&quot;base.path&quot;&gt;
  469. &lt;pathelement path=&quot;${classpath}&quot;/&gt;
  470. &lt;fileset dir=&quot;lib&quot;&gt;
  471. &lt;include name=&quot;**/*.jar&quot;/&gt;
  472. &lt;/fileset&gt;
  473. &lt;pathelement location=&quot;classes&quot;/&gt;
  474. &lt;/path&gt;
  475. &lt;path id=&quot;tests.path&quot;&gt;
  476. &lt;path refid=&quot;base.path&quot;/&gt;
  477. &lt;pathelement location=&quot;testclasses&quot;/&gt;
  478. &lt;/path&gt;
  479. </pre>
  480. The shortcuts previously mentioned for <code>&lt;classpath&gt;</code> are also valid for <code>&lt;path&gt;</code>.For example:
  481. <pre>
  482. &lt;path id=&quot;base.path&quot;&gt;
  483. &lt;pathelement path=&quot;${classpath}&quot;/&gt;
  484. &lt;/path&gt;
  485. </pre>
  486. can be written as:
  487. <pre>
  488. &lt;path id=&quot;base.path&quot; path=&quot;${classpath}&quot;/&gt;
  489. </pre>
  490. <h4><a name="pathshortcut">Path Shortcut</a></h4>
  491. <p>
  492. In Ant 1.6 a shortcut for converting paths to OS specific strings
  493. in properties has been added. One can use the expression
  494. ${toString:<em>pathreference</em>} to convert a path element
  495. reference to a string that can be used for a path argument.
  496. For example:
  497. </p>
  498. <pre>
  499. &lt;path id="lib.path.ref"&gt;
  500. &lt;fileset dir="lib" includes="*.jar"/&gt;
  501. &lt;/path&gt;
  502. &lt;javac srcdir="src" destdir="classes"&gt;
  503. &lt;compilerarg arg="-Xbootstrap/p:${toString:lib.path.ref}"/&gt;
  504. &lt;/javac&gt;
  505. </pre>
  506. <h3><a name="arg">Command-line Arguments</a></h3>
  507. <p>Several tasks take arguments that will be passed to another
  508. process on the command line. To make it easier to specify arguments
  509. that contain space characters, nested <code>arg</code> elements can be used.</p>
  510. <table border="1" cellpadding="2" cellspacing="0">
  511. <tr>
  512. <td width="12%" valign="top"><b>Attribute</b></td>
  513. <td width="78%" valign="top"><b>Description</b></td>
  514. <td width="10%" valign="top"><b>Required</b></td>
  515. </tr>
  516. <tr>
  517. <td valign="top">value</td>
  518. <td valign="top">a single command-line argument; can contain space
  519. characters.</td>
  520. <td align="center" rowspan="5">Exactly one of these.</td>
  521. </tr>
  522. <tr>
  523. <td valign="top">file</td>
  524. <td valign="top">The name of a file as a single command-line
  525. argument; will be replaced with the absolute filename of the file.</td>
  526. </tr>
  527. <tr>
  528. <td valign="top">path</td>
  529. <td valign="top">A string that will be treated as a path-like
  530. string as a single command-line argument; you can use <code>;</code>
  531. or <code>:</code> as
  532. path separators and Ant will convert it to the platform's local
  533. conventions.</td>
  534. </tr>
  535. <tr>
  536. <td valign="top">pathref</td>
  537. <td valign="top"><a href="#references">Reference</a> to a path
  538. defined elsewhere. Ant will convert it to the platform's local
  539. conventions.</td>
  540. </tr>
  541. <tr>
  542. <td valign="top">line</td>
  543. <td valign="top">a space-delimited list of command-line arguments.</td>
  544. </tr>
  545. </table>
  546. <p>It is highly recommended to avoid the <code>line</code> version
  547. when possible. Ant will try to split the command line in a way
  548. similar to what a (Unix) shell would do, but may create something that
  549. is very different from what you expect under some circumstances.</p>
  550. <h4>Examples</h4>
  551. <blockquote><pre>
  552. &lt;arg value=&quot;-l -a&quot;/&gt;
  553. </pre></blockquote>
  554. <p>is a single command-line argument containing a space character,
  555. <i>not</i> separate commands "-l" and "-a".</p>
  556. <blockquote><pre>
  557. &lt;arg line=&quot;-l -a&quot;/&gt;
  558. </pre></blockquote>
  559. <p>This is a command line with two separate arguments, "-l" and "-a".</p>
  560. <blockquote><pre>
  561. &lt;arg path=&quot;/dir;/dir2:\dir3&quot;/&gt;
  562. </pre></blockquote>
  563. <p>is a single command-line argument with the value
  564. <code>\dir;\dir2;\dir3</code> on DOS-based systems and
  565. <code>/dir:/dir2:/dir3</code> on Unix-like systems.</p>
  566. <h3><a name="references">References</a></h3>
  567. <p>Any project element can be assigned an identifier using its
  568. <code>id</code> attribute. In most cases the element can subsequently
  569. be referenced by specifying the <code>refid</code> attribute on an
  570. element of the same type. This can be useful if you are going to
  571. replicate the same snippet of XML over and over again--using a
  572. <code>&lt;classpath&gt;</code> structure more than once, for example.</p>
  573. <p>The following example:</p>
  574. <blockquote><pre>
  575. &lt;project ... &gt;
  576. &lt;target ... &gt;
  577. &lt;rmic ...&gt;
  578. &lt;classpath&gt;
  579. &lt;pathelement location=&quot;lib/&quot;/&gt;
  580. &lt;pathelement path=&quot;${java.class.path}/&quot;/&gt;
  581. &lt;pathelement path=&quot;${additional.path}&quot;/&gt;
  582. &lt;/classpath&gt;
  583. &lt;/rmic&gt;
  584. &lt;/target&gt;
  585. &lt;target ... &gt;
  586. &lt;javac ...&gt;
  587. &lt;classpath&gt;
  588. &lt;pathelement location=&quot;lib/&quot;/&gt;
  589. &lt;pathelement path=&quot;${java.class.path}/&quot;/&gt;
  590. &lt;pathelement path=&quot;${additional.path}&quot;/&gt;
  591. &lt;/classpath&gt;
  592. &lt;/javac&gt;
  593. &lt;/target&gt;
  594. &lt;/project&gt;
  595. </pre></blockquote>
  596. <p>could be rewritten as:</p>
  597. <blockquote><pre>
  598. &lt;project ... &gt;
  599. &lt;path id=&quot;project.class.path&quot;&gt;
  600. &lt;pathelement location=&quot;lib/&quot;/&gt;
  601. &lt;pathelement path=&quot;${java.class.path}/&quot;/&gt;
  602. &lt;pathelement path=&quot;${additional.path}&quot;/&gt;
  603. &lt;/path&gt;
  604. &lt;target ... &gt;
  605. &lt;rmic ...&gt;
  606. &lt;classpath refid=&quot;project.class.path&quot;/&gt;
  607. &lt;/rmic&gt;
  608. &lt;/target&gt;
  609. &lt;target ... &gt;
  610. &lt;javac ...&gt;
  611. &lt;classpath refid=&quot;project.class.path&quot;/&gt;
  612. &lt;/javac&gt;
  613. &lt;/target&gt;
  614. &lt;/project&gt;
  615. </pre></blockquote>
  616. <p>All tasks that use nested elements for
  617. <a href="CoreTypes/patternset.html">PatternSet</a>s,
  618. <a href="CoreTypes/fileset.html">FileSet</a>s,
  619. <a href="CoreTypes/zipfileset.html">ZipFileSet</a>s or
  620. <a href="#path">path-like structures</a> accept references to these structures
  621. as shown in the examples. Using <code>refid</code> on a task will ordinarily
  622. have the same effect (referencing a task already declared), but the user
  623. should be aware that the interpretation of this attribute is dependent on the
  624. implementation of the element upon which it is specified. Some tasks (the
  625. <a href="CoreTasks/property.html">property</a> task is a handy example)
  626. deliberately assign a different meaning to <code>refid</code>.</p>
  627. <h3><a name="toString">Getting the value of a Reference with ${toString:}</a></h3>
  628. <p>
  629. Any Ant type which has been declared with a reference can also its string
  630. value extracted by using the <code>${toString:}</code> operation,
  631. with the name of the reference listed after the <code>toString:</code> text.
  632. The <code>toString()</code> method of the Java class instance that is
  633. referenced is invoked -all built in types strive to produce useful and relevant
  634. output in such an instance.
  635. </p>
  636. <p>
  637. For example, here is how to get a listing of the files in a fileset,
  638. <p>
  639. <pre>
  640. &lt;fileset id=&quot;sourcefiles&quot; dir=&quot;src&quot; includes=&quot;**/*.java&quot; /&gt;
  641. &lt;echo&gt; sourcefiles = ${toString:sourcefiles} &lt;/echo&gt;
  642. </pre>
  643. <p>
  644. There is no guarantee that external types provide meaningful information in such
  645. a situation</p>
  646. <h3><a name="external-tasks">Use of external tasks</a></h3>
  647. Ant supports a plugin mechanism for using third party tasks. For using them you
  648. have to do two steps:
  649. <ol>
  650. <li>place their implementation somewhere where Ant can find them</li>
  651. <li>declare them.</li>
  652. </ol>
  653. Don't add anything to the CLASSPATH environment variable - this is often the
  654. reason for very obscure errors. Use Ant's own <a href="install.html#optionalTasks">mechanisms</a>
  655. for adding libraries:
  656. <ul>
  657. <li>via command line argument <code>-lib</code></li>
  658. <li>adding to <code>${user.home}/.ant/lib</code></li>
  659. <li>adding to <code>${ant.home}/lib</code></li>
  660. </ul>
  661. For the declaration there are several ways:
  662. <ul>
  663. <li>declare a single task per using instruction using
  664. <code>&lt;<a href="CoreTasks/taskdef.html">taskdef</a> name=&quot;taskname&quot;
  665. classname=&quot;ImplementationClass&quot;/&gt;</code>
  666. <br>
  667. <code>&lt;taskdef name=&quot;for&quot; classname=&quot;net.sf.antcontrib.logic.For&quot; /&gt;
  668. &lt;for ... /&gt;</code>
  669. </li>
  670. <li>declare a bundle of tasks using a properties-file holding these
  671. taskname-ImplementationClass-pairs and <code>&lt;taskdef&gt;</code>
  672. <br>
  673. <code>&lt;taskdef resource=&quot;net/sf/antcontrib/antcontrib.properties&quot; /&gt;
  674. &lt;for ... /&gt;</code>
  675. </li>
  676. <li>declare a bundle of tasks using a <a href="CoreTypes/antlib.html">xml-file</a> holding these
  677. taskname-ImplementationClass-pairs and <code>&lt;taskdef&gt;</code>
  678. <br>
  679. <code>&lt;taskdef resource=&quot;net/sf/antcontrib/antlib.xml&quot; /&gt;
  680. &lt;for ... /&gt;</code>
  681. </li>
  682. <li>declare a bundle of tasks using a xml-file named antlib.xml, XML-namespace and
  683. <a href="CoreTypes/antlib.html#antlibnamespace"><code>antlib:</code> protocoll handler</a>
  684. <br>
  685. <code>&lt;project xmlns:ac=&quot;antlib:net.sf.antconrib&quot;/&gt;
  686. &lt;ac:for ... /&gt;</code>
  687. </li>
  688. </ul>
  689. If you need a special function, you should
  690. <ol>
  691. <li>have a look at this manual, because Ant provides lot of tasks</li>
  692. <li>have a look at the external task page in the <a href="../external.html">manual</a>
  693. (or better <a href="http://ant.apache.org/external.html">online</a>)</li>
  694. <li>have a look at the external task <a href="http://wiki.apache.org/ant/AntExternalTaskdefs">wiki
  695. page</a></li>
  696. <li>ask on the <a href="http://ant.apache.org/mail.html#User%20List">Ant user</a> list</li>
  697. <li><a href="tutorial-writing-tasks.html">implement </a>(and share) your own</li>
  698. </ol>
  699. </body>
  700. </html>