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

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