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

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