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 44 kB

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