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

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