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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779
  1. <?xml version="1.0"?>
  2. <!--
  3. Copyright 2001-2006 The Apache Software Foundation
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. -->
  14. <document>
  15. <properties>
  16. <author email="bodewig@apache.org">Stefan Bodewig</author>
  17. <title>Frequently Asked Questions</title>
  18. </properties>
  19. <faqsection title="About this FAQ">
  20. <faq id="latest-version">
  21. <question>Where do I find the latest version of this
  22. document?</question>
  23. <answer>
  24. <p>The latest version can always be found at Ant&apos;s homepage
  25. <a href="http://ant.apache.org/faq.html">http://ant.apache.org/faq.html</a>.</p>
  26. </answer>
  27. </faq>
  28. <faq id="adding-faqs">
  29. <question>How can I contribute to this FAQ?</question>
  30. <answer>
  31. <p>The page you are looking it is generated from
  32. <a href="http://cvs.apache.org/viewcvs.cgi/~checkout~/ant/xdocs/faq.xml">this</a>
  33. document. If you want to add a new question, please submit
  34. a patch against this document to one of Ant&apos;s mailing lists;
  35. hopefully, the structure is self-explanatory.</p>
  36. <p>If you don&apos;t know how to create a patch, see the patches
  37. section of <a href="http://jakarta.apache.org/site/source.html">this
  38. page</a>.</p>
  39. </answer>
  40. </faq>
  41. <faq id="creating-faq">
  42. <question>How do you create the HTML version of this
  43. FAQ?</question>
  44. <answer>
  45. <p>We use
  46. <a href="http://jakarta.apache.org/velocity/anakia.html">Anakia</a>
  47. to render the HTML version from the original XML file.</p>
  48. <p>The Velocity stylesheets used to process the XML files can
  49. be found in the <code>xdocs/stylesheets</code> subdirectory of
  50. Ant&apos;s CVS repository - the build file
  51. <code>docs.xml</code> at the top level of the ant CVS
  52. module is used to drive Anakia.</p>
  53. <p>This file assumes that you have the
  54. <code>jakarta-site2</code> CVS module checked out as well, but
  55. if you follow the instruction from Anakia&apos;s homepage, you
  56. should get it to work without that. Just make sure all
  57. required jars are in the task&apos;s classpath.</p>
  58. </answer>
  59. </faq>
  60. </faqsection>
  61. <faqsection title="General">
  62. <faq id="what-is-ant">
  63. <question>What is Apache Ant?</question>
  64. <answer>
  65. <p> Ant is a Java-based build tool. In theory, it is kind of
  66. like Make, without Make&apos;s wrinkles and with the full
  67. portability of pure Java code.</p>
  68. </answer>
  69. </faq>
  70. <faq id="ant-name">
  71. <question>Why do you call it Ant?</question>
  72. <answer>
  73. <p>According to Ant&apos;s original author, James Duncan
  74. Davidson, the name is an acronym for &quot;Another Neat
  75. Tool&quot;.</p>
  76. <p>Later explanations go along the lines of &quot;ants
  77. do an extremely good job at building things&quot;, or
  78. &quot;ants are very small and can carry a weight dozens of times
  79. their own&quot; - describing what Ant is intended to
  80. be.</p>
  81. </answer>
  82. </faq>
  83. <faq id="history">
  84. <question>Tell us a little bit about Ant&apos;s history.</question>
  85. <answer>
  86. <p>Initially, Ant was part of the Tomcat code base, when it was
  87. donated to the Apache Software Foundation. It was
  88. created by James Duncan Davidson, who is also the original
  89. author of Tomcat. Ant was there to build Tomcat, nothing
  90. else.</p>
  91. <p>Soon thereafter, several open source Java projects realized
  92. that Ant could solve the problems they had with Makefiles.
  93. Starting with the projects hosted at Jakarta and the old Java
  94. Apache project, Ant spread like a virus and is now the build
  95. tool of choice for a lot of projects.</p>
  96. <p>In January 2000, Ant was moved to a separate CVS module and
  97. was promoted to a project of its own, independent of
  98. Tomcat, and became Apache Ant.</p>
  99. <p>The first version of Ant that was exposed to a larger audience
  100. was the one that shipped with Tomcat&apos;s 3.1 release on 19 April
  101. 2000. This version has later been referred to as Ant
  102. 0.3.1.</p>
  103. <p>The first official release of Ant as a stand-alone product was
  104. Ant 1.1, released on 19 July 2000. The complete release
  105. history:</p>
  106. <table>
  107. <tr>
  108. <th>Ant Version</th>
  109. <th>Release Date</th>
  110. </tr>
  111. <tr>
  112. <td>1.1</td>
  113. <td>19 July 2000</td>
  114. </tr>
  115. <tr>
  116. <td>1.2</td>
  117. <td>24 October 2000</td>
  118. </tr>
  119. <tr>
  120. <td>1.3</td>
  121. <td>3 March 2001</td>
  122. </tr>
  123. <tr>
  124. <td>1.4</td>
  125. <td>3 September 2001</td>
  126. </tr>
  127. <tr>
  128. <td>1.4.1</td>
  129. <td>11 October 2001</td>
  130. </tr>
  131. <tr>
  132. <td>1.5</td>
  133. <td>10 July 2002</td>
  134. </tr>
  135. <tr>
  136. <td>1.5.1</td>
  137. <td>3 October 2002</td>
  138. </tr>
  139. <tr>
  140. <td>1.5.2</td>
  141. <td>3 March 2003</td>
  142. </tr>
  143. <tr>
  144. <td>1.5.3</td>
  145. <td>9 April 2003</td>
  146. </tr>
  147. <tr>
  148. <td>1.5.4</td>
  149. <td>12 August 2003</td>
  150. </tr>
  151. <tr>
  152. <td>1.6.0</td>
  153. <td>18 December 2003</td>
  154. </tr>
  155. <tr>
  156. <td>1.6.1</td>
  157. <td>12 February 2004</td>
  158. </tr>
  159. <tr>
  160. <td>1.6.2</td>
  161. <td>16 July 2004</td>
  162. </tr>
  163. <tr>
  164. <td>1.6.3</td>
  165. <td>28 April 2005</td>
  166. </tr>
  167. <tr>
  168. <td>1.6.4</td>
  169. <td>19 May 2005</td>
  170. </tr>
  171. <tr>
  172. <td>1.6.5</td>
  173. <td>2 June 2005</td>
  174. </tr>
  175. </table>
  176. </answer>
  177. </faq>
  178. </faqsection>
  179. <faqsection title="Installation">
  180. <faq id="no-gnu-tar">
  181. <question>I get checksum errors when I try to extract the
  182. <code>tar.gz</code> distribution file. Why?</question>
  183. <answer>
  184. <p>Ant&apos;s distribution contains file names that are longer
  185. than 100 characters, which is not supported by the standard
  186. tar file format. Several different implementations of tar use
  187. different and incompatible ways to work around this
  188. restriction.</p>
  189. <p>Ant&apos;s &lt;tar&gt; task can create tar archives that use
  190. the GNU tar extension, and this has been used when putting
  191. together the distribution. If you are using a different
  192. version of tar (for example, the one shipping with Solaris),
  193. you cannot use it to extract the archive.</p>
  194. <p>The solution is to either install GNU tar, which can be
  195. found <a href="http://www.gnu.org/software/tar/tar.html">here</a>,
  196. or use the zip archive instead (you can extract it using
  197. <code>jar xf</code>).</p>
  198. </answer>
  199. </faq>
  200. <faq id="RedHat_ES_3">
  201. <question>How do you get ant-1.6.x (or any version later than
  202. 1.5.2) to work on on RedHat ES 3?</question>
  203. <answer>
  204. <p>Redhat ES 3.0 comes installed with ant 1.5.2. Even if you
  205. have your PATH and ANT_HOME variables set correctly to a later
  206. version of ant, you will always be forced to use the
  207. preinstalled version.</p>
  208. <p>To use a later version of ant on this OS you could do the
  209. following:</p>
  210. <source><![CDATA[
  211. $ ant -version
  212. Apache Ant version 1.5.2-23 compiled on November 12 2003
  213. $ su -
  214. # rpm -e ant ant-libs
  215. # exit
  216. $ hash -r
  217. $ ant -version
  218. Apache Ant version 1.6.2 compiled on July 16 2004
  219. ]]></source></answer>
  220. </faq>
  221. </faqsection>
  222. <faqsection title="How do I ...">
  223. <faq id="implement-os-specific-configuration">
  224. <question>How do I realize os--specific configurations?</question>
  225. <answer>
  226. <p>The core idea is using property files which name accords to the
  227. os-name. Then simply use the build-in property <tt>os.name</tt>.</p>
  228. <p>For better use you should also provide a file with defaul values.
  229. But be careful with the correct os-names. For test simply &lt;echo&gt;
  230. the ${os.name} on all machines and you can be sure to use the right
  231. file names.</p>
  232. <source><![CDATA[
  233. <property file="${os.name}.properties"/>
  234. <property file="default.properties"/>
  235. ]]></source>
  236. </answer>
  237. </faq>
  238. <faq id="adding-external-tasks">
  239. <question>How do I add an external task that I&apos;ve written to the
  240. page &quot;External Tools and Tasks&quot;?</question>
  241. <answer>
  242. <p>Join and post a message to the dev or user mailing
  243. list (one list is enough), including the following
  244. information:</p>
  245. <ul>
  246. <li>the name of the task/tool</li>
  247. <li>a short description of the task/tool</li>
  248. <li>a Compatibility: entry stating with which version(s) of
  249. Ant the tool/task is compatible to</li>
  250. <li>a URL: entry linking to the main page of the tool/task</li>
  251. <li>a Contact: entry containing the email address or the URL
  252. of a webpage for the person or list to contact for issues
  253. related to the tool/task. <strong>Note that we&apos;ll add a
  254. link on the page, so any email address added there is not
  255. obfuscated and can (and probably will) be abused by robots
  256. harvesting websites for addresses to spam.</strong></li>
  257. <li>a License: entry containing the type of license for the
  258. tool/task</li>
  259. </ul>
  260. <p>The preferred format for this information is a patch to <a
  261. href="http://cvs.apache.org/viewcvs.cgi/~checkout~/ant/xdocs/external.xml">this</a>
  262. document.</p>
  263. <p>If you have written something bigger than a 'simple plugin' to Ant it
  264. may be better to add the link to <a href="projects.html">projects.html</a>.
  265. The procedure to add it is the same. The file to patch is <a
  266. href="http://cvs.apache.org/viewcvs.cgi/~checkout~/ant/xdocs/projects.xml">this</a>
  267. document. The syntax of that file is the same.</p>
  268. </answer>
  269. </faq>
  270. <faq id="create-extensions">
  271. <question>How do I create new tasks?</question>
  272. <answer>
  273. <p>Apart from a lot of information on using Ant, the
  274. <a href="manual/index.html">Manual</a> also contains information
  275. on how to extend Ant with new tasks. This information
  276. can be found under &quot;Developing with Ant&quot;.</p>
  277. <p>Chances are that someone else already created the task you
  278. want to create, it may be wise to see
  279. <a href="external.html">External Tools and Tasks</a> and
  280. <a href="projects.html">Related Projects</a> first.</p>
  281. </answer>
  282. </faq>
  283. <faq id="passing-cli-args">
  284. <question>How do I pass parameters from the command line to my
  285. build file?</question>
  286. <answer>
  287. <p>Use properties. Using <code>ant
  288. -D<em>name</em>=<em>value</em></code> lets you define values for
  289. properties on the Ant command line. These properties can then be
  290. used within your build file as
  291. any normal property: <code>${<em>name</em>}</code> will put in
  292. <code><em>value</em></code>.</p>
  293. </answer>
  294. </faq>
  295. <faq id="jikes-switches">
  296. <question>How can I use Jikes-specific command-line
  297. switches?</question>
  298. <answer>
  299. <p>A couple of switches are supported via &quot;magic&quot;
  300. properties:</p>
  301. <table>
  302. <tr>
  303. <th>switch</th>
  304. <th>property</th>
  305. <th>default</th>
  306. </tr>
  307. <tr>
  308. <td>+E</td>
  309. <td>build.compiler.emacs</td>
  310. <td>false == not set</td>
  311. </tr>
  312. <tr>
  313. <td>+P</td>
  314. <td>build.compiler.pedantic</td>
  315. <td>false == not set</td>
  316. </tr>
  317. <tr>
  318. <td>+F</td>
  319. <td>build.compiler.fulldepend</td>
  320. <td>false == not set</td>
  321. </tr>
  322. <tr>
  323. <td><strong>(Only for Ant &lt; 1.4; replaced by the
  324. <code><strong>nowarn</strong></code>
  325. attribute of the <code><strong>&lt;javac&gt;</strong></code>
  326. task after that.)</strong><br></br>-nowarn</td>
  327. <td>build.compiler.warnings</td>
  328. <td>true == not set</td>
  329. </tr>
  330. </table>
  331. <p>With Ant &gt;= 1.5, you can also use nested
  332. <code>&lt;compilerarg&gt;</code> elements with the
  333. <code>&lt;javac&gt;</code> task.</p>
  334. </answer>
  335. </faq>
  336. <faq id="shell-redirect-1">
  337. <question>How do I include a &lt; character in my command-line arguments?</question>
  338. <answer>
  339. <p>The short answer is "Use: <code>&amp;lt;</code>".</p>
  340. <p>The long answer is that this probably won&apos;t do what you
  341. want anyway (see <a href="#shell-redirect-2">the next
  342. section</a>).</p>
  343. </answer>
  344. </faq>
  345. <faq id="shell-redirect-2">
  346. <question>How do I redirect standard input or standard output
  347. in the <code>&lt;exec&gt;</code> task?</question>
  348. <answer>
  349. <p>Say you want to redirect the standard output stream of the
  350. <code>m4</code> command to write to a file, something
  351. like:</p>
  352. <source><![CDATA[
  353. shell-prompt> m4 foo.m4 > foo
  354. ]]></source>
  355. <p>and try to translate it into</p>
  356. <source><![CDATA[
  357. <exec executable="m4">
  358. <arg value="foo.m4"/>
  359. <arg value="&gt;"/>
  360. <arg value="foo"/>
  361. </exec>
  362. ]]></source>
  363. <p>This will not do what you expect. The output redirection is
  364. performed by your shell, not the command itself, so this
  365. should read:</p>
  366. <source><![CDATA[
  367. <exec executable="/bin/sh">
  368. <arg value="-c" />
  369. <arg value="m4 foo.m4 &gt; foo" />
  370. </exec>
  371. ]]></source>
  372. <p>Note that you must use the <code>value</code> attribute of
  373. <code>&lt;arg&gt;</code> in the last element, in order to have
  374. the command passed as a single, quoted argument. Alternatively,
  375. you can use:</p>
  376. <source><![CDATA[
  377. <exec executable="/bin/sh">
  378. <arg line='-c "m4 foo.m4 &gt; foo"'/>
  379. </exec>
  380. ]]></source>
  381. <p>Note the double-quotes nested inside the single-quotes.</p>
  382. </answer>
  383. </faq>
  384. <faq id="batch-shell-execute">
  385. <question>How do I execute a batch file or shell script from Ant?</question>
  386. <answer>
  387. <p>On native Unix systems, you should be able to run shell scripts
  388. directly. On systems running a Unix-type shell (for example, Cygwin
  389. on Windows) execute the (command) shell instead - <code>cmd</code>
  390. for batch files, <code>sh</code> for shell scripts - then pass the
  391. batch file or shell script (plus any arguments to the script)
  392. as a single command, using the <code>/c</code> or
  393. <code>-c</code> switch, respectively. See
  394. <a href="#shell-redirect-2">the above section</a>
  395. for example <code>&lt;exec&gt;</code> tasks
  396. executing <code>sh</code>. For batch files, use something like:</p>
  397. <source><![CDATA[
  398. <exec dir="." executable="cmd" os="Windows NT">
  399. <arg line="/c test.bat"/>
  400. </exec>
  401. ]]></source>
  402. </answer>
  403. </faq>
  404. <faq id="multi-conditions">
  405. <question>I want to execute a particular target only if
  406. multiple conditions are true.</question>
  407. <answer>
  408. <p>There are actually several answers to this question.</p>
  409. <p>If you have only one set and one unset property to test,
  410. you can specify both an <code>if</code> and an <code>unless</code>
  411. attribute for the target, and they will act as if they
  412. are &quot;anded&quot; together.</p>
  413. <p>If you are using a version of Ant 1.3 or earlier, the
  414. way to work with all other cases is to chain targets together
  415. to determine the specific state you want to test for.</p>
  416. <p>To see how this works, assume you have three properties:
  417. <code>prop1</code>, <code>prop2</code>, and <code>prop3</code>.
  418. You want to test that <code>prop1</code> and <code>prop2</code>
  419. are set, and that <code>prop3</code> is not. If the condition
  420. holds true you want to echo &quot;yes&quot;.</p>
  421. <p>Here is the implementation in Ant 1.3 and earlier:</p>
  422. <source><![CDATA[
  423. <target name="cond" depends="cond-if"/>
  424. <target name="cond-if" if="prop1">
  425. <antcall target="cond-if-2"/>
  426. </target>
  427. <target name="cond-if-2" if="prop2">
  428. <antcall target="cond-if-3"/>
  429. </target>
  430. <target name="cond-if-3" unless="prop3">
  431. <echo message="yes"/>
  432. </target>
  433. ]]></source>
  434. <p>Note: <code>&lt;antcall&gt;</code> tasks do <em>not</em> pass
  435. property changes back up to the environment they were called
  436. from, so you wouldn&apos;t be able to, for example, set a
  437. <code>result</code> property in the <code>cond-if-3</code> target,
  438. then do
  439. <code>&lt;echo message=&quot;result is ${result}&quot;/&gt;</code>
  440. in the <code>cond</code> target.</p>
  441. <p>Starting with Ant 1.4, you can use the
  442. <code>&lt;condition&gt;</code> task.</p>
  443. <source><![CDATA[
  444. <target name="cond" depends="cond-if,cond-else"/>
  445. <target name="check-cond">
  446. <condition property="cond-is-true">
  447. <and>
  448. <not>
  449. <equals arg1="${prop1}" arg2="$${prop1}" />
  450. </not>
  451. <not>
  452. <equals arg1="${prop2}" arg2="$${prop2}" />
  453. </not>
  454. <equals arg1="${prop3}" arg2="$${prop3}" />
  455. </and>
  456. </condition>
  457. </target>
  458. <target name="cond-if" depends="check-cond" if="cond-is-true">
  459. <echo message="yes"/>
  460. </target>
  461. <target name="cond-else" depends="check-cond" unless="cond-is-true">
  462. <echo message="no"/>
  463. </target>
  464. ]]></source>
  465. <p>This version takes advantage of two things:</p>
  466. <ul>
  467. <li>If a property <code>a</code> has not been set,
  468. <code>${a}</code> will evaluate to <code>${a}</code>.</li>
  469. <li>To get a literal <code>$</code> in Ant, you have to
  470. escape it with another <code>$</code> - this will also break
  471. the special treatment of the <code>${</code> sequence.</li>
  472. </ul>
  473. <p>Because testing for a literal <code>${property}</code> string
  474. isn&apos;t all that readable or easy to understand,
  475. post-1.4.1 Ant introduces the <code>&lt;isset&gt;</code> element
  476. to the <code>&lt;condition&gt;</code> task.</p>
  477. <p>Here is the previous example done using
  478. <code>&lt;isset&gt;</code>:</p>
  479. <source><![CDATA[
  480. <target name="check-cond">
  481. <condition property="cond-is-true">
  482. <and>
  483. <isset property="prop1"/>
  484. <isset property="prop2"/>
  485. <not>
  486. <isset property="prop3"/>
  487. </not>
  488. </and>
  489. </condition>
  490. </target>
  491. ]]></source>
  492. <p>The last option is to use a scripting language to set the
  493. properties. This can be particularly handy when you need much
  494. finer control than the simple conditions shown here but, of
  495. course, comes with the overhead of adding JAR files to support
  496. the language, to say nothing of the added maintenance in requiring
  497. two languages to implement a single system. See the
  498. <a href="manual/OptionalTasks/script.html">
  499. <code>&lt;script&gt;</code> task documentation</a> for more
  500. details.</p>
  501. </answer>
  502. </faq>
  503. <faq id="encoding">
  504. <question>How can I include national characters like German
  505. umlauts in my build file?</question>
  506. <answer>
  507. <p>You need to tell the XML parser which character encoding
  508. your build file uses, this is done inside the <a
  509. href="http://www.w3.org/TR/2000/REC-xml-20001006#sec-prolog-dtd">XML
  510. declaration</a>.</p>
  511. <p>By default the parser assumes you are using the UTF-8
  512. encoding instead of your platform&apos;s default. For most Western
  513. European countries you should set the encoding to
  514. <code>ISO-8859-1</code>. To do so, make the very first line
  515. of you build file read like</p>
  516. <source><![CDATA[
  517. <?xml version="1.0" encoding="ISO-8859-1" ?>
  518. ]]></source>
  519. </answer>
  520. </faq>
  521. <faq id="use-zip-instead-of-jar">
  522. <question>How do I use <code>jar</code>&apos;s <code>M</code> switch?
  523. I don&apos;t want a MANIFEST.</question>
  524. <answer>
  525. <p>A JAR archive is a ZIP file, so if you don&apos;t want a
  526. MANIFEST you can simply use <code>&lt;zip&gt;</code>.</p>
  527. <p>If your file names contain national characters you should
  528. know that Sun&apos;s <code>jar</code> utility like Ant&apos;s
  529. <code>&lt;jar&gt;</code> uses UTF-8 to encode their names while
  530. <code>&lt;zip&gt;</code> uses your platforms default encoding.
  531. Use the encoding attribute of <code>&lt;zip&gt;</code> if
  532. necessary.</p>
  533. </answer>
  534. </faq>
  535. <faq id="propertyvalue-as-name-for-property">
  536. <question>How can I do something like <code>&lt;property name="prop"
  537. value="${${anotherprop}}"/&gt;</code> (double expanding the property)?</question>
  538. <answer>
  539. <p>Without any external help you can not.</p>
  540. <p>With &lt;script/&gt;, which needs external libraries, you can do</p>
  541. <source><![CDATA[
  542. <script language="javascript">
  543. propname = project.getProperty("anotherprop");
  544. project.setNewProperty("prop", propname);
  545. </script>
  546. ]]></source>
  547. <p>With AntContrib (external task library) you can do <code>
  548. &lt;propertycopy name="prop" from="${anotherprop}"/&gt;</code>.</p>
  549. <p>With Ant 1.6 you can simulate the AntContribs &lt;propertycopy&gt;
  550. and avoid the need of an external library:</p>
  551. <source><![CDATA[
  552. <macrodef name="propertycopy">
  553. <attribute name="name"/>
  554. <attribute name="from"/>
  555. <sequential>
  556. <property name="@{name}" value="${@{from}}"/>
  557. </sequential>
  558. </macrodef>
  559. ]]></source>
  560. </answer>
  561. </faq>
  562. </faqsection>
  563. <faqsection title="It doesn&apos;t work (as expected)">
  564. <faq id="genral-advice">
  565. <question>General Advice</question>
  566. <answer>
  567. <p>There are many reasons why Ant doesn&apos;t behave as
  568. expected, not all of them are due to Ant bugs. See our <a
  569. href="problems.html">Having Problems?</a> page for hints that
  570. may help pinning down the reasons for your problem.</p>
  571. </answer>
  572. </faq>
  573. <faq id="always-recompiles">
  574. <question>Why does Ant always recompile all my Java files?</question>
  575. <answer>
  576. <p>In order to find out which files should be compiled, Ant
  577. compares the timestamps of the source files to those of the
  578. resulting <code>.class</code> files. Opening all source files
  579. to find out which package they belong to would be very
  580. inefficient. Instead, Ant expects you to place your
  581. source files in a directory hierarchy that mirrors your
  582. package hierarchy and to point Ant to the root of this
  583. directory tree with the <code>srcdir</code> attribute.</p>
  584. <p>Say you have <code>&lt;javac srcdir=&quot;src&quot;
  585. destdir=&quot;dest&quot;/&gt;</code>. If Ant finds a file
  586. <code>src/a/b/C.java</code>, it expects it to be in package
  587. <code>a.b</code> so that the resulting <code>.class</code>
  588. file is going to be <code>dest/a/b/C.class</code>.</p>
  589. <p>If your source-tree directory structure does not match your
  590. package structure, Ant&apos;s heuristic won&apos;t work, and
  591. it will recompile classes that are up-to-date. Ant is not the
  592. only tool that expects a source-tree layout like this.</p>
  593. <p>If you have Java source files that aren&apos;t declared to
  594. be part of any package, you can still use the <code>&lt;javac&gt;</code>
  595. task to compile these files correctly - just set the
  596. <code>srcdir</code> and <code>destdir</code> attributes to
  597. the actual directory the source
  598. files live in and the directory the class files should go into,
  599. respectively.</p>
  600. </answer>
  601. </faq>
  602. <faq id="defaultexcludes">
  603. <question>I&apos;ve used a <code>&lt;delete&gt;</code> task to
  604. delete unwanted SourceSafe control files (CVS files, editor
  605. backup files, etc.), but it doesn&apos;t seem to work; the files
  606. never get deleted. What&apos;s wrong?</question>
  607. <answer>
  608. <p>This is probably happening because, by default, Ant excludes
  609. SourceSafe control files (<code>vssver.scc</code>) and certain other
  610. files from FileSets.</p>
  611. <p>Here&apos;s what you probably did:</p>
  612. <source><![CDATA[
  613. <delete>
  614. <fileset dir="${build.src}" includes="**/vssver.scc"/>
  615. </delete>
  616. ]]></source>
  617. <p>You need to switch off the default exclusions,
  618. and it will work:</p>
  619. <source><![CDATA[
  620. <delete>
  621. <fileset dir="${build.src}" includes="**/vssver.scc"
  622. defaultexcludes="no"/>
  623. </delete>
  624. ]]></source>
  625. <p>For a complete listing of the patterns that are excluded
  626. by default, see <a href="manual/dirtasks.html#defaultexcludes">the user
  627. manual</a>.</p>
  628. </answer>
  629. </faq>
  630. <faq id="stop-dependency">
  631. <question>I have a target I want to skip if a property is set,
  632. so I have <code>unless=&quot;property&quot;</code> as an attribute
  633. of the target, but all the targets this target
  634. depends on are still executed. Why?</question>
  635. <answer>
  636. <p>The list of dependencies is generated by Ant before any of the
  637. targets are run. This allows dependent targets, such as an
  638. <code>init</code> target, to set properties that can control the
  639. execution of the targets higher in the dependency graph. This
  640. is a good thing.</p>
  641. <p>However, when your dependencies break down the
  642. higher-level task
  643. into several smaller steps, this behaviour becomes
  644. counter-intuitive. There are a couple of solutions available:
  645. </p>
  646. <ol>
  647. <li>Put the same condition on each of the dependent targets.</li>
  648. <li>Execute the steps using <code>&lt;antcall&gt;</code>,
  649. instead of specifying them inside the <code>depends</code>
  650. attribute.</li>
  651. </ol>
  652. </answer>
  653. </faq>
  654. <faq id="include-order">
  655. <question>In my <code>&lt;fileset&gt;</code>, I&apos;ve put in an
  656. <code>&lt;exclude&gt;</code> of all files followed by an
  657. <code>&lt;include&gt;</code> of just the files I want, but it
  658. isn&apos;t giving me any files at all. What&apos;s wrong?
  659. </question>
  660. <answer>
  661. <p>The order of the <code>&lt;include&gt;</code> and
  662. <code>&lt;exclude&gt;</code> tags within a <code>&lt;fileset&gt;</code>
  663. is ignored when the FileSet is created. Instead, all of the
  664. <code>&lt;include&gt;</code> elements are processed together,
  665. followed by all of the <code>&lt;exclude&gt;</code>
  666. elements. This means that the <code>&lt;exclude&gt;</code>
  667. elements only apply to the file list produced by the
  668. <code>&lt;include&gt;</code> elements.</p>
  669. <p>To get the files you want, focus on just the
  670. <code>&lt;include&gt;</code> patterns that would be necessary
  671. to get them. If you find you need to trim the list that the
  672. <code>&lt;include&gt;</code> elements produce, then use
  673. <code>&lt;exclude&gt;</code> elements.</p>
  674. </answer>
  675. </faq>
  676. <faq id="properties-not-trimmed">
  677. <question><code>ant</code> failed to build my program via javac
  678. even when I put the needed jars in an external
  679. <code>build.properties</code> file and reference them by
  680. <code>pathelement</code> or <code>classpath refid</code>.</question>
  681. <answer>
  682. <p>When <code>ant</code> loads properties from an external
  683. file it doesn&apos;t touch the value of properties, trailing blanks
  684. will not be trimmed for example.</p>
  685. <p>If the value represents a file path, like a jar needed to
  686. compile, the task which requires the value, javac for example
  687. would fail to compile since it can&apos;t find the file due to
  688. trailing spaces.</p>
  689. </answer>
  690. </faq>
  691. <faq id="winzip-lies">
  692. <question>Ant creates WAR files with a lower-case
  693. <code>web-inf</code> or JAR files with a lower-case
  694. <code>meta-inf</code> directory.</question>
  695. <answer>
  696. <p>No it doesn&apos;t.</p>
  697. <p>You may have seen these lower-case directory names in
  698. WinZIP, but WinZIP is trying to be helpful (and fails). If
  699. WinZIP encounters a filename that is all upper-case, it
  700. assumes it has come from an old DOS box and changes the case to
  701. all lower-case for you.</p>
  702. <p>If you extract (or just check) the archive with jar, you
  703. will see that the names have the correct case.</p>
  704. <p>With WinZIP (version 8.1 at least), this can be corrected in the
  705. configuration. In the Options/Configuration menu, in the View tab, General
  706. section, check the "Allow all upper case files names" box. The META-INF and
  707. WEB-INF will look correct.</p>
  708. </answer>
  709. </faq>
  710. <faq id="NoClassDefFoundError">
  711. <question>I installed Ant 1.6.x and now get
  712. <code>Exception in thread "main" java.lang.NoClassDefFoundError:
  713. </code>
  714. </question>
  715. <answer>
  716. <p>
  717. The cause of this is that there is an old version of ant somewhere in the
  718. class path or configuration.
  719. </p>
  720. <p>
  721. A version of this problem happens with jars that are in the classpath
  722. that include an embedded copy of ant classes.
  723. An example of this is some copies of weblogic.jar.
  724. </p>
  725. <p>
  726. One can check if this is the case by doing (on unix/sh):
  727. <code><pre>
  728. unset CLASSPATH
  729. ant -version
  730. </pre>
  731. </code>
  732. </p>
  733. </answer>
  734. </faq>
  735. <faq id="InstantiationException">
  736. <question>I installed Ant 1.6.x and now get
  737. <code>java.lang.InstantiationException: org.apache.tools.ant.Main</code>
  738. </question>
  739. <answer>
  740. <p>
  741. The cause of this is that there is an old version of ant somewhere in the
  742. class path or configuration.
  743. </p>
  744. <p>
  745. A version of this problem may be seen on some linux systems.
  746. Some linux systems (Fedora Core 2 for example), comes with a version
  747. of ant pre-installed. There is a configuration file called
  748. <code>/etc/ant.conf</code> which if present, the ant shell
  749. script will 'dot' include. On Fedora Core 2, the /etc/ant.conf
  750. file resets the <code>ANT_HOME</code> environment variable to
  751. <code>/usr/share/ant</code>. This causes the problem that
  752. an old version of ant (1.5.x in this cause) will be used
  753. with a new version of the ant script file.
  754. </p>
  755. <p>
  756. One can check if this is the case by doing
  757. <code>ant --noconfig -version</code>.
  758. </p>
  759. </answer>
  760. </faq>
  761. <faq id="mangled-manifest">
  762. <question>
  763. Whenever I use the Ant jar or manifest related tasks, long lines in
  764. my manifest are wrapped at 70 characters and the resulting jar does
  765. not work in my application server. Why does Ant do this?
  766. </question>
  767. <answer>
  768. <p>
  769. Ant implements the Java
  770. <a href="http://java.sun.com/j2se/1.4.2/docs/guide/jar/jar.html">Jar
  771. file specification</a>. Please refer to the notes section where it
  772. discusses the maximum allowable length of a line and the concept of
  773. continuation characters.
  774. </p>
  775. <p>
  776. If a jar file produced by Ant does not work in your appserver, and
  777. that failure is due to the wrapped manifest, then you need
  778. to consult your appserver provider, as it is a bug in their
  779. appserver. Far more likely, however, is a problem in your
  780. specification of your classpath. It is not Ant's wrapping of your
  781. classpath that is the problem.
  782. </p>
  783. <p>
  784. Do not raise a bug about this issue until you have checked to ensure
  785. that the problem is not due to your classpath specification.
  786. </p>
  787. </answer>
  788. </faq>
  789. </faqsection>
  790. <faqsection title="Ant and IDEs/Editors">
  791. <faq id="integration">
  792. <question>Is Ant supported by my IDE/Editor?</question>
  793. <answer>
  794. <p>See the <a href="external.html#IDE and Editor Integration">section
  795. on IDE integration</a> on our External Tools and Tasks page.</p>
  796. </answer>
  797. </faq>
  798. <faq id="emacs-mode">
  799. <question>Why doesn&apos;t (X)Emacs/vi/MacOS X&apos;s project builder
  800. correctly parse the error messages generated by Ant?</question>
  801. <answer>
  802. <p>Ant adds a &quot;banner&quot; with the name of the current
  803. task in front of all logging messages - and there are no built-in
  804. regular expressions in your editor that would account for
  805. this.</p>
  806. <p>You can disable this banner by invoking Ant with the
  807. <code>-emacs</code> switch. To make Ant autodetect
  808. Emacs&apos; compile mode, put this into your
  809. <code>.antrc</code> (contributed by Ville Skytt&#228;).</p>
  810. <source><![CDATA[
  811. # Detect (X)Emacs compile mode
  812. if [ "$EMACS" = "t" ] ; then
  813. ANT_ARGS="$ANT_ARGS -emacs"
  814. ANT_OPTS="$ANT_OPTS -Dbuild.compiler.emacs=true"
  815. fi
  816. ]]></source>
  817. <p>Alternatively, you can add the following snippet to your
  818. <code>.emacs</code> to make Emacs understand Ant&apos;s
  819. output.</p>
  820. <source><![CDATA[
  821. (require 'compile)
  822. (setq compilation-error-regexp-alist
  823. (append (list
  824. ;; works for jikes
  825. '("^\\s-*\\[[^]]*\\]\\s-*\\(.+\\):\\([0-9]+\\):\\([0-9]+\\):[0-9]+:[0-9]+:" 1 2 3)
  826. ;; works for javac
  827. '("^\\s-*\\[[^]]*\\]\\s-*\\(.+\\):\\([0-9]+\\):" 1 2))
  828. compilation-error-regexp-alist))
  829. ]]></source>
  830. <p>Yet another alternative that preserves most of Ant&apos;s
  831. formatting is to pipe Ant&apos;s output through the following Perl
  832. script by Dirk-Willem van Gulik:</p>
  833. <source><![CDATA[
  834. #!/usr/bin/perl
  835. #
  836. # May 2001 dirkx@apache.org - remove any
  837. # [foo] lines from the output; keeping
  838. # spacing more or less there.
  839. #
  840. $|=1;
  841. while(<STDIN>) {
  842. if (s/^(\s+)\[(\w+)\]//) {
  843. if ($2 ne $last) {
  844. print "$1\[$2\]";
  845. $s = ' ' x length($2);
  846. } else {
  847. print "$1 $s ";
  848. };
  849. $last = $2;
  850. };
  851. print;
  852. };
  853. ]]></source>
  854. </answer>
  855. </faq>
  856. </faqsection>
  857. <faqsection title="Advanced Issues">
  858. <faq id="dtd">
  859. <question>Is there a DTD that I can use to validate my build
  860. files?</question>
  861. <answer>
  862. <p>An incomplete DTD can be created by the
  863. <code>&lt;antstructure&gt;</code> task - but this one
  864. has a few problems:</p>
  865. <ul>
  866. <li>It doesn&apos;t know about required attributes. Only
  867. manual tweaking of this file can help here.</li>
  868. <li>It is not complete - if you add new tasks via
  869. <code>&lt;taskdef&gt;</code> it won&apos;t know about it. See
  870. <a href="http://www.sdv.fr/pages/casa/html/ant-dtd.en.html">this
  871. page</a> by Michel Casabianca for a solution to this
  872. problem. Note that the DTD you can download at this page
  873. is based on Ant 0.3.1.</li>
  874. <li>It may even be an invalid DTD. As Ant allows tasks
  875. writers to define arbitrary elements, name collisions will
  876. happen quite frequently - if your version of Ant contains
  877. the optional <code>&lt;test&gt;</code> and
  878. <code>&lt;junit&gt;</code> tasks, there are two XML
  879. elements named <code>test</code> (the task and the nested child
  880. element of <code>&lt;junit&gt;</code>) with different attribute
  881. lists. This problem cannot be solved; DTDs don&apos;t give a
  882. syntax rich enough to support this.</li>
  883. </ul>
  884. </answer>
  885. </faq>
  886. <faq id="xml-entity-include">
  887. <question>How do I include an XML snippet in my build file?</question>
  888. <answer>
  889. <p>You can use XML&apos;s way of including external files and let
  890. the parser do the job for Ant:</p>
  891. <source><![CDATA[
  892. <?xml version="1.0"?>
  893. <!DOCTYPE project [
  894. <!ENTITY common SYSTEM "common.xml">
  895. ]>
  896. <project name="test" default="test" basedir=".">
  897. <target name="setup">
  898. ...
  899. </target>
  900. &common;
  901. ...
  902. </project>
  903. ]]></source>
  904. <p>will literally include the contents of <code>common.xml</code> where
  905. you&apos;ve placed the <code>&amp;common;</code> entity.</p>
  906. <p>(The filename <code>common.xml</code> in this example is resolved
  907. relative to the containing XML file by the XML parser. You may also use
  908. an absolute <code>file:</code> protocol URI.)</p>
  909. <p>In combination with a DTD, this would look like this:</p>
  910. <source><![CDATA[
  911. <!DOCTYPE project PUBLIC "-//ANT//DTD project//EN" "ant.dtd" [
  912. <!ENTITY include SYSTEM "header.xml">
  913. ]>
  914. ]]></source>
  915. <p>Starting with Ant 1.6, there is a new
  916. <code>&lt;import&gt;</code> task that can (also) be used to
  917. include build file fragments. Unlike the snippets used with
  918. entity includes, the referenced files have to be complete Ant
  919. build files, though.</p>
  920. <p>The example above would become:</p>
  921. <source><![CDATA[
  922. <?xml version="1.0"?>
  923. <project name="test" default="test" basedir=".">
  924. <target name="setup">
  925. ...
  926. </target>
  927. <import file="./common.xml"/>
  928. ...
  929. </project>
  930. ]]></source>
  931. <p>Unlike entity includes, <code>&lt;import&gt;</code> will
  932. let you use Ant properties in the file name.</p>
  933. </answer>
  934. </faq>
  935. <faq id="mail-logger">
  936. <question>How do I send an email with the result of my build
  937. process?</question>
  938. <answer>
  939. <p>If you are using a nightly build of Ant 1.5 after
  940. 2001-12-14, you can use the built-in MailLogger:</p>
  941. <source><![CDATA[
  942. ant -logger org.apache.tools.ant.listener.MailLogger
  943. ]]></source>
  944. <p>See the <a href="http://cvs.apache.org/viewcvs/~checkout~/ant/docs/manual/listeners.html?content-type=text/html">Listeners
  945. &amp; Loggers</a> documentation for details on the properties
  946. required.</p>
  947. <p>For older versions of Ant, you can use a custom
  948. BuildListener that sends out an email
  949. in the buildFinished() method. Will Glozer
  950. &lt;will.glozer@jda.com&gt; has written such a listener based
  951. on <a href="http://java.sun.com/products/javamail/">JavaMail</a>.
  952. The source is:</p>
  953. <source><![CDATA[
  954. import java.io.*;
  955. import java.util.*;
  956. import javax.mail.*;
  957. import javax.mail.internet.*;
  958. import org.apache.tools.ant.*;
  959. /**
  960. * A simple listener that waits for a build to finish and sends an email
  961. * of the results. The settings are stored in "monitor.properties" and
  962. * are fairly self explanatory.
  963. *
  964. * @author Will Glozer
  965. * @version 1.05a 09/06/2000
  966. */
  967. public class BuildMonitor implements BuildListener {
  968. protected Properties props;
  969. /**
  970. * Create a new BuildMonitor.
  971. */
  972. public BuildMonitor() throws Exception {
  973. props = new Properties();
  974. InputStream is = getClass().getResourceAsStream("monitor.properties");
  975. props.load(is);
  976. is.close();
  977. }
  978. public void buildStarted(BuildEvent e) {
  979. }
  980. /**
  981. * Determine the status of the build and the actions to follow, now that
  982. * the build has completed.
  983. *
  984. * @param e Event describing the build status.
  985. */
  986. public void buildFinished(BuildEvent e) {
  987. Throwable th = e.getException();
  988. String status = (th != null) ? "failed" : "succeeded";
  989. try {
  990. String key = "build." + status;
  991. if (props.getProperty(key + ".notify").equalsIgnoreCase("false")) {
  992. return;
  993. }
  994. Session session = Session.getDefaultInstance(props, null);
  995. MimeMessage message = new MimeMessage(session);
  996. message.addRecipients(Message.RecipientType.TO, parseAddresses(
  997. props.getProperty(key + ".email.to")));
  998. message.setSubject(props.getProperty(key + ".email.subject"));
  999. BufferedReader br = new BufferedReader(new FileReader(
  1000. props.getProperty("build.log")));
  1001. StringWriter sw = new StringWriter();
  1002. String line = br.readLine();
  1003. while (line != null) {
  1004. sw.write(line);
  1005. sw.write("\n");
  1006. line = br.readLine();
  1007. }
  1008. br.close();
  1009. message.setText(sw.toString(), "UTF-8");
  1010. sw.close();
  1011. Transport transport = session.getTransport();
  1012. transport.connect();
  1013. transport.send(message);
  1014. transport.close();
  1015. } catch (Exception ex) {
  1016. System.out.println("BuildMonitor failed to send email!");
  1017. ex.printStackTrace();
  1018. }
  1019. }
  1020. /**
  1021. * Parse a comma separated list of internet email addresses.
  1022. *
  1023. * @param s The list of addresses.
  1024. * @return Array of Addresses.
  1025. */
  1026. protected Address[] parseAddresses(String s) throws Exception {
  1027. StringTokenizer st = new StringTokenizer(s, ",");
  1028. Address[] addrs = new Address[st.countTokens()];
  1029. for (int i = 0; i < addrs.length; i++) {
  1030. addrs[i] = new InternetAddress(st.nextToken());
  1031. }
  1032. return addrs;
  1033. }
  1034. public void messageLogged(BuildEvent e) {
  1035. }
  1036. public void targetStarted(BuildEvent e) {
  1037. }
  1038. public void targetFinished(BuildEvent e) {
  1039. }
  1040. public void taskStarted(BuildEvent e) {
  1041. }
  1042. public void taskFinished(BuildEvent e) {
  1043. }
  1044. }
  1045. ]]></source>
  1046. <p>With a <code>monitor.properties</code> like this:</p>
  1047. <source><![CDATA[
  1048. # configuration for build monitor
  1049. mail.transport.protocol=smtp
  1050. mail.smtp.host=<host>
  1051. mail.from=Will Glozer <will.glozer@jda.com>
  1052. build.log=build.log
  1053. build.failed.notify=true
  1054. build.failed.email.to=will.glozer@jda.com
  1055. build.failed.email.subject=Nightly build failed!
  1056. build.succeeded.notify=true
  1057. build.succeeded.email.to=will.glozer@jda.com
  1058. build.succeeded.email.subject=Nightly build succeeded!
  1059. ]]></source>
  1060. <p><code>monitor.properties</code> should be placed right next
  1061. to your compiled <code>BuildMonitor.class</code>. To use it,
  1062. invoke Ant like:</p>
  1063. <source><![CDATA[
  1064. ant -listener BuildMonitor -logfile build.log
  1065. ]]></source>
  1066. <p>Make sure that <code>mail.jar</code> from JavaMail and
  1067. <code>activation.jar</code> from the
  1068. <a href="http://java.sun.com/products/javabeans/glasgow/jaf.html">Java
  1069. Beans Activation Framework</a> are in your <code>CLASSPATH</code>.</p>
  1070. </answer>
  1071. </faq>
  1072. <faq id="listener-properties">
  1073. <question>How do I get at the properties that Ant was running
  1074. with from inside BuildListener?</question>
  1075. <answer>
  1076. <p>You can get at a hashtable with all the properties that Ant
  1077. has been using through the BuildEvent parameter. For
  1078. example:</p>
  1079. <source><![CDATA[
  1080. public void buildFinished(BuildEvent e) {
  1081. Hashtable table = e.getProject().getProperties();
  1082. String buildpath = (String)table.get("build.path");
  1083. ...
  1084. }
  1085. ]]></source>
  1086. <p>This is more accurate than just reading the same property
  1087. files that your project does, since it will give the correct
  1088. results for properties that were specified on the Ant command line.</p>
  1089. </answer>
  1090. </faq>
  1091. </faqsection>
  1092. <faqsection title="Known Problems">
  1093. <faq id="remove-cr">
  1094. <question>&lt;chmod&gt; or &lt;exec&gt; doesn&apos;t work in Ant
  1095. 1.3 on Unix</question>
  1096. <answer>
  1097. <p>The <code>antRun</code> script in <code>ANT_HOME/bin</code>
  1098. has DOS instead of Unix line endings; you must remove the
  1099. carriage-return characters from this file. This can be done by
  1100. using Ant&apos;s <code>&lt;fixcrlf&gt;</code> task
  1101. or something like:</p>
  1102. <source><![CDATA[
  1103. tr -d '\r' < $ANT_HOME/bin/antRun > /tmp/foo
  1104. mv /tmp/foo $ANT_HOME/bin/antRun
  1105. ]]></source>
  1106. </answer>
  1107. </faq>
  1108. <faq id="javadoc-cannot-execute">
  1109. <question>JavaDoc failed: java.io.IOException: javadoc: cannot execute</question>
  1110. <answer>
  1111. <p>There is a bug in the Solaris reference implementation of
  1112. 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>).
  1113. This also appears to be true under Linux. Moving the JDK to
  1114. the front of the PATH fixes the problem.</p>
  1115. </answer>
  1116. </faq>
  1117. <faq id="delegating-classloader">
  1118. <question>&lt;style&gt; or &lt;junit&gt; ignores my
  1119. &lt;classpath&gt;</question>
  1120. <answer>
  1121. <p>These tasks don&apos;t ignore your classpath setting, you
  1122. are facing a common problem with delegating classloaders.</p>
  1123. <p>This question collects a common type of problem: A task
  1124. needs an external library and it has a nested classpath
  1125. element so that you can point it to this external library, but
  1126. that doesn&apos;t work unless you put the external library
  1127. into the <code>CLASSPATH</code> or place it in
  1128. <code>ANT_HOME/lib</code>.</p>
  1129. <p>Some background is necessary before we can discuss
  1130. solutions for <a href="#delegating-classloader-1.5">Ant
  1131. 1.5.x</a> and <a href="#delegating-classloader-1.6">Ant
  1132. 1.6.x</a>.</p>
  1133. <p>When you specify a nested <code>&lt;classpath&gt;</code> in
  1134. Ant, Ant creates a new class loader that uses the path you
  1135. have specified. It then tries to load additional classes from
  1136. this classloader.</p>
  1137. <p>In most cases - for example using &lt;style&gt; or
  1138. &lt;junit&gt; - Ant doesn&apos;t load the external library
  1139. directly, it is the loaded class that does so.</p>
  1140. <p>In the case of <code>&lt;junit&gt;</code> it is the task
  1141. implementation itself and in the case of
  1142. <code>&lt;style&gt;</code> it is the implementation of the
  1143. <code>org.apache.tools.ant.taskdefs.XSLTLiaison</code>
  1144. class.</p>
  1145. <p>Ant&apos;s class loader implementation uses Java&apos;s
  1146. delegation model, see <a
  1147. 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>
  1148. the paragraph</p>
  1149. <blockquote>The <code>ClassLoader</code> class uses a
  1150. delegation model to search for classes and resources. Each
  1151. instance of <code>ClassLoader</code> has an associated parent
  1152. class loader. When called upon to find a class or resource, a
  1153. <code>ClassLoader</code> instance will delegate the search for
  1154. the class or resource to its parent class loader before
  1155. attempting to find the class or resource itself. The virtual
  1156. machine&apos;s built-in class loader, called the bootstrap
  1157. class loader, does not itself have a parent but may serve as
  1158. the parent of a <code>ClassLoader</code>
  1159. instance.</blockquote>
  1160. <p>The possible solutions depend on the version of Ant you
  1161. use, see the next sections.</p>
  1162. </answer>
  1163. </faq>
  1164. <faq id="delegating-classloader-1.5">
  1165. <question>&lt;style&gt; or &lt;junit&gt; ignores my
  1166. &lt;classpath&gt; - Ant 1.5.x version</question>
  1167. <answer>
  1168. <p>Please read <a href="#delegating-classloader">the previous
  1169. entry</a> before you go ahead.</p>
  1170. <p>First of all let&apos;s state that Ant's wrapper script
  1171. (<code>ant</code> or <code>ant.bat</code>) adds all
  1172. <code>.jar</code> files from <code>ANT_HOME/lib</code> to
  1173. <code>CLASSPATH</code>, therefore &quot;in
  1174. <code>CLASSPATH</code>&quot; shall mean &quot;either in your
  1175. <code>CLASSPATH</code> environment variable or
  1176. <code>ANT_HOME/lib</code>&quot; for the rest of this
  1177. answer.</p>
  1178. <p>The root of the problem is that the class that needs the
  1179. external library is on the <code>CLASSPATH</code>.</p>
  1180. <p>Let's see what happens when you load the &lt;junit&gt;
  1181. task. Ant&apos;s class loader will consult the
  1182. bootstrap class loader first, which tries to load classes from
  1183. <code>CLASSPATH</code>. The bootstrap class loader
  1184. doesn&apos;t know anything about Ant&apos;s class loader or
  1185. even the path you have specified.</p>
  1186. <p>If the bootstrap class loader can load the class Ant has
  1187. asked it to load (which it can if <code>optional.jar</code> is
  1188. part of <code>CLASSPATH</code>), this class will try to load
  1189. the external library from <code>CLASSPATH</code> as well - it
  1190. doesn&apos;t know anything else - and will not find it unless
  1191. the library is in <code>CLASSPATH</code> as well.</p>
  1192. <p>To solve this, you have two major options:</p>
  1193. <ol>
  1194. <li>put all external libraries you need in
  1195. <code>CLASSPATH</code> as well this is not what you want,
  1196. otherwise you wouldn&apos;t have found this FAQ entry.</li>
  1197. <li>remove the class that loads the external library from
  1198. the <code>CLASSPATH</code>.</li>
  1199. </ol>
  1200. <p>The easiest way to do this is to remove
  1201. <code>optional.jar</code> from <code>ANT_HOME/lib</code>. If
  1202. you do so, you will have to <code>&lt;taskdef&gt;</code> all
  1203. optional tasks and use nested <code>&lt;classpath&gt;</code>
  1204. elements in the <code>&lt;taskdef&gt;</code> tasks that point
  1205. to the new location of <code>optional.jar</code>. Also,
  1206. don&apos;t forget to add the new location of
  1207. <code>optional.jar</code> to the
  1208. <code>&lt;classpath&gt;</code> of your
  1209. <code>&lt;style&gt;</code> or <code>&lt;junit&gt;</code>
  1210. task.</p>
  1211. <p>If you want to avoid to <code>&lt;taskdef&gt;</code> all
  1212. optional tasks you need, the only other option is to remove
  1213. the classes that should not be loaded via the bootstrap class
  1214. loader from <code>optional.jar</code> and put them into a
  1215. separate archive. Add this separate archive to the
  1216. <code>&lt;classpath&gt;</code> of your
  1217. <code>&lt;style&gt;</code> or <code>&lt;junit&gt;</code> task
  1218. - and make sure the separate archive is not in
  1219. <code>CLASSPATH</code>.</p>
  1220. <p>In the case of <code>&lt;junit&gt;</code> you&apos;d have
  1221. to remove all classes that are in the
  1222. <code>org/apache/tools/ant/taskdefs/optional/junit</code>
  1223. directory, in the <code>&lt;style&gt;</code> case it is one of
  1224. the <code>*Liaison</code> classes in
  1225. <code>org/apache/tools/ant/taskdefs/optional</code>.</p>
  1226. <p>If you use the option to break up <code>optional.jar</code>
  1227. for <code>&lt;junit&gt;</code> or remove
  1228. <code>ant-junit.jar</code>, you still have to use a
  1229. <code>&lt;taskdef&gt;</code> with a nested
  1230. <code>&lt;classpath&gt;</code> to define the junit task.</p>
  1231. </answer>
  1232. </faq>
  1233. <faq id="delegating-classloader-1.6">
  1234. <question>&lt;style&gt; or &lt;junit&gt; ignores my
  1235. &lt;classpath&gt; - Ant 1.6.x version</question>
  1236. <answer>
  1237. <p>Please read <a href="#delegating-classloader">the general
  1238. entry</a> before you go ahead.</p>
  1239. <p>The wrapper script of Ant 1.6.x no longer adds the contents
  1240. of <code>ANT_HOME/lib</code> to <code>CLASSPATH</code>,
  1241. instead Ant will create a classloader on top of the bootstrap
  1242. classloader - let's call it the coreloader for the rest of
  1243. this answer - which holds the contents of
  1244. <code>ANT_HOME/lib</code>. Ant's core and its tasks will be
  1245. loaded through this classloader and not the bootstrap
  1246. classloader.</p>
  1247. <p>This causes some small but notable differences between Ant
  1248. 1.5.x and 1.6.x. Most importantly, a third-party task that is
  1249. part of <code>CLASSPATH</code> will no longer work in Ant
  1250. 1.6.x since the task now can't find Ant's classes. In a sense
  1251. this is the same problem this entry is about, only
  1252. <code>ant.jar</code> has become the external library in
  1253. question now.</p>
  1254. <p>This coreloader also holds the contents of
  1255. <code>~/.ant/lib</code> and any file or directory that has
  1256. been specified using Ant's <code>-lib</code> command line
  1257. argument.</p>
  1258. <p>Let's see what happens when you load the &lt;junit&gt;
  1259. task. Ant&apos;s class loader will consult the bootstrap
  1260. class loader first, which tries to load classes from
  1261. <code>CLASSPATH</code>. The bootstrap class loader
  1262. doesn&apos;t know anything about Ant&apos;s class loader or
  1263. even the path you have specified. If it fails to find the
  1264. class using the bootstrap classloader it will try the
  1265. coreloader next. Again, the coreloader doesn't know anything
  1266. about your path.</p>
  1267. <p>If the coreloader can load the class Ant has asked it to
  1268. load (which it can if <code>ant-junit.jar</code> is in
  1269. <code>ANT_HOME/lib</code>), this class will try to load the
  1270. external library from coreloader as well - it doesn&apos;t
  1271. know anything else - and will not find it unless the library
  1272. is in <code>CLASSPATH</code> or the coreloader as well.</p>
  1273. <p>To solve this, you have the following major options:</p>
  1274. <ol>
  1275. <li>put all external libraries you need in
  1276. <code>CLASSPATH</code> as well this is not what you want,
  1277. otherwise you wouldn&apos;t have found this FAQ entry.</li>
  1278. <li>put all external libraries you need in
  1279. <code>ANT_HOME/lib</code> or <code>.ant/lib</code>. This
  1280. probably still isn't what you want, but you might reconsider
  1281. the <code>.ant/lib</code> option.</li>
  1282. <li>Always start Ant with the <code>-lib</code> command line
  1283. switch and point to your external libraries (or the
  1284. directories holding them).</li>
  1285. <li>remove the class that loads the external library from
  1286. the coreloader.</li>
  1287. </ol>
  1288. <p>In Ant 1.6 <code>optional.jar</code> has been split into
  1289. multiple jars, each one containing classes with the same
  1290. dependencies on external libraries. You can move the
  1291. "offending" jar out of <code>ANT_HOME/lib</code>. For the
  1292. <code>&lt;junit&gt;</code> task it would be
  1293. <code>ant-junit.jar</code> and for <code>&lt;style&gt;</code>
  1294. it would be <code>ant-trax.jar</code>,
  1295. <code>ant-xalan1.jar</code> or <code>ant-xslp.jar</code> -
  1296. depending on the processor you use.</p>
  1297. <p>If you do so, you will have to <code>&lt;taskdef&gt;</code>
  1298. all optional tasks that need the external libary and use
  1299. nested <code>&lt;classpath&gt;</code> elements in the
  1300. <code>&lt;taskdef&gt;</code> tasks that point to the new
  1301. location of <code>ant-*.jar</code>. Also, don&apos;t forget
  1302. to add the new location of <code>ant-*.jar</code> to the
  1303. <code>&lt;classpath&gt;</code> of your
  1304. <code>&lt;style&gt;</code> or <code>&lt;junit&gt;</code>
  1305. task.</p>
  1306. <p>For example</p>
  1307. <source><![CDATA[
  1308. <taskdef name="junit"
  1309. class="org.apache.tools.ant.taskdefs.optional.junit.JUnitTask">
  1310. <classpath>
  1311. <pathelement location="HOME-OF/junit.jar"/>
  1312. <pathelement location="NEW-HOME-OF/ant-junit.jar"/>
  1313. </classpath>
  1314. </taskdef>
  1315. ]]></source>
  1316. </answer>
  1317. </faq>
  1318. <faq id="winxp-jdk14-ant14">
  1319. <question>When running Ant 1.4 on Windows XP and JDK 1.4, I get
  1320. various errors when trying to <code>&lt;exec&gt;</code>, fork
  1321. <code>&lt;java&gt;</code> or access environment
  1322. variables.</question>
  1323. <answer>
  1324. <p>Ant &lt; 1.5 doesn&apos;t recognize Windows XP as a flavor
  1325. of Windows that runs <code>CMD.EXE</code> instead of
  1326. <code>COMMAND.COM</code>. JDK 1.3 will tell Ant that Windows
  1327. XP is Windows 2000 so the problem doesn&apos;t show up
  1328. there.</p>
  1329. <p>Apart from upgrading to Ant 1.5 or better, setting the
  1330. environment variable <code>ANT_OPTS</code> to
  1331. <code>-Dos.name=Windows_NT</code> prior to invoking Ant has
  1332. been confirmed as a workaround.</p>
  1333. </answer>
  1334. </faq>
  1335. <faq id="1.5-cygwin-sh">
  1336. <question>The <code>ant</code> wrapper script of Ant 1.5 fails
  1337. for Cygwin if <code>ANT_HOME</code> is set to a Windows style
  1338. path.</question>
  1339. <answer>
  1340. <p>This problem has been reported only hours after Ant 1.5 has
  1341. been released, see <a
  1342. href="http://issues.apache.org/bugzilla/show_bug.cgi?id=10664">Bug
  1343. 10664</a> and all its duplicates.</p>
  1344. <p>A fixed version of the wrapper script can be found <a
  1345. href="http://ant.apache.org/old-releases/v1.5/errata/">here</a>.
  1346. Simply replace your script with this version.</p>
  1347. </answer>
  1348. </faq>
  1349. <faq id="1.5.2-zip-broken">
  1350. <question><code>&lt;zip&gt;</code> is broken in Ant 1.5.2.</question>
  1351. <answer>
  1352. <p>Yes, it is.</p>
  1353. <p>The problem reported by most people - see <a
  1354. href="http://issues.apache.org/bugzilla/show_bug.cgi?id=17648">Bug
  1355. 17648</a> and all its duplicates - is that Ant creates
  1356. archives that a partially unreadable by WinZIP. Luckily
  1357. <code>jar</code> deals with the archives and so the generated
  1358. jars/wars/ears will most likely work for you anyway.</p>
  1359. <p>There are additional problems, see bugs <a
  1360. href="http://issues.apache.org/bugzilla/show_bug.cgi?id=17780">Bug
  1361. 17780</a>, <a
  1362. href="http://issues.apache.org/bugzilla/show_bug.cgi?id=17871">Bug
  1363. 17871</a> and <a
  1364. href="http://issues.apache.org/bugzilla/show_bug.cgi?id=18403">Bug
  1365. 18403</a>. All of them are supposed to be fixed with Ant
  1366. 1.5.3 (and only 18403 should exist in 1.5.3beta1).</p>
  1367. </answer>
  1368. </faq>
  1369. <faq id="unknownelement.taskcontainer">
  1370. <question>
  1371. Why do my custom task containers see Unknown Elements in Ant 1.6
  1372. - they worked in Ant 1.5?
  1373. </question>
  1374. <answer>
  1375. <p>
  1376. The objects added in TaskContainer.addTask(Task task)
  1377. have changed from Tasks to UnknownElements.
  1378. </p>
  1379. <p>
  1380. There was a number of valid reasons for this change. But the backward
  1381. compatibility problems were not noticed until after Ant 1.6.0 was
  1382. released.
  1383. </p>
  1384. <p>
  1385. Your container class will need to be modified to check if the Task
  1386. is an UnknownElement and call perform on it to
  1387. convert it to a Task and to execute it.
  1388. (see apache.tools.ant.taskdefs.Sequential)
  1389. </p>
  1390. <p>
  1391. If you want to do more processing on the task,
  1392. you need to use the techniques in apache.tools.ant.taskdefs.Antlib#execute()
  1393. This does make use of one 1.6 method call (UE#getRealObject()),
  1394. you need to use UE#getTask() instead - this will
  1395. return null for non tasks (types like fileset id=x).
  1396. </p>
  1397. <p>
  1398. So.. iterate over the tasks, if they are UEs, convert them to
  1399. tasks, using UE#maybeConfigure and UE#getTask()
  1400. </p>
  1401. <source><![CDATA[
  1402. for (Iterator i = tasks.iterator(); i.hasNext();) {
  1403. Task t = (Task) i.next();
  1404. if (t instanceof UnknownElement) {
  1405. ((UnknownElement) t).maybeConfigure();
  1406. t = ((UnknownElement) t).getTask();
  1407. if (t == null) {
  1408. continue;
  1409. }
  1410. }
  1411. // .... original Custom code
  1412. }
  1413. ]]></source>
  1414. <p>
  1415. This approach should work for ant1.5 and ant1.6.
  1416. </p>
  1417. </answer>
  1418. </faq>
  1419. <faq id="java.exception.stacktrace">
  1420. <question>
  1421. The program I run via &lt;java&gt; throws an exception but I
  1422. can't seem to get the full stack trace.
  1423. </question>
  1424. <answer>
  1425. <p>This is a know bug that has been fixed after the release of
  1426. Ant 1.6.1.</p>
  1427. <p>As a workaround, run your &lt;java&gt; task with
  1428. <code>fork="true"</code> and Ant will display the full
  1429. trace.</p>
  1430. </answer>
  1431. </faq>
  1432. <faq id="junit-no-runtime-xml">
  1433. <question>
  1434. Using format=&quot;xml&quot;, &lt;junit&gt; fails with a
  1435. <code>NoClassDefFoundError</code> if forked.
  1436. </question>
  1437. <answer>
  1438. <p>The XML formatter needs the <a
  1439. href="http://www.w3.org/DOM/">DOM classes</a> to work. If you
  1440. are using JDK 1.4 or later they are included with your Java
  1441. Runtime and this problem won't occur. If you are running JDK
  1442. 1.3 or earlier, the DOM classes have to be on your
  1443. &lt;junit&gt; task's &lt;classpath&gt;.</p>
  1444. <p>Prior to Ant 1.6.0 Ant would include the DOM classes from
  1445. the XML parser that is used by Ant itself if you set the
  1446. includeAntRuntime attribute to true (the default). With Ant
  1447. 1.6.0 this has been changed as this behavior made it
  1448. impossible to use a different XML parser in your tests.</p>
  1449. <p>This means that you have to take care of the DOM classes
  1450. explicitly starting with Ant 1.6.0. If you don't need to set
  1451. up a different XML parser for your tests, the easiest solution
  1452. is to add</p>
  1453. <source><![CDATA[
  1454. <pathelement path="${ant.home}/lib/xml-apis.jar:${ant.home}/lib/xercesImpl.jar"/>
  1455. ]]></source>
  1456. <p>to your task's &lt;classpath&gt;.</p>
  1457. </answer>
  1458. </faq>
  1459. <faq id="xalan-jdk1.5">
  1460. <question>
  1461. <code>&lt;junitreport&gt;</code> doesn't work with JDK 1.5 but
  1462. worked fine with JDK 1.4.
  1463. </question>
  1464. <answer>
  1465. <p>While JDK 1.4.x contains a version of Xalan-J 2, JDK 1.5
  1466. (and later?) have <a
  1467. href="http://java.sun.com/j2se/1.5.0/compatibility.html#4959783">moved
  1468. to XSLTC</a>. Since this task uses Xalan's redirect
  1469. extensions for its internal stylesheet, Ant prior to 1.6.2 didn't support
  1470. XSLTC. This means that you have to install <a
  1471. href="http://xml.apache.org/xalan-j/">Xalan-J 2</a> in order
  1472. to use this task with JDK 1.5 in older versions of Ant.</p>
  1473. <p>Starting with Ant 1.6.2 <code>&lt;junitreport&gt;</code>
  1474. supports JDK 1.5.</p>
  1475. </answer>
  1476. </faq>
  1477. </faqsection>
  1478. </document>