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.

faq.xml 43 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212
  1. <?xml version="1.0"?>
  2. <document>
  3. <properties>
  4. <author email="bodewig@apache.org">Stefan Bodewig</author>
  5. <title>Frequently Asked Questions</title>
  6. </properties>
  7. <faqsection title="About this FAQ">
  8. <faq id="latest-version">
  9. <question>Where do I find the latest version of this
  10. document?</question>
  11. <answer>
  12. <p>The latest version can always be found at Ant&apos;s homepage
  13. <a href="http://ant.apache.org/faq.html">http://ant.apache.org/faq.html</a>.</p>
  14. </answer>
  15. </faq>
  16. <faq id="adding-faqs">
  17. <question>How can I contribute to this FAQ?</question>
  18. <answer>
  19. <p>The page you are looking it is generated from
  20. <a href="http://cvs.apache.org/viewcvs.cgi/~checkout~/ant/xdocs/faq.xml">this</a>
  21. document. If you want to add a new question, please submit
  22. a patch against this document to one of Ant&apos;s mailing lists;
  23. hopefully, the structure is self-explanatory.</p>
  24. <p>If you don&apos;t know how to create a patch, see the patches
  25. section of <a href="http://jakarta.apache.org/site/source.html">this
  26. page</a>.</p>
  27. </answer>
  28. </faq>
  29. <faq id="creating-faq">
  30. <question>How do you create the HTML version of this
  31. FAQ?</question>
  32. <answer>
  33. <p>We use
  34. <a href="http://jakarta.apache.org/velocity/anakia.html">Anakia</a>
  35. to render the HTML version from the original XML file.</p>
  36. <p>The Velocity stylesheets used to process the XML files can
  37. be found in the <code>xdocs/stylesheets</code> subdirectory of
  38. Ant&apos;s CVS repository - the build file
  39. <code>docs.xml</code> at the top level of the ant CVS
  40. module is used to drive Anakia.</p>
  41. <p>This file assumes that you have the
  42. <code>jakarta-site2</code> CVS module checked out as well, but
  43. if you follow the instruction from Anakia&apos;s homepage, you
  44. should get it to work without that. Just make sure all
  45. required jars are in the task&apos;s classpath.</p>
  46. </answer>
  47. </faq>
  48. </faqsection>
  49. <faqsection title="General">
  50. <faq id="what-is-ant">
  51. <question>What is Apache Ant?</question>
  52. <answer>
  53. <p> Ant is a Java-based build tool. In theory, it is kind of
  54. like Make, without Make&apos;s wrinkles and with the full
  55. portability of pure Java code.</p>
  56. </answer>
  57. </faq>
  58. <faq id="ant-name">
  59. <question>Why do you call it Ant?</question>
  60. <answer>
  61. <p>According to Ant&apos;s original author, James Duncan
  62. Davidson, the name is an acronym for &quot;Another Neat
  63. Tool&quot;.</p>
  64. <p>Later explanations go along the lines of &quot;ants
  65. do an extremely good job at building things&quot;, or
  66. &quot;ants are very small and can carry a weight dozens of times
  67. their own&quot; - describing what Ant is intended to
  68. be.</p>
  69. </answer>
  70. </faq>
  71. <faq id="history">
  72. <question>Tell us a little bit about Ant&apos;s history.</question>
  73. <answer>
  74. <p>Initially, Ant was part of the Tomcat code base, when it was
  75. donated to the Apache Software Foundation. It was
  76. created by James Duncan Davidson, who is also the original
  77. author of Tomcat. Ant was there to build Tomcat, nothing
  78. else.</p>
  79. <p>Soon thereafter, several open source Java projects realized
  80. that Ant could solve the problems they had with Makefiles.
  81. Starting with the projects hosted at Jakarta and the old Java
  82. Apache project, Ant spread like a virus and is now the build
  83. tool of choice for a lot of projects.</p>
  84. <p>In January 2000, Ant was moved to a separate CVS module and
  85. was promoted to a project of its own, independent of
  86. Tomcat, and became Apache Ant.</p>
  87. <p>The first version of Ant that was exposed to a larger audience
  88. was the one that shipped with Tomcat&apos;s 3.1 release on 19 April
  89. 2000. This version has later been referred to as Ant
  90. 0.3.1.</p>
  91. <p>The first official release of Ant as a stand-alone product was
  92. Ant 1.1, released on 19 July 2000. The complete release
  93. history:</p>
  94. <table>
  95. <tr>
  96. <th>Ant Version</th>
  97. <th>Release Date</th>
  98. </tr>
  99. <tr>
  100. <td>1.1</td>
  101. <td>19 July 2000</td>
  102. </tr>
  103. <tr>
  104. <td>1.2</td>
  105. <td>24 October 2000</td>
  106. </tr>
  107. <tr>
  108. <td>1.3</td>
  109. <td>3 March 2001</td>
  110. </tr>
  111. <tr>
  112. <td>1.4</td>
  113. <td>3 September 2001</td>
  114. </tr>
  115. <tr>
  116. <td>1.4.1</td>
  117. <td>11 October 2001</td>
  118. </tr>
  119. <tr>
  120. <td>1.5</td>
  121. <td>10 July 2002</td>
  122. </tr>
  123. <tr>
  124. <td>1.5.1</td>
  125. <td>3 October 2002</td>
  126. </tr>
  127. </table>
  128. </answer>
  129. </faq>
  130. </faqsection>
  131. <faqsection title="Installation">
  132. <faq id="no-gnu-tar">
  133. <question>I get checksum errors when I try to extract the
  134. <code>tar.gz</code> distribution file. Why?</question>
  135. <answer>
  136. <p>Ant&apos;s distribution contains file names that are longer
  137. than 100 characters, which is not supported by the standard
  138. tar file format. Several different implementations of tar use
  139. different and incompatible ways to work around this
  140. restriction.</p>
  141. <p>Ant&apos;s &lt;tar&gt; task can create tar archives that use
  142. the GNU tar extension, and this has been used when putting
  143. together the distribution. If you are using a different
  144. version of tar (for example, the one shipping with Solaris),
  145. you cannot use it to extract the archive.</p>
  146. <p>The solution is to either install GNU tar, which can be
  147. found <a href="http://www.gnu.org/software/tar/tar.html">here</a>,
  148. or use the zip archive instead (you can extract it using
  149. <code>jar xf</code>).</p>
  150. </answer>
  151. </faq>
  152. </faqsection>
  153. <faqsection title="How do I ...">
  154. <faq id="adding-external-tasks">
  155. <question>How do I add an external task that I've written to the
  156. page &quot;External Tools and Task&quot;?</question>
  157. <answer>
  158. <!-- <p>Join and post a message to the dev or user mailing-->
  159. <p>Join and post a message to the ant-dev or ant-user mailing
  160. list (one list is enough), including the following
  161. information:</p>
  162. <ul>
  163. <li>the name of the task/tool</li>
  164. <li>a short description of the task/tool</li>
  165. <li>a Compatibility: entry stating with which version(s) of
  166. Ant the tool/task is compatible to</li>
  167. <li>a URL: entry linking to the main page of the tool/task</li>
  168. <li>a Contact: entry containing the email address or the URL
  169. of a webpage for the person or list to contact for issues
  170. related to the tool/task. <strong>Note that we'll add a
  171. link on the page, so any email address added there is not
  172. obfuscated and can (and probably will) be abused by robots
  173. harvesting websites for addresses to spam.</strong></li>
  174. <li>a License: entry containing the type of license for the
  175. tool/task</li>
  176. </ul>
  177. <p>The preferred format for this information is a patch to <a
  178. href="http://cvs.apache.org/viewcvs.cgi/~checkout~/ant/xdocs/external.xml">this</a>
  179. document.</p>
  180. </answer>
  181. </faq>
  182. <faq id="passing-cli-args">
  183. <question>How do I pass parameters from the command line to my
  184. build file?</question>
  185. <answer>
  186. <p>Use properties. Using <code>ant
  187. -D<em>name</em>=<em>value</em></code> lets you define values for
  188. properties on the Ant command line. These properties can then be
  189. used within your build file as
  190. any normal property: <code>${<em>name</em>}</code> will put in
  191. <code><em>value</em></code>.</p>
  192. </answer>
  193. </faq>
  194. <faq id="jikes-switches">
  195. <question>How can I use Jikes-specific command-line
  196. switches?</question>
  197. <answer>
  198. <p>A couple of switches are supported via &quot;magic&quot;
  199. properties:</p>
  200. <table>
  201. <tr>
  202. <th>switch</th>
  203. <th>property</th>
  204. <th>default</th>
  205. </tr>
  206. <tr>
  207. <td>+E</td>
  208. <td>build.compiler.emacs</td>
  209. <td>false == not set</td>
  210. </tr>
  211. <tr>
  212. <td>+P</td>
  213. <td>build.compiler.pedantic</td>
  214. <td>false == not set</td>
  215. </tr>
  216. <tr>
  217. <td>+F</td>
  218. <td>build.compiler.fulldepend</td>
  219. <td>false == not set</td>
  220. </tr>
  221. <tr>
  222. <td><strong>(Only for Ant &lt; 1.4; replaced by the
  223. <code><strong>nowarn</strong></code>
  224. attribute of the <code><strong>&lt;javac&gt;</strong></code>
  225. task after that.)</strong><br></br>-nowarn</td>
  226. <td>build.compiler.warnings</td>
  227. <td>true == not set</td>
  228. </tr>
  229. </table>
  230. <p>With Ant &gt;= 1.5, you can also use nested
  231. <code>&lt;compilerarg&gt;</code> elements with the
  232. <code>&lt;javac&gt;</code> task.</p>
  233. </answer>
  234. </faq>
  235. <faq id="shell-redirect-1">
  236. <question>How do I include a &lt; character in my command-line arguments?</question>
  237. <answer>
  238. <p>The short answer is "Use: <code>&amp;lt;</code>".</p>
  239. <p>The long answer is that this probably won&apos;t do what you
  240. want anyway (see <a href="#shell-redirect-2">the next
  241. section</a>).</p>
  242. </answer>
  243. </faq>
  244. <faq id="shell-redirect-2">
  245. <question>How do I redirect standard input or standard output
  246. in the <code>&lt;exec&gt;</code> task?</question>
  247. <answer>
  248. <p>Say you want to redirect the standard input stream of the
  249. <code>cat</code> command to read from a file, something
  250. like:</p>
  251. <source><![CDATA[
  252. shell-prompt> cat < foo
  253. ]]></source>
  254. <p>and try to translate it into</p>
  255. <source><![CDATA[
  256. <exec executable="cat">
  257. <arg value="&lt;" />
  258. <arg value="foo" />
  259. </exec>
  260. ]]></source>
  261. <p>This will not do what you expect. The input redirection is
  262. performed by your shell, not the command itself, so this
  263. should read:</p>
  264. <source><![CDATA[
  265. <exec executable="/bin/sh">
  266. <arg value="-c" />
  267. <arg value="cat &lt; foo" />
  268. </exec>
  269. ]]></source>
  270. <p>Note that you must use the <code>value</code> attribute of
  271. <code>&lt;arg&gt;</code> in the last element, in order to have
  272. the command passed as a single, quoted argument. Alternatively,
  273. you can use:</p>
  274. <source><![CDATA[
  275. <exec executable="/bin/sh">
  276. <arg line='-c "cat &lt; foo"'/>
  277. </exec>
  278. ]]></source>
  279. <p>Note the double-quotes nested inside the single-quotes.</p>
  280. </answer>
  281. </faq>
  282. <faq id="batch-shell-execute">
  283. <question>How do I execute a batch file or shell script from Ant?</question>
  284. <answer>
  285. <p>On native Unix systems, you should be able to run shell scripts
  286. directly. On systems running a Unix-type shell (for example, Cygwin
  287. on Windows) execute the (command) shell instead - <code>cmd</code>
  288. for batch files, <code>sh</code> for shell scripts - then pass the
  289. batch file or shell script (plus any arguments to the script)
  290. as a single command, using the <code>/c</code> or
  291. <code>-c</code> switch, respectively. See
  292. <a href="#shell-redirect-2">the above section</a>
  293. for example <code>&lt;exec&gt;</code> tasks
  294. executing <code>sh</code>. For batch files, use something like:</p>
  295. <source><![CDATA[
  296. <exec dir="." executable="cmd" os="Windows NT">
  297. <arg line="/c test.bat"/>
  298. </exec>
  299. ]]></source>
  300. </answer>
  301. </faq>
  302. <faq id="multi-conditions">
  303. <question>I want to execute a particular target only if
  304. multiple conditions are true.</question>
  305. <answer>
  306. <p>There are actually several answers to this question.</p>
  307. <p>If you have only one set and one unset property to test,
  308. you can specify both an <code>if</code> and an <code>unless</code>
  309. attribute for the target, and they will act as if they
  310. are &quot;anded&quot; together.</p>
  311. <p>If you are using a version of Ant 1.3 or earlier, the
  312. way to work with all other cases is to chain targets together
  313. to determine the specific state you want to test for.</p>
  314. <p>To see how this works, assume you have three properties:
  315. <code>prop1</code>, <code>prop2</code>, and <code>prop3</code>.
  316. You want to test that <code>prop1</code> and <code>prop2</code>
  317. are set, and that <code>prop3</code> is not. If the condition
  318. holds true you want to echo &quot;yes&quot;.</p>
  319. <p>Here is the implementation in Ant 1.3 and earlier:</p>
  320. <source><![CDATA[
  321. <target name="cond" depends="cond-if"/>
  322. <target name="cond-if" if="prop1">
  323. <antcall target="cond-if-2"/>
  324. </target>
  325. <target name="cond-if-2" if="prop2">
  326. <antcall target="cond-if-3"/>
  327. </target>
  328. <target name="cond-if-3" unless="prop3">
  329. <echo message="yes"/>
  330. </target>
  331. ]]></source>
  332. <p>Note: <code>&lt;antcall&gt;</code> tasks do <em>not</em> pass
  333. property changes back up to the environment they were called
  334. from, so you would&apos;nt be able to, for example, set a
  335. <code>result</code> property in the <code>cond-if-3</code> target,
  336. then do
  337. <code>&lt;echo message=&quot;result is ${result}&quot;/&gt;</code>
  338. in the <code>cond</code> target.</p>
  339. <p>Starting with Ant 1.4, you can use the
  340. <code>&lt;condition&gt;</code> task.</p>
  341. <source><![CDATA[
  342. <target name="cond" depends="cond-if,cond-else"/>
  343. <target name="check-cond">
  344. <condition property="cond-is-true">
  345. <and>
  346. <not>
  347. <equals arg1="${prop1}" arg2="$${prop1}" />
  348. </not>
  349. <not>
  350. <equals arg1="${prop2}" arg2="$${prop2}" />
  351. </not>
  352. <equals arg1="${prop3}" arg2="$${prop3}" />
  353. </and>
  354. </condition>
  355. </target>
  356. <target name="cond-if" depends="check-cond" if="cond-is-true">
  357. <echo message="yes"/>
  358. </target>
  359. <target name="cond-else" depends="check-cond" unless="cond-is-true">
  360. <echo message="no"/>
  361. </target>
  362. ]]></source>
  363. <p>This version takes advantage of two things:</p>
  364. <ul>
  365. <li>If a property <code>a</code> has not been set,
  366. <code>${a}</code> will evaluate to <code>${a}</code>.</li>
  367. <li>To get a literal <code>$</code> in Ant, you have to
  368. escape it with another <code>$</code> - this will also break
  369. the special treatment of the <code>${</code> sequence.</li>
  370. </ul>
  371. <p>Because testing for a literal <code>${property}</code> string
  372. isn&apos;t all that readable or easy to understand,
  373. post-1.4.1 Ant introduces the <code>&lt;isset&gt;</code> element
  374. to the <code>&lt;condition&gt;</code> task.</p>
  375. <p>Here is the previous example done using
  376. <code>&lt;isset&gt;</code>:</p>
  377. <source><![CDATA[
  378. <target name="check-cond">
  379. <condition property="cond-is-true">
  380. <and>
  381. <isset property="prop1"/>
  382. <isset property="prop2"/>
  383. <not>
  384. <isset property="prop3"/>
  385. </not>
  386. </and>
  387. </condition>
  388. </target>
  389. ]]></source>
  390. <p>The last option is to use a scripting language to set the
  391. properties. This can be particularly handy when you need much
  392. finer control than the simple conditions shown here but, of
  393. course, comes with the overhead of adding JAR files to support
  394. the language, to say nothing of the added maintenance in requiring
  395. two languages to implement a single system. See the
  396. <a href="manual/OptionalTasks/script.html">
  397. <code>&lt;script&gt;</code> task documentation</a> for more
  398. details.</p>
  399. </answer>
  400. </faq>
  401. <faq id="encoding">
  402. <question>How can I include national characters like German
  403. umlauts in my build file?</question>
  404. <answer>
  405. <p>You need to tell the XML parser which character encoding
  406. your build file uses, this is done inside the <a
  407. href="http://www.w3.org/TR/2000/REC-xml-20001006#sec-prolog-dtd">XML
  408. declaration</a>.</p>
  409. <p>By default the parser assumes you are using the UTF-8
  410. encoding instead of your platform's default. For most Western
  411. European countries you should set the encoding to
  412. <code>ISO-8859-1</code>. To do so, make the very first line
  413. of you build file read like</p>
  414. <source><![CDATA[
  415. <?xml version="1.0" encoding="ISO-8859-1" ?>
  416. ]]></source>
  417. </answer>
  418. </faq>
  419. </faqsection>
  420. <faqsection title="It doesn&apos;t work (as expected)">
  421. <faq id="always-recompiles">
  422. <question>Why does Ant always recompile all my Java files?</question>
  423. <answer>
  424. <p>In order to find out which files should be compiled, Ant
  425. compares the timestamps of the source files to those of the
  426. resulting <code>.class</code> files. Opening all source files
  427. to find out which package they belong to would be very
  428. inefficient. Instead, Ant expects you to place your
  429. source files in a directory hierarchy that mirrors your
  430. package hierarchy and to point Ant to the root of this
  431. directory tree with the <code>srcdir</code> attribute.</p>
  432. <p>Say you have <code>&lt;javac srcdir=&quot;src&quot;
  433. destdir=&quot;dest&quot;/&gt;</code>. If Ant finds a file
  434. <code>src/a/b/C.java</code>, it expects it to be in package
  435. <code>a.b</code> so that the resulting <code>.class</code>
  436. file is going to be <code>dest/a/b/C.class</code>.</p>
  437. <p>If your source-tree directory structure does not match your
  438. package structure, Ant&apos;s heuristic won&apos;t work, and
  439. it will recompile classes that are up-to-date. Ant is not the
  440. only tool that expects a source-tree layout like this.</p>
  441. <p>If you have Java source files that aren&apos;t declared to
  442. be part of any package, you can still use the <code>&lt;javac&gt;</code>
  443. task to compile these files correctly - just set the
  444. <code>srcdir</code> and <code>destdir</code> attributes to
  445. the actual directory the source
  446. files live in and the directory the class files should go into,
  447. respectively.</p>
  448. </answer>
  449. </faq>
  450. <faq id="defaultexcludes">
  451. <question>I&apos;ve used a <code>&lt;delete&gt;</code> task to
  452. delete unwanted SourceSafe control files (CVS files, editor
  453. backup files, etc.), but it doesn&apos;t seem to work; the files
  454. never get deleted. What&apos;s wrong?</question>
  455. <answer>
  456. <p>This is probably happening because, by default, Ant excludes
  457. SourceSafe control files (<code>vssver.scc</code>) and certain other
  458. files from FileSets.</p>
  459. <p>Here&apos;s what you probably did:</p>
  460. <source><![CDATA[
  461. <delete>
  462. <fileset dir="${build.src}" includes="**/vssver.scc"/>
  463. </delete>
  464. ]]></source>
  465. <p>You need to switch off the default exclusions,
  466. and it will work:</p>
  467. <source><![CDATA[
  468. <delete>
  469. <fileset dir="${build.src}" includes="**/vssver.scc"
  470. defaultexcludes="no"/>
  471. </delete>
  472. ]]></source>
  473. <p>For a complete listing of the patterns that are excluded
  474. by default, see <a href="manual/dirtasks.html#defaultexcludes">the user
  475. manual</a>.</p>
  476. </answer>
  477. </faq>
  478. <faq id="stop-dependency">
  479. <question>I have a target I want to skip if a property is set,
  480. so I have <code>unless=&quot;property&quot;</code> as an attribute
  481. of the target, but all the targets this target
  482. depends on are still executed. Why?</question>
  483. <answer>
  484. <p>The list of dependencies is generated by Ant before any of the
  485. targets are run. This allows dependent targets, such as an
  486. <code>init</code> target, to set properties that can control the
  487. execution of the targets higher in the dependency graph. This
  488. is a good thing.</p>
  489. <p>However, when your dependencies break down the
  490. higher-level task
  491. into several smaller steps, this behaviour becomes
  492. counter-intuitive. There are a couple of solutions available:
  493. </p>
  494. <ol>
  495. <li>Put the same condition on each of the dependent targets.</li>
  496. <li>Execute the steps using <code>&lt;antcall&gt;</code>,
  497. instead of specifying them inside the <code>depends</code>
  498. attribute.</li>
  499. </ol>
  500. </answer>
  501. </faq>
  502. <faq id="include-order">
  503. <question>In my <code>&lt;fileset&gt;</code>, I&apos;ve put in an
  504. <code>&lt;exclude&gt;</code> of all files followed by an
  505. <code>&lt;include&gt;</code> of just the files I want, but it
  506. isn&apos;t giving me any files at all. What&apos;s wrong?
  507. </question>
  508. <answer>
  509. <p>The order of the <code>&lt;include&gt;</code> and
  510. <code>&lt;exclude&gt;</code> tags within a <code>&lt;fileset&gt;</code>
  511. is ignored when the FileSet is created. Instead, all of the
  512. <code>&lt;include&gt;</code> elements are processed together,
  513. followed by all of the <code>&lt;exclude&gt;</code>
  514. elements. This means that the <code>&lt;exclude&gt;</code>
  515. elements only apply to the file list produced by the
  516. <code>&lt;include&gt;</code> elements.</p>
  517. <p>To get the files you want, focus on just the
  518. <code>&lt;include&gt;</code> patterns that would be necessary
  519. to get them. If you find you need to trim the list that the
  520. <code>&lt;include&gt;</code> elements produce, then use
  521. <code>&lt;exclude&gt;</code> elements.</p>
  522. </answer>
  523. </faq>
  524. <faq id="properties-not-trimmed">
  525. <question><code>ant</code> failed to build my program via javac
  526. even when I put the needed jars in an external
  527. <code>build.properties</code> file and reference them by
  528. <code>pathelement</code> or <code>classpath refid</code>.</question>
  529. <answer>
  530. <p>When <code>ant</code> loads properties from an external
  531. file it dosn't touch the value of properties, trailing blanks
  532. will not be trimmed for example.</p>
  533. <p>If the value represents a file path, like a jar needed to
  534. compile, the task which requires the value, javac for example
  535. would fail to compile since it can't find the file due to
  536. trailing spaces.</p>
  537. </answer>
  538. </faq>
  539. <faq id="winzip-lies">
  540. <question>Ant creates WAR files with a lower-case
  541. <code>web-inf</code> or JAR files with a lower-case
  542. <code>meta-inf</code> directory.</question>
  543. <answer>
  544. <p>No it doesn't.</p>
  545. <p>You may have seen these lower-case directory names in
  546. WinZIP, but WinZIP is trying to be helpful (and fails). If
  547. WinZIP encounters a filename that is all upper-case, it
  548. assumes it has come from an old DOS box andchanges the case to
  549. all lower-case for you.</p>
  550. <p>If you extract (or just check) the archive with jar, you
  551. will see that the names have the correct case.</p>
  552. </answer>
  553. </faq>
  554. </faqsection>
  555. <faqsection title="Ant and IDEs/Editors">
  556. <faq id="integration">
  557. <question>Is Ant supported by my IDE/Editor?</question>
  558. <answer>
  559. <p>See the <a href="external.html#IDE and Editor Integration">section
  560. on IDE integration</a> on our External Tools and Tasks page.</p>
  561. </answer>
  562. </faq>
  563. <faq id="emacs-mode">
  564. <question>Why doesn&apos;t (X)Emacs/vi/MacOS X&apos;s project builder
  565. correctly parse the error messages generated by Ant?</question>
  566. <answer>
  567. <p>Ant adds a &quot;banner&quot; with the name of the current
  568. task in front of all logging messages - and there are no built-in
  569. regular expressions in your editor that would account for
  570. this.</p>
  571. <p>You can disable this banner by invoking Ant with the
  572. <code>-emacs</code> switch. To make Ant autodetect
  573. Emacs&apos; compile mode, put this into your
  574. <code>.antrc</code> (contributed by Ville Skytt&#228;).</p>
  575. <source><![CDATA[
  576. # Detect (X)Emacs compile mode
  577. if [ "$EMACS" = "t" ] ; then
  578. ANT_ARGS="$ANT_ARGS -emacs"
  579. ANT_OPTS="$ANT_OPTS -Dbuild.compiler.emacs=true"
  580. fi
  581. ]]></source>
  582. <p>Alternatively, you can add the following snippet to your
  583. <code>.emacs</code> to make Emacs understand Ant&apos;s
  584. output.</p>
  585. <source><![CDATA[
  586. (require 'compile)
  587. (setq compilation-error-regexp-alist
  588. (append (list
  589. ;; works for jikes
  590. '("^\\s-*\\[[^]]*\\]\\s-*\\(.+\\):\\([0-9]+\\):\\([0-9]+\\):[0-9]+:[0-9]+:" 1 2 3)
  591. ;; works for javac
  592. '("^\\s-*\\[[^]]*\\]\\s-*\\(.+\\):\\([0-9]+\\):" 1 2))
  593. compilation-error-regexp-alist))
  594. ]]></source>
  595. <p>Yet another alternative that preserves most of Ant&apos;s
  596. formatting is to pipe Ant&apos;s output through the following Perl
  597. script by Dirk-Willem van Gulik:</p>
  598. <source><![CDATA[
  599. #!/usr/bin/perl
  600. #
  601. # May 2001 dirkx@apache.org - remove any
  602. # [foo] lines from the output; keeping
  603. # spacing more or less there.
  604. #
  605. $|=1;
  606. while(<STDIN>) {
  607. if (s/^(\s+)\[(\w+)\]//) {
  608. if ($2 ne $last) {
  609. print "$1\[$2\]";
  610. $s = ' ' x length($2);
  611. } else {
  612. print "$1 $s ";
  613. };
  614. $last = $2;
  615. };
  616. print;
  617. };
  618. ]]></source>
  619. </answer>
  620. </faq>
  621. </faqsection>
  622. <faqsection title="Advanced Issues">
  623. <faq id="dtd">
  624. <question>Is there a DTD that I can use to validate my build
  625. files?</question>
  626. <answer>
  627. <p>An incomplete DTD can be created by the
  628. <code>&lt;antstructure&gt;</code> task - but this one
  629. has a few problems:</p>
  630. <ul>
  631. <li>It doesn&apos;t know about required attributes. Only
  632. manual tweaking of this file can help here.</li>
  633. <li>It is not complete - if you add new tasks via
  634. <code>&lt;taskdef&gt;</code> it won&apos;t know about it. See
  635. <a href="http://www.sdv.fr/pages/casa/html/ant-dtd.en.html">this
  636. page</a> by Michel Casabianca for a solution to this
  637. problem. Note that the DTD you can download at this page
  638. is based on Ant 0.3.1.</li>
  639. <li>It may even be an invalid DTD. As Ant allows tasks
  640. writers to define arbitrary elements, name collisions will
  641. happen quite frequently - if your version of Ant contains
  642. the optional <code>&lt;test&gt;</code> and
  643. <code>&lt;junit&gt;</code> tasks, there are two XML
  644. elements named <code>test</code> (the task and the nested child
  645. element of <code>&lt;junit&gt;</code>) with different attribute
  646. lists. This problem cannot be solved; DTDs don&apos;t give a
  647. syntax rich enough to support this.</li>
  648. </ul>
  649. </answer>
  650. </faq>
  651. <faq id="xml-entity-include">
  652. <question>How do I include an XML snippet in my build file?</question>
  653. <answer>
  654. <p>You can use XML&apos;s way of including external files and let
  655. the parser do the job for Ant:</p>
  656. <source><![CDATA[
  657. <?xml version="1.0"?>
  658. <!DOCTYPE project [
  659. <!ENTITY common SYSTEM "file:./common.xml">
  660. ]>
  661. <project name="test" default="test" basedir=".">
  662. <target name="setup">
  663. ...
  664. </target>
  665. &common;
  666. ...
  667. </project>
  668. ]]></source>
  669. <p>will literally include the contents of <code>common.xml</code> where
  670. you&apos;ve placed the <code>&amp;common;</code> entity.</p>
  671. <p>In combination with a DTD, this would look like this:</p>
  672. <source><![CDATA[
  673. <!DOCTYPE project PUBLIC "-//ANT//DTD project//EN" "file:./ant.dtd" [
  674. <!ENTITY include SYSTEM "file:./header.xml">
  675. ]>
  676. ]]></source>
  677. </answer>
  678. </faq>
  679. <faq id="mail-logger">
  680. <question>How do I send an email with the result of my build
  681. process?</question>
  682. <answer>
  683. <p>If you are using a nightly build of Ant 1.5 after
  684. 2001-12-14, you can use the built-in MailLogger:</p>
  685. <source><![CDATA[
  686. ant -logger org.apache.tools.ant.listener.MailLogger
  687. ]]></source>
  688. <p>See the <a href="http://cvs.apache.org/viewcvs/~checkout~/ant/docs/manual/listeners.html?content-type=text/html">Listeners
  689. &amp; Loggers</a> documentation for details on the properties
  690. required.</p>
  691. <p>For older versions of Ant, you can use a custom
  692. BuildListener that sends out an email
  693. in the buildFinished() method. Will Glozer
  694. &lt;will.glozer@jda.com&gt; has written such a listener based
  695. on <a href="http://java.sun.com/products/javamail/">JavaMail</a>.
  696. The source is:</p>
  697. <source><![CDATA[
  698. import java.io.*;
  699. import java.util.*;
  700. import javax.mail.*;
  701. import javax.mail.internet.*;
  702. import org.apache.tools.ant.*;
  703. /**
  704. * A simple listener that waits for a build to finish and sends an email
  705. * of the results. The settings are stored in "monitor.properties" and
  706. * are fairly self explanatory.
  707. *
  708. * @author Will Glozer
  709. * @version 1.05a 09/06/2000
  710. */
  711. public class BuildMonitor implements BuildListener {
  712. protected Properties props;
  713. /**
  714. * Create a new BuildMonitor.
  715. */
  716. public BuildMonitor() throws Exception {
  717. props = new Properties();
  718. InputStream is = getClass().getResourceAsStream("monitor.properties");
  719. props.load(is);
  720. is.close();
  721. }
  722. public void buildStarted(BuildEvent e) {
  723. }
  724. /**
  725. * Determine the status of the build and the actions to follow, now that
  726. * the build has completed.
  727. *
  728. * @param e Event describing the build status.
  729. */
  730. public void buildFinished(BuildEvent e) {
  731. Throwable th = e.getException();
  732. String status = (th != null) ? "failed" : "succeeded";
  733. try {
  734. String key = "build." + status;
  735. if (props.getProperty(key + ".notify").equalsIgnoreCase("false")) {
  736. return;
  737. }
  738. Session session = Session.getDefaultInstance(props, null);
  739. MimeMessage message = new MimeMessage(session);
  740. message.addRecipients(Message.RecipientType.TO, parseAddresses(
  741. props.getProperty(key + ".email.to")));
  742. message.setSubject(props.getProperty(key + ".email.subject"));
  743. BufferedReader br = new BufferedReader(new FileReader(
  744. props.getProperty("build.log")));
  745. StringWriter sw = new StringWriter();
  746. String line = br.readLine();
  747. while (line != null) {
  748. sw.write(line);
  749. sw.write("\n");
  750. line = br.readLine();
  751. }
  752. br.close();
  753. message.setText(sw.toString(), "UTF-8");
  754. sw.close();
  755. Transport transport = session.getTransport();
  756. transport.connect();
  757. transport.send(message);
  758. transport.close();
  759. } catch (Exception ex) {
  760. System.out.println("BuildMonitor failed to send email!");
  761. ex.printStackTrace();
  762. }
  763. }
  764. /**
  765. * Parse a comma separated list of internet email addresses.
  766. *
  767. * @param s The list of addresses.
  768. * @return Array of Addresses.
  769. */
  770. protected Address[] parseAddresses(String s) throws Exception {
  771. StringTokenizer st = new StringTokenizer(s, ",");
  772. Address[] addrs = new Address[st.countTokens()];
  773. for (int i = 0; i < addrs.length; i++) {
  774. addrs[i] = new InternetAddress(st.nextToken());
  775. }
  776. return addrs;
  777. }
  778. public void messageLogged(BuildEvent e) {
  779. }
  780. public void targetStarted(BuildEvent e) {
  781. }
  782. public void targetFinished(BuildEvent e) {
  783. }
  784. public void taskStarted(BuildEvent e) {
  785. }
  786. public void taskFinished(BuildEvent e) {
  787. }
  788. }
  789. ]]></source>
  790. <p>With a <code>monitor.properties</code> like this:</p>
  791. <source><![CDATA[
  792. # configuration for build monitor
  793. mail.transport.protocol=smtp
  794. mail.smtp.host=<host>
  795. mail.from=Will Glozer <will.glozer@jda.com>
  796. build.log=build.log
  797. build.failed.notify=true
  798. build.failed.email.to=will.glozer@jda.com
  799. build.failed.email.subject=Nightly build failed!
  800. build.succeeded.notify=true
  801. build.succeeded.email.to=will.glozer@jda.com
  802. build.succeeded.email.subject=Nightly build succeeded!
  803. ]]></source>
  804. <p><code>monitor.properties</code> should be placed right next
  805. to your compiled <code>BuildMonitor.class</code>. To use it,
  806. invoke Ant like:</p>
  807. <source><![CDATA[
  808. ant -listener BuildMonitor -logfile build.log
  809. ]]></source>
  810. <p>Make sure that <code>mail.jar</code> from JavaMail and
  811. <code>activation.jar</code> from the
  812. <a href="http://java.sun.com/products/javabeans/glasgow/jaf.html">Java
  813. Beans Activation Framework</a> are in your <code>CLASSPATH</code>.</p>
  814. </answer>
  815. </faq>
  816. <faq id="listener-properties">
  817. <question>How do I get at the properties that Ant was running
  818. with from inside BuildListener?</question>
  819. <answer>
  820. <p>You can get at a hashtable with all the properties that Ant
  821. has been using through the BuildEvent parameter. For
  822. example:</p>
  823. <source><![CDATA[
  824. public void buildFinished(BuildEvent e) {
  825. Hashtable table = e.getProject().getProperties();
  826. String buildpath = (String)table.get("build.path");
  827. ...
  828. }
  829. ]]></source>
  830. <p>This is more accurate than just reading the same property
  831. files that your project does, since it will give the correct
  832. results for properties that were specified on the Ant command line.</p>
  833. </answer>
  834. </faq>
  835. </faqsection>
  836. <faqsection title="Known Problems">
  837. <faq id="remove-cr">
  838. <question>&lt;chmod&gt; or &lt;exec&gt; doesn&apos;t work in Ant
  839. 1.3 on Unix</question>
  840. <answer>
  841. <p>The <code>antRun</code> script in <code>ANT_HOME/bin</code>
  842. has DOS instead of Unix line endings; you must remove the
  843. carriage-return characters from this file. This can be done by
  844. using Ant&apos;s <code>&lt;fixcrlf&gt;</code> task
  845. or something like:</p>
  846. <source><![CDATA[
  847. tr -d '\r' < $ANT_HOME/bin/antRun > /tmp/foo
  848. mv /tmp/foo $ANT_HOME/bin/antRun
  849. ]]></source>
  850. </answer>
  851. </faq>
  852. <faq id="javadoc-cannot-execute">
  853. <question>JavaDoc failed: java.io.IOException: javadoc: cannot execute</question>
  854. <answer>
  855. <p>There is a bug in the Solaris reference implementation of
  856. the JDK (see <a href="http://developer.java.sun.com/developer/bugParade/bugs/4230399.html">http://developer.java.sun.com/developer/bugParade/bugs/4230399.html</a>).
  857. This also appears to be true under Linux. Moving the JDK to
  858. the front of the PATH fixes the problem.</p>
  859. </answer>
  860. </faq>
  861. <faq id="delegating-classloader">
  862. <question>&lt;style&gt; or &lt;junit&gt; ignores my
  863. &lt;classpath&gt;</question>
  864. <answer>
  865. <p>These tasks don&apos;t ignore your classpath setting, you
  866. are facing a common problem with delegating classloaders.</p>
  867. <p>First of all let&apos;s state that Ant adds all
  868. <code>.jar</code> files from <code>ANT_HOME/lib</code> to
  869. <code>CLASSPATH</code>, therefore &quot;in
  870. <code>CLASSPATH</code>&quot; shall mean &quot;either in your
  871. <code>CLASSPATH</code> environment variable or
  872. <code>ANT_HOME/lib</code>&quot; for the rest of this
  873. answer.</p>
  874. <p>This question collects a common type of problem: A task
  875. needs an external library and it has a nested classpath
  876. element so that you can point it to this external library, but
  877. that doesn&apos;t work unless you put the external library into the
  878. <code>CLASSPATH</code>.</p>
  879. <p>The root of the problem is that the class that needs the
  880. external library is on the <code>CLASSPATH</code>.</p>
  881. <p>When you specify a nested <code>&lt;classpath&gt;</code> in
  882. Ant, Ant creates a new class loader that uses the path you
  883. have specified. It then tries to load additional classes from
  884. this classloader.</p>
  885. <p>In most cases - for example the two cases above - Ant
  886. doesn&apos;t load the external library directly, it is the loaded
  887. class that does so.</p>
  888. <p>In the case of <code>&lt;junit&gt;</code> it is the task
  889. implementation itself and in the case of
  890. <code>&lt;style&gt;</code> it is the implementation of the
  891. <code>org.apache.tools.ant.taskdefs.XSLTLiaison</code>
  892. class.</p>
  893. <p>Ant&apos;s class loader implementation uses Java&apos;s
  894. delegation model, see <a
  895. href="http://java.sun.com/products/jdk/1.2/docs/api/java/lang/ClassLoader.html">http://java.sun.com/products/jdk/1.2/docs/api/java/lang/ClassLoader.html</a>
  896. the paragraph</p>
  897. <blockquote>The <code>ClassLoader</code> class uses a
  898. delegation model to search for classes and resources. Each
  899. instance of <code>ClassLoader</code> has an associated parent
  900. class loader. When called upon to find a class or resource, a
  901. <code>ClassLoader</code> instance will delegate the search for
  902. the class or resource to its parent class loader before
  903. attempting to find the class or resource itself. The virtual
  904. machine&apos;s built-in class loader, called the bootstrap
  905. class loader, does not itself have a parent but may serve as
  906. the parent of a <code>ClassLoader</code>
  907. instance.</blockquote>
  908. <p>This means, Ant&apos;s class loader will consult the
  909. bootstrap class loader first, which tries to load classes from
  910. <code>CLASSPATH</code>. The bootstrap class loader
  911. doesn&apos;t know anything about Ant&apos;s class loader or
  912. even the path you have specified.</p>
  913. <p>If the bootstrap class loader can load the class Ant has
  914. asked it to load, this class will try to load the external
  915. library from <code>CLASSPATH</code> as well - it doesn&apos;t
  916. know anything else - and will not find it unless the library
  917. is in <code>CLASSPATH</code> as well.</p>
  918. <p>To solve this, you have two major options:</p>
  919. <ol>
  920. <li>put all external libraries you need in
  921. <code>CLASSPATH</code> as well this is not what you want,
  922. otherwise you wouldn&apos;t have found this FAQ entry.</li>
  923. <li>remove the class that loads the external library from
  924. the <code>CLASSPATH</code>.</li>
  925. </ol>
  926. <p>The easiest way to do this is to remove
  927. <code>optional.jar</code> from <code>ANT_HOME/lib</code>. If
  928. you do so, you will have to <code>&lt;taskdef&gt;</code> all
  929. optional tasks and use nested <code>&lt;classpath&gt;</code>
  930. elements in the <code>&lt;taskdef&gt;</code> tasks that point
  931. to the new location of <code>optional.jar</code>. Also,
  932. don&apos;t forget to add the new location of
  933. <code>optional.jar</code> to the
  934. <code>&lt;classpath&gt;</code> of your
  935. <code>&lt;style&gt;</code> or <code>&lt;junit&gt;</code>
  936. task.</p>
  937. <p>If you want to avoid to <code>&lt;taskdef&gt;</code> all
  938. optional tasks you need, the only other option is to remove
  939. the classes that should not be loaded via the bootstrap class
  940. loader from <code>optional.jar</code> and put them into a
  941. separate archive. Add this separate archive to the
  942. <code>&lt;classpath&gt;</code> of your
  943. <code>&lt;style&gt;</code> or <code>&lt;junit&gt;</code> task
  944. - and make sure the separate archive is not in
  945. <code>CLASSPATH</code>.</p>
  946. <p>In the case of <code>&lt;junit&gt;</code> you&apos;d have
  947. to remove all classes that are in the
  948. <code>org/apache/tools/ant/taskdefs/optional/junit</code>
  949. directory, in the <code>&lt;style&gt;</code> case it is one of
  950. the <code>*Liaison</code> classes in
  951. <code>org/apache/tools/ant/taskdefs/optional</code>.</p>
  952. <p>If you use the option to break up <code>optional.jar</code>
  953. for <code>&lt;junit&gt;</code>, you still have to use a
  954. <code>&lt;taskdef&gt;</code> with a nested
  955. <code>&lt;classpath&gt;</code> to define the junit task.</p>
  956. </answer>
  957. </faq>
  958. <faq id="winxp-jdk14-ant14">
  959. <question>When running Ant 1.4 on Windows XP and JDK 1.4, I get
  960. various errors when trying to <code>&lt;exec&gt;</code>, fork
  961. <code>&lt;java&gt;</code> or access environment
  962. variables.</question>
  963. <answer>
  964. <p>Ant &lt; 1.5 doesn&apos;t recognize Windows XP as a flavor
  965. of Windows that runs <code>CMD.EXE</code> instead of
  966. <code>COMMAND.COM</code>. JDK 1.3 will tell Ant that Windows
  967. XP is Windows 2000 so the problem doesn&apos;t show up
  968. there.</p>
  969. <p>Apart from upgrading to Ant 1.5 or better, setting the
  970. environment variable <code>ANT_OPTS</code> to
  971. <code>-Dos.name=Windows_NT</code> prior to invoking Ant has
  972. been confirmed as a workaround.</p>
  973. </answer>
  974. </faq>
  975. <faq id="1.5-cygwin-sh">
  976. <question>The <code>ant</code> wrapper script of Ant 1.5 fails
  977. for Cygwin if <code>ANT_HOME</code> is set to a Windows style
  978. path.</question>
  979. <answer>
  980. <p>This problem has been reported only hours after Ant 1.5 has
  981. been released, see <a
  982. href="http://issues.apache.org/bugzilla/show_bug.cgi?id=10664">Bug
  983. 10664</a> and all it's duplicates.</p>
  984. <p>A fixed version of the wrapper script can be found <a
  985. href="http://ant.apache.org/old-releases/v1.5/errata/">here</a>.
  986. Simply replace your script with this version.</p>
  987. </answer>
  988. </faq>
  989. </faqsection>
  990. </document>