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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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 Apache Ant</h1>
  23. <h2 id="buildfile">Writing a Simple Buildfile</h2>
  24. <p>Apache 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 <var>id</var> 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 id="projects">Projects</h3>
  31. <p>A <em>project</em> has three attributes:</p>
  32. <table class="attr">
  33. <tr>
  34. <th>Attribute</th>
  35. <th>Description</th>
  36. <th>Required</th>
  37. </tr>
  38. <tr>
  39. <td>name</td>
  40. <td>the name of the project.</td>
  41. <td>No</td>
  42. </tr>
  43. <tr>
  44. <td>default</td>
  45. <td>the default target to use when no target is supplied.</td>
  46. <td>No; however, <em>since Ant 1.6.0</em>,
  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"><code>-projecthelp</code></a> option.
  51. </td>
  52. </tr>
  53. <tr>
  54. <td>basedir</td>
  55. <td>the base directory from which all path calculations are
  56. done. A relative path is resolved relative to the directory containing
  57. the buildfile.
  58. </td>
  59. <td>No; defaults to the parent directory of the buildfile,
  60. unless overridden by the project's <var>basedir</var> or the <code>basedir</code>
  61. property</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="Types/description.html">description</a> type).</p>
  67. <p>Each project defines one or more <em>targets</em>.
  68. A target is a set of <em>tasks</em> 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 <var>default</var> is used.</p>
  72. <h3 id="targets">Targets</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. <em>depends on</em> the compile target. Ant resolves these dependencies.</p>
  77. <p>It should be noted, however, that Ant's <var>depends</var> attribute
  78. only specifies the <em>order</em> in which targets should be executed&mdash;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>More information can be found in the dedicated <a href="targets.html">manual page</a>.</p>
  83. <h3 id="tasks">Tasks</h3>
  84. <p>A task is a piece of code that can be executed.</p>
  85. <p>A task can have multiple attributes (or arguments, if you prefer). The value
  86. of an attribute might contain references to a property. These references will be
  87. resolved before the task is executed.</p>
  88. <p>Tasks have a common structure:</p>
  89. <pre>&lt;<i>name</i> <var>attribute1</var>=&quot;<i>value1</i>&quot; <var>attribute2</var>=&quot;<i>value2</i>&quot; ... /&gt;</pre>
  90. <p>where <code><i>name</i></code> is the name of the task,
  91. <var>attributeN</var> is the attribute name, and
  92. <code><i>valueN</i></code> is the value for this attribute.</p>
  93. <p>There is a set of <a href="tasklist.html" target="navFrame">built-in tasks</a>, but it is also very
  94. easy to <a href="develop.html#writingowntask">write your own</a>.</p>
  95. <p>All tasks can have a <var>name</var> attribute. The value of
  96. this attribute will be used in the logging messages generated by
  97. Ant.</p>
  98. Tasks can be assigned an <var>id</var> attribute:
  99. <pre>&lt;<i>taskname</i> <var>id</var>="<i>taskID</i>" ... /&gt;</pre>
  100. where <code><i>taskname</i></code> is the name of the task, and <code><i>taskID</i></code> is
  101. a unique identifier for this task.
  102. You can refer to the
  103. corresponding task object in scripts or other tasks via this name.
  104. For example, in scripts you could do:
  105. <pre>
  106. &lt;script ... &gt;
  107. task1.setFoo("bar");
  108. &lt;/script&gt;</pre>
  109. to set the <code>foo</code> attribute of this particular task instance.
  110. In another task (written in Java), you can access the instance via
  111. <code>project.getReference("task1")</code>.
  112. <p>
  113. Note 1: If <q>task1</q> has not been run yet, then
  114. it has not been configured (ie., no attributes have been set), and if it is
  115. going to be configured later, anything you've done to the instance may
  116. be overwritten.
  117. </p>
  118. <p>
  119. Note 2: Future versions of Ant will most likely <em>not</em>
  120. be backward-compatible with this behaviour, since there will likely be no
  121. task instances at all, only proxies.
  122. </p>
  123. <h3 id="properties">Properties</h3>
  124. <p>Properties are an important way to customize a build process or
  125. to just provide shortcuts for strings that are used repeatedly
  126. inside a buildfile.</p>
  127. <p>In its most simple form properties are defined in the buildfile
  128. (for example by the <a href="Tasks/property.html">property</a>
  129. task) or might be set outside Ant. A property has a name and a
  130. value; the name is case-sensitive. Properties may be used in the
  131. value of task attributes or in the nested text of tasks that support
  132. them. This is done by placing the property name between
  133. <q>${</q> and <q>}</q> in the
  134. attribute value. For example, if there is a <code>builddir</code>
  135. property with the value <q>build</q>, then this could be used
  136. in an attribute like this: <samp>${builddir}/classes</samp>. This
  137. is resolved at run-time as <samp>build/classes</samp>.</p>
  138. <p><em>Since Ant 1.8.0</em>, property expansion has become much more powerful
  139. than simple key value pairs, more details can be
  140. found <a href="properties.html">in the concepts section</a> of this
  141. manual.</p>
  142. <h3 id="example">Example Buildfile</h3>
  143. <pre>
  144. &lt;project name=&quot;MyProject&quot; default=&quot;dist&quot; basedir=&quot;.&quot;&gt;
  145. &lt;description&gt;
  146. simple example build file
  147. &lt;/description&gt;
  148. &lt;!-- set global properties for this build --&gt;
  149. &lt;property name=&quot;src&quot; location=&quot;src&quot;/&gt;
  150. &lt;property name=&quot;build&quot; location=&quot;build&quot;/&gt;
  151. &lt;property name=&quot;dist&quot; location=&quot;dist&quot;/&gt;
  152. &lt;target name=&quot;init&quot;&gt;
  153. &lt;!-- Create the time stamp --&gt;
  154. &lt;tstamp/&gt;
  155. &lt;!-- Create the build directory structure used by compile --&gt;
  156. &lt;mkdir dir=&quot;${build}&quot;/&gt;
  157. &lt;/target&gt;
  158. &lt;target name=&quot;compile&quot; depends=&quot;init&quot;
  159. description=&quot;compile the source&quot;&gt;
  160. &lt;!-- Compile the Java code from ${src} into ${build} --&gt;
  161. &lt;javac srcdir=&quot;${src}&quot; destdir=&quot;${build}&quot;/&gt;
  162. &lt;/target&gt;
  163. &lt;target name=&quot;dist&quot; depends=&quot;compile&quot;
  164. description=&quot;generate the distribution&quot;&gt;
  165. &lt;!-- Create the distribution directory --&gt;
  166. &lt;mkdir dir=&quot;${dist}/lib&quot;/&gt;
  167. &lt;!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file --&gt;
  168. &lt;jar jarfile=&quot;${dist}/lib/MyProject-${DSTAMP}.jar&quot; basedir=&quot;${build}&quot;/&gt;
  169. &lt;/target&gt;
  170. &lt;target name=&quot;clean&quot;
  171. description=&quot;clean up&quot;&gt;
  172. &lt;!-- Delete the ${build} and ${dist} directory trees --&gt;
  173. &lt;delete dir=&quot;${build}&quot;/&gt;
  174. &lt;delete dir=&quot;${dist}&quot;/&gt;
  175. &lt;/target&gt;
  176. &lt;/project&gt;</pre>
  177. <p>Notice that we are declaring properties outside any target. <em>Since
  178. Ant 1.6</em>, all tasks can be declared outside targets (earlier version
  179. only allowed <code>&lt;property&gt;</code>, <code>&lt;typedef&gt;</code> and
  180. <code>&lt;taskdef&gt;</code>). When you do this they are evaluated before
  181. any targets are executed. Some tasks will generate build failures if
  182. they are used outside of targets as they may cause infinite loops
  183. otherwise (<code>&lt;antcall&gt;</code> for example).</p>
  184. <p>
  185. We have given some targets descriptions; this causes the <code>-projecthelp</code>
  186. invocation option to list them as public targets with the descriptions; the
  187. other target is internal and not listed.
  188. <p>
  189. Finally, for this target to work the source in the <samp>src</samp> subdirectory
  190. should be stored in a directory tree which matches the package names. Check the
  191. <code>&lt;javac&gt;</code> task for details.
  192. <h3 id="filters">Token Filters</h3>
  193. <p>A project can have a set of tokens that might be automatically expanded if
  194. found when a file is copied, when the filtering-copy behavior is selected in the
  195. tasks that support this. These might be set in the buildfile
  196. by the <a href="Tasks/filter.html">filter</a> task.</p>
  197. <p>Since this can potentially be a very harmful behavior,
  198. the tokens in the files <strong>must</strong>
  199. be of the form <code>@<var>token</var>@</code>, where
  200. <var>token</var> is the token name that is set
  201. in the <code>&lt;filter&gt;</code> task. This token syntax matches the syntax of other build systems
  202. that perform such filtering and remains sufficiently orthogonal to most
  203. programming and scripting languages, as well as with documentation systems.</p>
  204. <p><strong>Note</strong>: If a token with the format <code>@<var>token</var>@</code>
  205. is found in a file, but no
  206. filter is associated with that token, no changes take place;
  207. therefore, no escaping
  208. method is available&mdash;but as long as you choose appropriate names for your
  209. tokens, this should not cause problems.</p>
  210. <p><strong>Warning</strong>: If you copy binary files with filtering turned on, you can corrupt the
  211. files. This feature should be used with text files <em>only</em>.</p>
  212. <h3 id="path">Path-like Structures</h3>
  213. <p>You can specify <code>PATH</code>- and <code>CLASSPATH</code>-type
  214. references using both <q>:</q> and <q>;</q> as separator
  215. characters. Ant will
  216. convert the separator to the correct character of the current operating
  217. system.</p>
  218. <p>Wherever path-like values need to be specified, a nested element can
  219. be used. This takes the general form of:</p>
  220. <pre>
  221. &lt;classpath&gt;
  222. &lt;pathelement path=&quot;${classpath}&quot;/&gt;
  223. &lt;pathelement location=&quot;lib/helper.jar&quot;/&gt;
  224. &lt;/classpath&gt;</pre>
  225. <p>The <var>location</var> attribute specifies a single file or
  226. directory relative to the project's base directory (or an absolute
  227. filename), while the <var>path</var> attribute accepts colon-
  228. or semicolon-separated lists of locations. The <var>path</var>
  229. attribute is intended to be used with predefined paths&mdash;in any other
  230. case, multiple elements with <var>location</var> attributes should be
  231. preferred.</p>
  232. <p><em>Since Ant 1.8.2</em> the <var>location</var> attribute can also contain a
  233. wildcard in its last path component (i.e. it can end in a
  234. <q>*</q>) in order to support wildcard <code>CLASSPATH</code>s introduced
  235. with Java 6. Ant will not expand or evaluate the wildcards and the
  236. resulting path may not work as anything else but a <code>CLASSPATH</code>&mdash;or
  237. even as a <code>CLASSPATH</code> for JVM prior to Java 6.</p>
  238. <p>As a shortcut, the <code>&lt;classpath&gt;</code> tag
  239. supports <var>path</var> and
  240. <var>location</var> attributes of its own, so:</p>
  241. <pre>
  242. &lt;classpath&gt;
  243. &lt;pathelement path=&quot;${classpath}&quot;/&gt;
  244. &lt;/classpath&gt;
  245. </pre>
  246. <p>can be abbreviated to:</p>
  247. <pre>&lt;classpath path=&quot;${classpath}&quot;/&gt;</pre>
  248. <p>In addition, one or more
  249. <a href="Types/resources.html#collection">resource collections</a>
  250. can be specified as nested elements (these must consist of
  251. <a href="Types/resources.html#file">file</a>-type resources only).
  252. Additionally, it should be noted that although resource collections are
  253. processed in the order encountered, certain resource collection types
  254. such as <a href="Types/fileset.html">fileset</a>,
  255. <a href="Types/dirset.html">dirset</a> and
  256. <a href="Types/resources.html#files">files</a>
  257. are undefined in terms of order.</p>
  258. <pre>
  259. &lt;classpath&gt;
  260. &lt;pathelement path=&quot;${classpath}&quot;/&gt;
  261. &lt;fileset dir=&quot;lib&quot;&gt;
  262. &lt;include name=&quot;**/*.jar&quot;/&gt;
  263. &lt;/fileset&gt;
  264. &lt;pathelement location=&quot;classes&quot;/&gt;
  265. &lt;dirset dir=&quot;${build.dir}&quot;&gt;
  266. &lt;include name=&quot;apps/**/classes&quot;/&gt;
  267. &lt;exclude name=&quot;apps/**/*Test*&quot;/&gt;
  268. &lt;/dirset&gt;
  269. &lt;filelist refid=&quot;third-party_jars&quot;/&gt;
  270. &lt;/classpath&gt;</pre>
  271. <p>This builds a path that holds the value of <samp>${classpath}</samp>,
  272. followed by all jar files in the <samp>lib</samp> directory,
  273. the <samp>classes</samp> directory, all directories named
  274. <samp>classes</samp> under the <samp>apps</samp> subdirectory of
  275. <samp>${build.dir}</samp>, except those
  276. that have the text <code>Test</code> in their name, and
  277. the files specified in the referenced FileList.</p>
  278. <p>If you want to use the same path-like structure for several tasks,
  279. you can define them with a <code>&lt;path&gt;</code> element at the
  280. same level as <code>&lt;target&gt;</code>s, and reference them via their
  281. <var>id</var> attribute&mdash;see <a href="#references">References</a> for an
  282. example.</p>
  283. <p>By default a path-like structure will re-evaluate all nested
  284. resource collections whenever it is used, which may lead to
  285. unnecessary re-scanning of the filesystem. <em>Since Ant 1.8.0</em>, path has
  286. an optional <var>cache</var> attribute, if it is set to <q>true</q>, the path
  287. instance will only scan its nested resource collections once and
  288. assume it doesn't change during the build anymore (the default
  289. for <var>cache</var> still is <q>false</q>). Even if you are using the
  290. path only in a single task it may improve overall performance to set
  291. <var>cache</var> to <q>true</q> if you are using complex nested
  292. constructs.</p>
  293. <p>A path-like structure can include a reference to another path-like
  294. structure (a path being itself a resource collection)
  295. via nested <code>&lt;path&gt;</code> elements:</p>
  296. <pre>
  297. &lt;path id=&quot;base.path&quot;&gt;
  298. &lt;pathelement path=&quot;${classpath}&quot;/&gt;
  299. &lt;fileset dir=&quot;lib&quot;&gt;
  300. &lt;include name=&quot;**/*.jar&quot;/&gt;
  301. &lt;/fileset&gt;
  302. &lt;pathelement location=&quot;classes&quot;/&gt;
  303. &lt;/path&gt;
  304. &lt;path id=&quot;tests.path&quot; cache=&quot;true&quot;&gt;
  305. &lt;path refid=&quot;base.path&quot;/&gt;
  306. &lt;pathelement location=&quot;testclasses&quot;/&gt;
  307. &lt;/path&gt;</pre>
  308. The shortcuts previously mentioned for <code>&lt;classpath&gt;</code>
  309. are also valid for <code>&lt;path&gt;</code>. For example:
  310. <pre>
  311. &lt;path id=&quot;base.path&quot;&gt;
  312. &lt;pathelement path=&quot;${classpath}&quot;/&gt;
  313. &lt;/path&gt;</pre>
  314. can be written as:
  315. <pre>&lt;path id=&quot;base.path&quot; path=&quot;${classpath}&quot;/&gt;</pre>
  316. <h4 id="pathshortcut">Path Shortcut</h4>
  317. <p>
  318. <em>Since Ant 1.6</em>, there is a shortcut for converting paths to OS specific
  319. strings in properties. One can use the expression
  320. <samp>${toString:<em>pathreference</em>}</samp> to convert a path element
  321. reference to a string that can be used for a path argument. For example:
  322. </p>
  323. <pre>
  324. &lt;path id="lib.path.ref"&gt;
  325. &lt;fileset dir="lib" includes="*.jar"/&gt;
  326. &lt;/path&gt;
  327. &lt;javac srcdir="src" destdir="classes"&gt;
  328. &lt;compilerarg arg="-Xbootclasspath/p:${toString:lib.path.ref}"/&gt;
  329. &lt;/javac&gt;</pre>
  330. <h3 id="arg">Command-line Arguments</h3>
  331. <p>Several tasks take arguments that will be passed to another
  332. process on the command line. To make it easier to specify arguments
  333. that contain space characters, nested <code>arg</code> elements can be used.</p>
  334. <table class="attr">
  335. <tr>
  336. <th>Attribute</th>
  337. <th>Description</th>
  338. <th>Required</th>
  339. </tr>
  340. <tr>
  341. <td>value</td>
  342. <td>a single command-line argument; can contain space
  343. characters.</td>
  344. <td rowspan="5">Exactly one of these.</td>
  345. </tr>
  346. <tr>
  347. <td>file</td>
  348. <td class="left">The name of a file as a single command-line
  349. argument; will be replaced with the absolute filename of the file.</td>
  350. </tr>
  351. <tr>
  352. <td>path</td>
  353. <td class="left">A string that will be treated as a path-like
  354. string as a single command-line argument; you can use <q>;</q>
  355. or <q>:</q> as
  356. path separators and Ant will convert it to the platform's local
  357. conventions.</td>
  358. </tr>
  359. <tr>
  360. <td>pathref</td>
  361. <td class="left"><a href="#references">Reference</a> to a path
  362. defined elsewhere. Ant will convert it to the platform's local
  363. conventions.</td>
  364. </tr>
  365. <tr>
  366. <td>line</td>
  367. <td class="left">a space-delimited list of command-line arguments.</td>
  368. </tr>
  369. <tr>
  370. <td>prefix</td>
  371. <td>A fixed string to be placed in front of the
  372. argument. In the case of a line broken into parts, it will be
  373. placed in front of every part. <em>Since Ant 1.8.</em></td>
  374. <td>No</td>
  375. </tr>
  376. <tr>
  377. <td>suffix</td>
  378. <td>A fixed string to be placed immediately after the
  379. argument. In the case of a line broken into parts, it will be
  380. placed after every part. <em>Since Ant 1.8.</em></td>
  381. <td>No</td>
  382. </tr>
  383. </table>
  384. <p>It is highly recommended to avoid the <var>line</var> version
  385. when possible. Ant will try to split the command line in a way
  386. similar to what a (Unix) shell would do, but may create something that
  387. is very different from what you expect under some circumstances.</p>
  388. <h4>Examples</h4>
  389. <pre>&lt;arg value=&quot;-l -a&quot;/&gt;</pre>
  390. <p>is a single command-line argument containing a space character,
  391. <em>not</em> separate options <q>-l</q> and <q>-a</q>.</p>
  392. <pre>&lt;arg line=&quot;-l -a&quot;/&gt;</pre>
  393. <p>This is a command line with two separate options, <q>-l</q> and <q>-a</q>.</p>
  394. <pre>&lt;arg path=&quot;/dir;/dir2:\dir3&quot;/&gt;</pre>
  395. <p>is a single command-line argument with the value
  396. <code>\dir;\dir2;\dir3</code> on DOS-based systems and
  397. <code>/dir:/dir2:/dir3</code> on Unix(-like) systems.</p>
  398. <h3 id="references">References</h3>
  399. <p>Any project element can be assigned an identifier using its
  400. <var>id</var> attribute. In most cases the element can subsequently
  401. be referenced by specifying the <var>refid</var> attribute on an
  402. element of the same type. This can be useful if you are going to
  403. replicate the same snippet of XML over and over again&mdash;using a
  404. <code>&lt;classpath&gt;</code> structure more than once, for example.</p>
  405. <p>The following example:</p>
  406. <pre>
  407. &lt;project ... &gt;
  408. &lt;target ... &gt;
  409. &lt;rmic ...&gt;
  410. &lt;classpath&gt;
  411. &lt;pathelement location=&quot;lib/&quot;/&gt;
  412. &lt;pathelement path=&quot;${java.class.path}/&quot;/&gt;
  413. &lt;pathelement path=&quot;${additional.path}&quot;/&gt;
  414. &lt;/classpath&gt;
  415. &lt;/rmic&gt;
  416. &lt;/target&gt;
  417. &lt;target ... &gt;
  418. &lt;javac ...&gt;
  419. &lt;classpath&gt;
  420. &lt;pathelement location=&quot;lib/&quot;/&gt;
  421. &lt;pathelement path=&quot;${java.class.path}/&quot;/&gt;
  422. &lt;pathelement path=&quot;${additional.path}&quot;/&gt;
  423. &lt;/classpath&gt;
  424. &lt;/javac&gt;
  425. &lt;/target&gt;
  426. &lt;/project&gt;</pre>
  427. <p>could be rewritten as:</p>
  428. <pre>
  429. &lt;project ... &gt;
  430. &lt;path id=&quot;project.class.path&quot;&gt;
  431. &lt;pathelement location=&quot;lib/&quot;/&gt;
  432. &lt;pathelement path=&quot;${java.class.path}/&quot;/&gt;
  433. &lt;pathelement path=&quot;${additional.path}&quot;/&gt;
  434. &lt;/path&gt;
  435. &lt;target ... &gt;
  436. &lt;rmic ...&gt;
  437. &lt;classpath refid=&quot;project.class.path&quot;/&gt;
  438. &lt;/rmic&gt;
  439. &lt;/target&gt;
  440. &lt;target ... &gt;
  441. &lt;javac ...&gt;
  442. &lt;classpath refid=&quot;project.class.path&quot;/&gt;
  443. &lt;/javac&gt;
  444. &lt;/target&gt;
  445. &lt;/project&gt;</pre>
  446. <p>All tasks that use nested elements for
  447. <a href="Types/patternset.html">PatternSet</a>s,
  448. <a href="Types/fileset.html">FileSet</a>s,
  449. <a href="Types/zipfileset.html">ZipFileSet</a>s or
  450. <a href="#path">path-like structures</a> accept references to these structures
  451. as shown in the examples. Using <var>refid</var> on a task will ordinarily
  452. have the same effect (referencing a task already declared), but the user
  453. should be aware that the interpretation of this attribute is dependent on the
  454. implementation of the element upon which it is specified. Some tasks (the
  455. <a href="Tasks/property.html">property</a> task is a handy example)
  456. deliberately assign a different meaning to <var>refid</var>.</p>
  457. <h3 id="external-tasks">Use of external tasks</h3>
  458. Ant supports a plugin mechanism for using third party tasks. For using them you
  459. have to do two steps:
  460. <ol>
  461. <li>place their implementation somewhere where Ant can find them.</li>
  462. <li>declare them.</li>
  463. </ol>
  464. Don't add anything to the <code>CLASSPATH</code> environment variable&mdash;this is often the
  465. reason for very obscure errors. Use Ant's own <a href="install.html#optionalTasks">mechanisms</a>
  466. for adding libraries:
  467. <ul>
  468. <li>via command line argument <code>-lib</code></li>
  469. <li>adding to <code>${user.home}/.ant/lib</code></li>
  470. <li>adding to <code>${ant.home}/lib</code></li>
  471. </ul>
  472. For the declaration there are several ways:
  473. <ul>
  474. <li>declare a single task per using instruction using
  475. <code>&lt;<a href="Tasks/taskdef.html">taskdef</a> name=&quot;taskname&quot;
  476. classname=&quot;ImplementationClass&quot;/&gt;</code>
  477. <br/>
  478. <code>&lt;taskdef name=&quot;for&quot; classname=&quot;net.sf.antcontrib.logic.For&quot;/&gt;
  479. &lt;for ... /&gt;</code>
  480. </li>
  481. <li>declare a bundle of tasks using a <samp>properties</samp> file holding these
  482. taskname-ImplementationClass-pairs and <code>&lt;taskdef&gt;</code>
  483. <br/>
  484. <code>&lt;taskdef resource=&quot;net/sf/antcontrib/antcontrib.properties&quot;/&gt;
  485. &lt;for ... /&gt;</code>
  486. </li>
  487. <li>declare a bundle of tasks using
  488. an <a href="Types/antlib.html">xml file</a> holding these
  489. taskname-ImplementationClass-pairs and <code>&lt;taskdef&gt;</code>
  490. <br/>
  491. <code>&lt;taskdef resource=&quot;net/sf/antcontrib/antlib.xml&quot;/&gt;
  492. &lt;for ... /&gt;</code>
  493. </li>
  494. <li>declare a bundle of tasks using an xml file named <samp>antlib.xml</samp>, XML namespace and
  495. <a href="Types/antlib.html#antlibnamespace"><code>antlib:</code> protocol handler</a>
  496. <br/>
  497. <code>&lt;project xmlns:ac=&quot;antlib:net.sf.antcontrib&quot;/&gt;
  498. &lt;ac:for ... /&gt;</code>
  499. </li>
  500. </ul>
  501. If you need a special function, you should
  502. <ol>
  503. <li>have a look at this manual, because Ant provides lot of tasks</li>
  504. <li>have a look at the external task page <a href="https://ant.apache.org/external.html">online</a></li>
  505. <li>have a look at the external task <a href="https://wiki.apache.org/ant/AntExternalTaskdefs">wiki
  506. page</a></li>
  507. <li>ask on the <a href="https://ant.apache.org/mail.html#User%20List">Ant user</a> list</li>
  508. <li><a href="tutorial-writing-tasks.html">implement</a> (and share) your own</li>
  509. </ol>
  510. </body>
  511. </html>