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

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