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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Language" content="en-us">
  4. <title>Apache Ant User Manual</title>
  5. </head>
  6. <body>
  7. <h1>Using Ant</h1>
  8. <h2><a name="buildfile">Writing a Simple Buildfile</a></h2>
  9. <p>Ant's buildfiles are written in XML. Each buildfile contains one project
  10. and at least one (default) target. Targets contain task elements.
  11. Each task element of the buildfile can have an <code>id</code> attribute and
  12. can later be referred to by the value supplied to this. The value has
  13. to be unique. (For additional information, see the
  14. <a href="#tasks"> Tasks</a> section below.)</p>
  15. <h3><a name="projects">Projects</a></h3>
  16. <p>A <i>project</i> has three attributes:</p>
  17. <table border="1" cellpadding="2" cellspacing="0">
  18. <tr>
  19. <td valign="top"><b>Attribute</b></td>
  20. <td valign="top"><b>Description</b></td>
  21. <td align="center" valign="top"><b>Required</b></td>
  22. </tr>
  23. <tr>
  24. <td valign="top">name</td>
  25. <td valign="top">the name of the project.</td>
  26. <td align="center" valign="top">No</td>
  27. </tr>
  28. <tr>
  29. <td valign="top">default</td>
  30. <td valign="top">the default target to use when no target is supplied.</td>
  31. <td align="center" valign="top">Yes</td>
  32. </tr>
  33. <tr>
  34. <td valign="top">basedir</td>
  35. <td valign="top">the base directory from which all path calculations are
  36. done. This attribute might be overridden by setting
  37. the &quot;basedir&quot;
  38. property beforehand. When this is done, it must be omitted in the
  39. project tag. If neither the attribute nor the property have
  40. been set, the parent directory of the buildfile will be used.</td>
  41. <td align="center" valign="top">No</td>
  42. </tr>
  43. </table>
  44. <p>Optionally, a description for the project can be provided as a
  45. top-level <code>&lt;description&gt;</code> element (see the <a
  46. href="CoreTypes/description.html">description</a> type).</p>
  47. <p>Each project defines one or more <i>targets</i>.
  48. A target is a set of <i>tasks</i> you want
  49. to be executed. When starting Ant, you can select which target(s) you
  50. want to have executed. When no target is given,
  51. the project's default is used.</p>
  52. <h3><a name="targets">Targets</a></h3>
  53. <p>A target can depend on other targets. You might have a target for compiling,
  54. for example, and a target for creating a distributable. You can only build a
  55. distributable when you have compiled first, so the distribute target
  56. <i>depends on</i> the compile target. Ant resolves these dependencies.</p>
  57. <p>It should be noted, however, that Ant's <code>depends</code> attribute
  58. only specifies the <i>order</i> in which targets should be executed - it
  59. does not affect whether the target that specifies the dependency(s) gets
  60. executed if the dependent target(s) did not (need to) run.
  61. </p>
  62. <p>Ant tries to execute the targets in the <code>depends</code>
  63. attribute in the order
  64. they appear (from left to right). Keep in mind that it is possible that a target
  65. can get executed earlier when an earlier target depends on it:</p>
  66. <blockquote>
  67. <pre>&lt;target name=&quot;A&quot;/&gt;
  68. &lt;target name=&quot;B&quot; depends=&quot;A&quot;/&gt;
  69. &lt;target name=&quot;C&quot; depends=&quot;B&quot;/&gt;
  70. &lt;target name=&quot;D&quot; depends=&quot;C,B,A&quot;/&gt;</pre>
  71. </blockquote>
  72. <p>Suppose we want to execute target D. From its
  73. <code>depends</code> attribute, you
  74. might think that first target C, then B and then A is executed.
  75. Wrong! C depends on B, and B depends on A, so first A is executed, then B, then C, and finally D.</p>
  76. <p>A target gets executed only once, even when more than one target
  77. depends on it (see the previous example).</p>
  78. <p>A target also has the ability to perform its execution if (or
  79. unless) a property has been set. This allows, for example, better
  80. control on the building process depending on the state of the system
  81. (java version, OS, command-line property defines, etc.). To make a target
  82. <i>sense</i> this property, you should add the <code>if</code> (or
  83. <code>unless</code>) attribute with the name of the property that the target
  84. should react to. For example:</p>
  85. <blockquote>
  86. <pre>&lt;target name=&quot;build-module-A&quot; if=&quot;module-A-present&quot;/&gt;</pre>
  87. <pre>&lt;target name=&quot;build-own-fake-module-A&quot; unless=&quot;module-A-present&quot;/&gt;</pre>
  88. </blockquote>
  89. <p>If no <code>if</code> and no <code>unless</code> attribute is present,
  90. the target will always be executed.</p>
  91. <p>The optional <code>description</code> attribute can be used to provide a one-line description of this target, which is printed by the
  92. <nobr><code>-projecthelp</code></nobr> command-line option.</p>
  93. <p>It is a good practice to place your <a
  94. href="CoreTasks/tstamp.html">tstamp</a> tasks in a so-called
  95. <i>initialization</i> target, on which
  96. all other targets depend. Make sure that target is always the first one in
  97. the depends list of the other targets. In this manual, most initialization targets
  98. have the name &quot;init&quot;.</p>
  99. <p>A target has the following attributes:</p>
  100. <table border="1" cellpadding="2" cellspacing="0">
  101. <tr>
  102. <td valign="top"><b>Attribute</b></td>
  103. <td valign="top"><b>Description</b></td>
  104. <td align="center" valign="top"><b>Required</b></td>
  105. </tr>
  106. <tr>
  107. <td valign="top">name</td>
  108. <td valign="top">the name of the target.</td>
  109. <td align="center" valign="top">Yes</td>
  110. </tr>
  111. <tr>
  112. <td valign="top">depends</td>
  113. <td valign="top">a comma-separated list of names of targets on which this
  114. target depends.</td>
  115. <td align="center" valign="top">No</td>
  116. </tr>
  117. <tr>
  118. <td valign="top">if</td>
  119. <td valign="top">the name of the property that must be set in order for this
  120. target to execute.</td>
  121. <td align="center" valign="top">No</td>
  122. </tr>
  123. <tr>
  124. <td valign="top">unless</td>
  125. <td valign="top">the name of the property that must not be set in order
  126. for this target to execute.</td>
  127. <td align="center" valign="top">No</td>
  128. </tr>
  129. <tr>
  130. <td valign="top">description</td>
  131. <td valign="top">a short description of this target's function.</td>
  132. <td align="center" valign="top">No</td>
  133. </tr>
  134. </table>
  135. <h3><a name="tasks">Tasks</a></h3>
  136. <p>A task is a piece of code that can be executed.</p>
  137. <p>A task can have multiple attributes (or arguments, if you prefer). The value
  138. of an attribute might contain references to a property. These references will be
  139. resolved before the task is executed.</p>
  140. <p>Tasks have a common structure:</p>
  141. <blockquote>
  142. <pre>&lt;<i>name</i> <i>attribute1</i>=&quot;<i>value1</i>&quot; <i>attribute2</i>=&quot;<i>value2</i>&quot; ... /&gt;</pre>
  143. </blockquote>
  144. <p>where <i>name</i> is the name of the task,
  145. <i>attributeN</i> is the attribute name, and
  146. <i>valueN</i> is the value for this attribute.</p>
  147. <p>There is a set of <a href="coretasklist.html" target="navFrame">built-in tasks</a>, along with a
  148. number of
  149. <a href="optionaltasklist.html" target="navFrame"> optional tasks</a>, but it is also very
  150. easy to <a href="develop.html#writingowntask">write your own</a>.</p>
  151. <p>All tasks share a task name attribute. The value of
  152. this attribute will be used in the logging messages generated by
  153. Ant.</p>
  154. Tasks can be assigned an <code>id</code> attribute:
  155. <blockquote>
  156. <pre>&lt;<i>taskname</i> id="<i>taskID</i>" ... /&gt;</pre>
  157. </blockquote>
  158. where <i>taskname</i> is the name of the task, and <i>taskID</i> is
  159. a unique identifier for this task.
  160. You can refer to the
  161. corresponding task object in scripts or other tasks via this name.
  162. For example, in scripts you could do:
  163. <blockquote>
  164. <pre>
  165. &lt;script ... &gt;
  166. task1.setFoo("bar");
  167. &lt;/script&gt;
  168. </pre>
  169. </blockquote>
  170. to set the <code>foo</code> attribute of this particular task instance.
  171. In another task (written in Java), you can access the instance via
  172. <code>project.getReference("task1")</code>.
  173. <p>
  174. Note<sup>1</sup>: If "task1" has not been run yet, then
  175. it has not been configured (ie., no attributes have been set), and if it is
  176. going to be configured later, anything you've done to the instance may
  177. be overwritten.
  178. </p>
  179. <p>
  180. Note<sup>2</sup>: Future versions of Ant will most likely <i>not</i>
  181. be backward-compatible with this behaviour, since there will likely be no
  182. task instances at all, only proxies.
  183. </p>
  184. <h3><a name="properties">Properties</a></h3>
  185. <p>A project can have a set of properties. These might be set in the buildfile
  186. by the <a href="CoreTasks/property.html">property</a> task, or might be set outside Ant. A
  187. property has a name and a value; the name is case-sensitive. Properties may be used in the value of
  188. task attributes. This is done by placing the property name between
  189. &quot;<code>${</code>&quot; and &quot;<code>}</code>&quot; in the
  190. attribute value. For example,
  191. if there is a &quot;builddir&quot; property with the value
  192. &quot;build&quot;, then this could be used in an attribute like this:
  193. <code>${builddir}/classes</code>.
  194. This is resolved at run-time as <code>build/classes</code>.</p>
  195. <h3><a name="built-in-props">Built-in Properties</a></h3>
  196. <p>Ant provides access to all system properties as if they had been
  197. defined using a <code>&lt;property&gt;</code> task.
  198. For example, <code>${os.name}</code> expands to the
  199. name of the operating system.</p>
  200. <p>For a list of system properties see
  201. <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/System.html#getProperties()">the Javadoc of System.getProperties</a>.
  202. </p>
  203. <p>In addition, Ant has some built-in properties:</p>
  204. <pre>
  205. basedir the absolute path of the project's basedir (as set
  206. with the basedir attribute of &lt;project&gt;).
  207. ant.file the absolute path of the buildfile.
  208. ant.version the version of Ant
  209. ant.project.name the name of the project that is currently executing;
  210. it is set in the name attribute of &lt;project&gt;.
  211. ant.java.version the JVM version Ant detected; currently it can hold
  212. the values &quot;1.1&quot;, &quot;1.2&quot;, &quot;1.3&quot; and &quot;1.4&quot;.
  213. </pre>
  214. <a name="example"><h3>Example Buildfile</h3></a>
  215. <pre>
  216. &lt;project name=&quot;MyProject&quot; default=&quot;dist&quot; basedir=&quot;.&quot;&gt;
  217. &lt;!-- set global properties for this build --&gt;
  218. &lt;property name=&quot;src&quot; value=&quot;.&quot;/&gt;
  219. &lt;property name=&quot;build&quot; value=&quot;build&quot;/&gt;
  220. &lt;property name=&quot;dist&quot; value=&quot;dist&quot;/&gt;
  221. &lt;target name=&quot;init&quot;&gt;
  222. &lt;!-- Create the time stamp --&gt;
  223. &lt;tstamp/&gt;
  224. &lt;!-- Create the build directory structure used by compile --&gt;
  225. &lt;mkdir dir=&quot;${build}&quot;/&gt;
  226. &lt;/target&gt;
  227. &lt;target name=&quot;compile&quot; depends=&quot;init&quot;&gt;
  228. &lt;!-- Compile the java code from ${src} into ${build} --&gt;
  229. &lt;javac srcdir=&quot;${src}&quot; destdir=&quot;${build}&quot;/&gt;
  230. &lt;/target&gt;
  231. &lt;target name=&quot;dist&quot; depends=&quot;compile&quot;&gt;
  232. &lt;!-- Create the distribution directory --&gt;
  233. &lt;mkdir dir=&quot;${dist}/lib&quot;/&gt;
  234. &lt;!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file --&gt;
  235. &lt;jar jarfile=&quot;${dist}/lib/MyProject-${DSTAMP}.jar&quot; basedir=&quot;${build}&quot;/&gt;
  236. &lt;/target&gt;
  237. &lt;target name=&quot;clean&quot;&gt;
  238. &lt;!-- Delete the ${build} and ${dist} directory trees --&gt;
  239. &lt;delete dir=&quot;${build}&quot;/&gt;
  240. &lt;delete dir=&quot;${dist}&quot;/&gt;
  241. &lt;/target&gt;
  242. &lt;/project&gt;
  243. </pre>
  244. <a name="filters"><h3>Token Filters</h3></a>
  245. <p>A project can have a set of tokens that might be automatically expanded if
  246. found when a file is copied, when the filtering-copy behavior is selected in the
  247. tasks that support this. These might be set in the buildfile
  248. by the <a href="CoreTasks/filter.html">filter</a> task.</p>
  249. <p>Since this can potentially be a very harmful behavior,
  250. the tokens in the files <b>must</b>
  251. be of the form <code>@</code><i>token</i><code>@</code>, where
  252. <i>token</i> is the token name that is set
  253. in the <code>&lt;filter&gt;</code> task. This token syntax matches the syntax of other build systems
  254. that perform such filtering and remains sufficiently orthogonal to most
  255. programming and scripting languages, as well as with documentation systems.</p>
  256. <p>Note: If a token with the format <code>@</code><i>token</i><code>@</code>
  257. is found in a file, but no
  258. filter is associated with that token, no changes take place;
  259. therefore, no escaping
  260. method is available - but as long as you choose appropriate names for your
  261. tokens, this should not cause problems.</p>
  262. <p><b>Warning:</b> If you copy binary files with filtering turned on, you can corrupt the
  263. files. This feature should be used with text files <em>only</em>.</p>
  264. <h3><a name="path">Path-like Structures</a></h3>
  265. <p>You can specify <code>PATH</code>- and <code>CLASSPATH</code>-type
  266. references using both
  267. &quot;<code>:</code>&quot; and &quot;<code>;</code>&quot; as separator
  268. characters. Ant will
  269. convert the separator to the correct character of the current operating
  270. system.</p>
  271. <p>Wherever path-like values need to be specified, a nested element can
  272. be used. This takes the general form of:</p>
  273. <pre>
  274. &lt;classpath&gt;
  275. &lt;pathelement path=&quot;${classpath}&quot;/&gt;
  276. &lt;pathelement location=&quot;lib/helper.jar&quot;/&gt;
  277. &lt;/classpath&gt;
  278. </pre>
  279. <p>The <code>location</code> attribute specifies a single file or
  280. directory relative to the project's base directory (or an absolute
  281. filename), while the <code>path</code> attribute accepts colon-
  282. or semicolon-separated lists of locations. The <code>path</code>
  283. attribute is intended to be used with predefined paths - in any other
  284. case, multiple elements with <code>location</code> attributes should be
  285. preferred.</p>
  286. <p>As a shortcut, the <code>&lt;classpath&gt;</code> tag
  287. supports <code>path</code> and
  288. <code>location</code> attributes of its own, so:</p>
  289. <pre>
  290. &lt;classpath&gt;
  291. &lt;pathelement path=&quot;${classpath}&quot;/&gt;
  292. &lt;/classpath&gt;
  293. </pre>
  294. <p>can be abbreviated to:</p>
  295. <pre>
  296. &lt;classpath path=&quot;${classpath}&quot;/&gt;
  297. </pre>
  298. <p>In addition, <a href="CoreTypes/fileset.html">FileSet</a>s can be specified via
  299. nested <code>&lt;fileset&gt;</code> elements. The order in which the files
  300. building up a fileset are added to the path-like structure is not
  301. defined.</p>
  302. <pre>
  303. &lt;classpath&gt;
  304. &lt;pathelement path=&quot;${classpath}&quot;/&gt;
  305. &lt;fileset dir=&quot;lib&quot;&gt;
  306. &lt;include name=&quot;**/*.jar&quot;/&gt;
  307. &lt;/fileset&gt;
  308. &lt;pathelement location=&quot;classes&quot;/&gt;
  309. &lt;/classpath&gt;
  310. </pre>
  311. <p>This builds a path that holds the value of <code>${classpath}</code>,
  312. followed by all jar files in the <code>lib</code> directory, followed
  313. by the <code>classes</code> directory.</p>
  314. <p>If you want to use the same path-like structure for several tasks,
  315. you can define them with a <code>&lt;path&gt;</code> element at the
  316. same level as <i>target</i>s, and reference them via their
  317. <i>id</i> attribute - see <a href="#references">References</a> for an
  318. example.</p>
  319. <p>A path-like structure can include a reference to another path-like
  320. structure via nested <code>&lt;path&gt;</code> elements:</p>
  321. <pre>
  322. &lt;path id=&quot;base.path&quot;&gt;
  323. &lt;pathelement path=&quot;${classpath}&quot;/&gt;
  324. &lt;fileset dir=&quot;lib&quot;&gt;
  325. &lt;include name=&quot;**/*.jar&quot;/&gt;
  326. &lt;/fileset&gt;
  327. &lt;pathelement location=&quot;classes&quot;/&gt;
  328. &lt;/path&gt;
  329. &lt;path id=&quot;tests.path&quot;&gt;
  330. &lt;path refid=&quot;base.path&quot;/&gt;
  331. &lt;pathelement location=&quot;testclasses&quot;/&gt;
  332. &lt;/path&gt;
  333. </pre>
  334. The shortcuts previously mentioned for <code>&lt;classpath&gt;</code> are also valid for <code>&lt;path&gt;</code>.For example:
  335. <pre>
  336. &lt;path id=&quot;base.path&quot;&gt;
  337. &lt;pathelement path=&quot;${classpath}&quot;/&gt;
  338. &lt;/path&gt;
  339. </pre>
  340. can be written as:
  341. <pre>
  342. &lt;path id=&quot;base.path&quot; path=&quot;${classpath}&quot;/&gt;
  343. </pre>
  344. <h3><a name="arg">Command-line Arguments</a></h3>
  345. <p>Several tasks take arguments that will be passed to another
  346. process on the command line. To make it easier to specify arguments
  347. that contain space characters, nested <code>arg</code> elements can be used.</p>
  348. <table border="1" cellpadding="2" cellspacing="0">
  349. <tr>
  350. <td width="12%" valign="top"><b>Attribute</b></td>
  351. <td width="78%" valign="top"><b>Description</b></td>
  352. <td width="10%" valign="top"><b>Required</b></td>
  353. </tr>
  354. <tr>
  355. <td valign="top">value</td>
  356. <td valign="top">a single command-line argument; can contain space
  357. characters.</td>
  358. <td align="center" rowspan="4">Exactly one of these.</td>
  359. </tr>
  360. <tr>
  361. <td valign="top">line</td>
  362. <td valign="top">a space-delimited list of command-line arguments.</td>
  363. </tr>
  364. <tr>
  365. <td valign="top">file</td>
  366. <td valign="top">The name of a file as a single command-line
  367. argument; will be replaced with the absolute filename of the file.</td>
  368. </tr>
  369. <tr>
  370. <td valign="top">path</td>
  371. <td valign="top">A string that will be treated as a path-like
  372. string as a single command-line argument; you can use <code>;</code>
  373. or <code>:</code> as
  374. path separators and Ant will convert it to the platform's local
  375. conventions.</td>
  376. </tr>
  377. </table>
  378. <h4>Examples</h4>
  379. <blockquote><pre>
  380. &lt;arg value=&quot;-l -a&quot;/&gt;
  381. </pre></blockquote>
  382. <p>is a single command-line argument containing a space character.</p>
  383. <blockquote><pre>
  384. &lt;arg line=&quot;-l -a&quot;/&gt;
  385. </pre></blockquote>
  386. <p>represents two separate command-line arguments.</p>
  387. <blockquote><pre>
  388. &lt;arg path=&quot;/dir;/dir2:\dir3&quot;/&gt;
  389. </pre></blockquote>
  390. <p>is a single command-line argument with the value
  391. <code>\dir;\dir2;\dir3</code> on DOS-based systems and
  392. <code>/dir:/dir2:/dir3</code> on Unix-like systems.</p>
  393. <h3><a name="references">References</a></h3>
  394. <p>The <code>id</code> attribute of the buildfile's elements can be
  395. used to refer to them. This can be useful if you are going to replicate
  396. the same snippet of XML over and over again - using a
  397. <code>&lt;classpath&gt;</code> structure more than once, for
  398. example.</p>
  399. <p>The following example:</p>
  400. <blockquote><pre>
  401. &lt;project ... &gt;
  402. &lt;target ... &gt;
  403. &lt;rmic ...&gt;
  404. &lt;classpath&gt;
  405. &lt;pathelement location=&quot;lib/&quot;/&gt;
  406. &lt;pathelement path=&quot;${java.class.path}/&quot;/&gt;
  407. &lt;pathelement path=&quot;${additional.path}&quot;/&gt;
  408. &lt;/classpath&gt;
  409. &lt;/rmic&gt;
  410. &lt;/target&gt;
  411. &lt;target ... &gt;
  412. &lt;javac ...&gt;
  413. &lt;classpath&gt;
  414. &lt;pathelement location=&quot;lib/&quot;/&gt;
  415. &lt;pathelement path=&quot;${java.class.path}/&quot;/&gt;
  416. &lt;pathelement path=&quot;${additional.path}&quot;/&gt;
  417. &lt;/classpath&gt;
  418. &lt;/javac&gt;
  419. &lt;/target&gt;
  420. &lt;/project&gt;
  421. </pre></blockquote>
  422. <p>could be rewritten as:</p>
  423. <blockquote><pre>
  424. &lt;project ... &gt;
  425. &lt;path id=&quot;project.class.path&quot;&gt;
  426. &lt;pathelement location=&quot;lib/&quot;/&gt;
  427. &lt;pathelement path=&quot;${java.class.path}/&quot;/&gt;
  428. &lt;pathelement path=&quot;${additional.path}&quot;/&gt;
  429. &lt;/path&gt;
  430. &lt;target ... &gt;
  431. &lt;rmic ...&gt;
  432. &lt;classpath refid=&quot;project.class.path&quot;/&gt;
  433. &lt;/rmic&gt;
  434. &lt;/target&gt;
  435. &lt;target ... &gt;
  436. &lt;javac ...&gt;
  437. &lt;classpath refid=&quot;project.class.path&quot;/&gt;
  438. &lt;/javac&gt;
  439. &lt;/target&gt;
  440. &lt;/project&gt;
  441. </pre></blockquote>
  442. <p>All tasks that use nested elements for <a
  443. href="CoreTypes/patternset.html">PatternSet</a>s, <a href="CoreTypes/fileset.html">FileSet</a>s or
  444. <a href="#path">path-like structures</a> accept references to these
  445. structures as well.</p>
  446. <hr>
  447. <p align="center">Copyright &copy; 2001 Apache Software Foundation. All rights
  448. Reserved.</p>
  449. </body>
  450. </html>