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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550
  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="always-recompiles">
  519. <question>Why does Ant always recompile all my Java files?</question>
  520. <answer>
  521. <p>In order to find out which files should be compiled, Ant
  522. compares the timestamps of the source files to those of the
  523. resulting <code>.class</code> files. Opening all source files
  524. to find out which package they belong to would be very
  525. inefficient. Instead, Ant expects you to place your
  526. source files in a directory hierarchy that mirrors your
  527. package hierarchy and to point Ant to the root of this
  528. directory tree with the <code>srcdir</code> attribute.</p>
  529. <p>Say you have <code>&lt;javac srcdir=&quot;src&quot;
  530. destdir=&quot;dest&quot;/&gt;</code>. If Ant finds a file
  531. <code>src/a/b/C.java</code>, it expects it to be in package
  532. <code>a.b</code> so that the resulting <code>.class</code>
  533. file is going to be <code>dest/a/b/C.class</code>.</p>
  534. <p>If your source-tree directory structure does not match your
  535. package structure, Ant&apos;s heuristic won&apos;t work, and
  536. it will recompile classes that are up-to-date. Ant is not the
  537. only tool that expects a source-tree layout like this.</p>
  538. <p>If you have Java source files that aren&apos;t declared to
  539. be part of any package, you can still use the <code>&lt;javac&gt;</code>
  540. task to compile these files correctly - just set the
  541. <code>srcdir</code> and <code>destdir</code> attributes to
  542. the actual directory the source
  543. files live in and the directory the class files should go into,
  544. respectively.</p>
  545. </answer>
  546. </faq>
  547. <faq id="defaultexcludes">
  548. <question>I&apos;ve used a <code>&lt;delete&gt;</code> task to
  549. delete unwanted SourceSafe control files (CVS files, editor
  550. backup files, etc.), but it doesn&apos;t seem to work; the files
  551. never get deleted. What&apos;s wrong?</question>
  552. <answer>
  553. <p>This is probably happening because, by default, Ant excludes
  554. SourceSafe control files (<code>vssver.scc</code>) and certain other
  555. files from FileSets.</p>
  556. <p>Here&apos;s what you probably did:</p>
  557. <source><![CDATA[
  558. <delete>
  559. <fileset dir="${build.src}" includes="**/vssver.scc"/>
  560. </delete>
  561. ]]></source>
  562. <p>You need to switch off the default exclusions,
  563. and it will work:</p>
  564. <source><![CDATA[
  565. <delete>
  566. <fileset dir="${build.src}" includes="**/vssver.scc"
  567. defaultexcludes="no"/>
  568. </delete>
  569. ]]></source>
  570. <p>For a complete listing of the patterns that are excluded
  571. by default, see <a href="manual/dirtasks.html#defaultexcludes">the user
  572. manual</a>.</p>
  573. </answer>
  574. </faq>
  575. <faq id="stop-dependency">
  576. <question>I have a target I want to skip if a property is set,
  577. so I have <code>unless=&quot;property&quot;</code> as an attribute
  578. of the target, but all the targets this target
  579. depends on are still executed. Why?</question>
  580. <answer>
  581. <p>The list of dependencies is generated by Ant before any of the
  582. targets are run. This allows dependent targets, such as an
  583. <code>init</code> target, to set properties that can control the
  584. execution of the targets higher in the dependency graph. This
  585. is a good thing.</p>
  586. <p>However, when your dependencies break down the
  587. higher-level task
  588. into several smaller steps, this behaviour becomes
  589. counter-intuitive. There are a couple of solutions available:
  590. </p>
  591. <ol>
  592. <li>Put the same condition on each of the dependent targets.</li>
  593. <li>Execute the steps using <code>&lt;antcall&gt;</code>,
  594. instead of specifying them inside the <code>depends</code>
  595. attribute.</li>
  596. </ol>
  597. </answer>
  598. </faq>
  599. <faq id="include-order">
  600. <question>In my <code>&lt;fileset&gt;</code>, I&apos;ve put in an
  601. <code>&lt;exclude&gt;</code> of all files followed by an
  602. <code>&lt;include&gt;</code> of just the files I want, but it
  603. isn&apos;t giving me any files at all. What&apos;s wrong?
  604. </question>
  605. <answer>
  606. <p>The order of the <code>&lt;include&gt;</code> and
  607. <code>&lt;exclude&gt;</code> tags within a <code>&lt;fileset&gt;</code>
  608. is ignored when the FileSet is created. Instead, all of the
  609. <code>&lt;include&gt;</code> elements are processed together,
  610. followed by all of the <code>&lt;exclude&gt;</code>
  611. elements. This means that the <code>&lt;exclude&gt;</code>
  612. elements only apply to the file list produced by the
  613. <code>&lt;include&gt;</code> elements.</p>
  614. <p>To get the files you want, focus on just the
  615. <code>&lt;include&gt;</code> patterns that would be necessary
  616. to get them. If you find you need to trim the list that the
  617. <code>&lt;include&gt;</code> elements produce, then use
  618. <code>&lt;exclude&gt;</code> elements.</p>
  619. </answer>
  620. </faq>
  621. <faq id="properties-not-trimmed">
  622. <question><code>ant</code> failed to build my program via javac
  623. even when I put the needed jars in an external
  624. <code>build.properties</code> file and reference them by
  625. <code>pathelement</code> or <code>classpath refid</code>.</question>
  626. <answer>
  627. <p>When <code>ant</code> loads properties from an external
  628. file it dosn&apos;t touch the value of properties, trailing blanks
  629. will not be trimmed for example.</p>
  630. <p>If the value represents a file path, like a jar needed to
  631. compile, the task which requires the value, javac for example
  632. would fail to compile since it can&apos;t find the file due to
  633. trailing spaces.</p>
  634. </answer>
  635. </faq>
  636. <faq id="winzip-lies">
  637. <question>Ant creates WAR files with a lower-case
  638. <code>web-inf</code> or JAR files with a lower-case
  639. <code>meta-inf</code> directory.</question>
  640. <answer>
  641. <p>No it doesn&apos;t.</p>
  642. <p>You may have seen these lower-case directory names in
  643. WinZIP, but WinZIP is trying to be helpful (and fails). If
  644. WinZIP encounters a filename that is all upper-case, it
  645. assumes it has come from an old DOS box and changes the case to
  646. all lower-case for you.</p>
  647. <p>If you extract (or just check) the archive with jar, you
  648. will see that the names have the correct case.</p>
  649. <p>With WinZIP (version 8.1 at least), this can be corrected in the
  650. configuration. In the Options/Configuration menu, in the View tab, General
  651. section, check the "Allow all upper case files names" box. The META-INF and
  652. WEB-INF will look correct.</p>
  653. </answer>
  654. </faq>
  655. <faq id="NoClassDefFoundError">
  656. <question>I installed Ant 1.6.x and now get
  657. <code>Exception in thread "main" java.lang.NoClassDefFoundError:
  658. org/apache/tools/ant/launch/Launcher</code>
  659. </question>
  660. <answer>
  661. <p>
  662. The cause of this is that there is an old version of ant somewhere in the
  663. class path or configuration.
  664. </p>
  665. <p>
  666. A version of this problem may be seen on some linux systems.
  667. Some linux systems (Fedora Core 2 for example), comes with a version
  668. of ant pre-installed. There is a configuration file called
  669. <code>/etc/ant.conf</code> which if present, the ant shell
  670. script will 'dot' include. On Fedora Core 2, the /etc/ant.conf
  671. file resets the <code>ANT_HOME</code> environment variable to
  672. <code>/usr/share/ant</code>. This causes the problem that
  673. an old version of ant (1.5.x in this cause) will be used
  674. with a new version of the ant script file.
  675. </p>
  676. <p>
  677. One can check if this is the case by doing
  678. <code>ant --noconfig -version</code>.
  679. </p>
  680. </answer>
  681. </faq>
  682. </faqsection>
  683. <faqsection title="Ant and IDEs/Editors">
  684. <faq id="integration">
  685. <question>Is Ant supported by my IDE/Editor?</question>
  686. <answer>
  687. <p>See the <a href="external.html#IDE and Editor Integration">section
  688. on IDE integration</a> on our External Tools and Tasks page.</p>
  689. </answer>
  690. </faq>
  691. <faq id="emacs-mode">
  692. <question>Why doesn&apos;t (X)Emacs/vi/MacOS X&apos;s project builder
  693. correctly parse the error messages generated by Ant?</question>
  694. <answer>
  695. <p>Ant adds a &quot;banner&quot; with the name of the current
  696. task in front of all logging messages - and there are no built-in
  697. regular expressions in your editor that would account for
  698. this.</p>
  699. <p>You can disable this banner by invoking Ant with the
  700. <code>-emacs</code> switch. To make Ant autodetect
  701. Emacs&apos; compile mode, put this into your
  702. <code>.antrc</code> (contributed by Ville Skytt&#228;).</p>
  703. <source><![CDATA[
  704. # Detect (X)Emacs compile mode
  705. if [ "$EMACS" = "t" ] ; then
  706. ANT_ARGS="$ANT_ARGS -emacs"
  707. ANT_OPTS="$ANT_OPTS -Dbuild.compiler.emacs=true"
  708. fi
  709. ]]></source>
  710. <p>Alternatively, you can add the following snippet to your
  711. <code>.emacs</code> to make Emacs understand Ant&apos;s
  712. output.</p>
  713. <source><![CDATA[
  714. (require 'compile)
  715. (setq compilation-error-regexp-alist
  716. (append (list
  717. ;; works for jikes
  718. '("^\\s-*\\[[^]]*\\]\\s-*\\(.+\\):\\([0-9]+\\):\\([0-9]+\\):[0-9]+:[0-9]+:" 1 2 3)
  719. ;; works for javac
  720. '("^\\s-*\\[[^]]*\\]\\s-*\\(.+\\):\\([0-9]+\\):" 1 2))
  721. compilation-error-regexp-alist))
  722. ]]></source>
  723. <p>Yet another alternative that preserves most of Ant&apos;s
  724. formatting is to pipe Ant&apos;s output through the following Perl
  725. script by Dirk-Willem van Gulik:</p>
  726. <source><![CDATA[
  727. #!/usr/bin/perl
  728. #
  729. # May 2001 dirkx@apache.org - remove any
  730. # [foo] lines from the output; keeping
  731. # spacing more or less there.
  732. #
  733. $|=1;
  734. while(<STDIN>) {
  735. if (s/^(\s+)\[(\w+)\]//) {
  736. if ($2 ne $last) {
  737. print "$1\[$2\]";
  738. $s = ' ' x length($2);
  739. } else {
  740. print "$1 $s ";
  741. };
  742. $last = $2;
  743. };
  744. print;
  745. };
  746. ]]></source>
  747. </answer>
  748. </faq>
  749. </faqsection>
  750. <faqsection title="Advanced Issues">
  751. <faq id="dtd">
  752. <question>Is there a DTD that I can use to validate my build
  753. files?</question>
  754. <answer>
  755. <p>An incomplete DTD can be created by the
  756. <code>&lt;antstructure&gt;</code> task - but this one
  757. has a few problems:</p>
  758. <ul>
  759. <li>It doesn&apos;t know about required attributes. Only
  760. manual tweaking of this file can help here.</li>
  761. <li>It is not complete - if you add new tasks via
  762. <code>&lt;taskdef&gt;</code> it won&apos;t know about it. See
  763. <a href="http://www.sdv.fr/pages/casa/html/ant-dtd.en.html">this
  764. page</a> by Michel Casabianca for a solution to this
  765. problem. Note that the DTD you can download at this page
  766. is based on Ant 0.3.1.</li>
  767. <li>It may even be an invalid DTD. As Ant allows tasks
  768. writers to define arbitrary elements, name collisions will
  769. happen quite frequently - if your version of Ant contains
  770. the optional <code>&lt;test&gt;</code> and
  771. <code>&lt;junit&gt;</code> tasks, there are two XML
  772. elements named <code>test</code> (the task and the nested child
  773. element of <code>&lt;junit&gt;</code>) with different attribute
  774. lists. This problem cannot be solved; DTDs don&apos;t give a
  775. syntax rich enough to support this.</li>
  776. </ul>
  777. </answer>
  778. </faq>
  779. <faq id="xml-entity-include">
  780. <question>How do I include an XML snippet in my build file?</question>
  781. <answer>
  782. <p>You can use XML&apos;s way of including external files and let
  783. the parser do the job for Ant:</p>
  784. <source><![CDATA[
  785. <?xml version="1.0"?>
  786. <!DOCTYPE project [
  787. <!ENTITY common SYSTEM "file:./common.xml">
  788. ]>
  789. <project name="test" default="test" basedir=".">
  790. <target name="setup">
  791. ...
  792. </target>
  793. &common;
  794. ...
  795. </project>
  796. ]]></source>
  797. <p>will literally include the contents of <code>common.xml</code> where
  798. you&apos;ve placed the <code>&amp;common;</code> entity.</p>
  799. <p>In combination with a DTD, this would look like this:</p>
  800. <source><![CDATA[
  801. <!DOCTYPE project PUBLIC "-//ANT//DTD project//EN" "file:./ant.dtd" [
  802. <!ENTITY include SYSTEM "file:./header.xml">
  803. ]>
  804. ]]></source>
  805. <p>Starting with Ant 1.6, there is a new
  806. <code>&lt;import&gt;</code> task that can (also) be used to
  807. include build file fragments. Unlike the snippets used with
  808. entity includes, the referenced files have to be complete Ant
  809. build files, though.</p>
  810. <p>The example above would become:</p>
  811. <source><![CDATA[
  812. <?xml version="1.0"?>
  813. <project name="test" default="test" basedir=".">
  814. <target name="setup">
  815. ...
  816. </target>
  817. <import file="./common.xml"/>
  818. ...
  819. </project>
  820. ]]></source>
  821. <p>Unlike entity includes, <code>&lt;import&gt;</code> will
  822. let you use Ant properties in the file name.</p>
  823. </answer>
  824. </faq>
  825. <faq id="mail-logger">
  826. <question>How do I send an email with the result of my build
  827. process?</question>
  828. <answer>
  829. <p>If you are using a nightly build of Ant 1.5 after
  830. 2001-12-14, you can use the built-in MailLogger:</p>
  831. <source><![CDATA[
  832. ant -logger org.apache.tools.ant.listener.MailLogger
  833. ]]></source>
  834. <p>See the <a href="http://cvs.apache.org/viewcvs/~checkout~/ant/docs/manual/listeners.html?content-type=text/html">Listeners
  835. &amp; Loggers</a> documentation for details on the properties
  836. required.</p>
  837. <p>For older versions of Ant, you can use a custom
  838. BuildListener that sends out an email
  839. in the buildFinished() method. Will Glozer
  840. &lt;will.glozer@jda.com&gt; has written such a listener based
  841. on <a href="http://java.sun.com/products/javamail/">JavaMail</a>.
  842. The source is:</p>
  843. <source><![CDATA[
  844. import java.io.*;
  845. import java.util.*;
  846. import javax.mail.*;
  847. import javax.mail.internet.*;
  848. import org.apache.tools.ant.*;
  849. /**
  850. * A simple listener that waits for a build to finish and sends an email
  851. * of the results. The settings are stored in "monitor.properties" and
  852. * are fairly self explanatory.
  853. *
  854. * @author Will Glozer
  855. * @version 1.05a 09/06/2000
  856. */
  857. public class BuildMonitor implements BuildListener {
  858. protected Properties props;
  859. /**
  860. * Create a new BuildMonitor.
  861. */
  862. public BuildMonitor() throws Exception {
  863. props = new Properties();
  864. InputStream is = getClass().getResourceAsStream("monitor.properties");
  865. props.load(is);
  866. is.close();
  867. }
  868. public void buildStarted(BuildEvent e) {
  869. }
  870. /**
  871. * Determine the status of the build and the actions to follow, now that
  872. * the build has completed.
  873. *
  874. * @param e Event describing the build status.
  875. */
  876. public void buildFinished(BuildEvent e) {
  877. Throwable th = e.getException();
  878. String status = (th != null) ? "failed" : "succeeded";
  879. try {
  880. String key = "build." + status;
  881. if (props.getProperty(key + ".notify").equalsIgnoreCase("false")) {
  882. return;
  883. }
  884. Session session = Session.getDefaultInstance(props, null);
  885. MimeMessage message = new MimeMessage(session);
  886. message.addRecipients(Message.RecipientType.TO, parseAddresses(
  887. props.getProperty(key + ".email.to")));
  888. message.setSubject(props.getProperty(key + ".email.subject"));
  889. BufferedReader br = new BufferedReader(new FileReader(
  890. props.getProperty("build.log")));
  891. StringWriter sw = new StringWriter();
  892. String line = br.readLine();
  893. while (line != null) {
  894. sw.write(line);
  895. sw.write("\n");
  896. line = br.readLine();
  897. }
  898. br.close();
  899. message.setText(sw.toString(), "UTF-8");
  900. sw.close();
  901. Transport transport = session.getTransport();
  902. transport.connect();
  903. transport.send(message);
  904. transport.close();
  905. } catch (Exception ex) {
  906. System.out.println("BuildMonitor failed to send email!");
  907. ex.printStackTrace();
  908. }
  909. }
  910. /**
  911. * Parse a comma separated list of internet email addresses.
  912. *
  913. * @param s The list of addresses.
  914. * @return Array of Addresses.
  915. */
  916. protected Address[] parseAddresses(String s) throws Exception {
  917. StringTokenizer st = new StringTokenizer(s, ",");
  918. Address[] addrs = new Address[st.countTokens()];
  919. for (int i = 0; i < addrs.length; i++) {
  920. addrs[i] = new InternetAddress(st.nextToken());
  921. }
  922. return addrs;
  923. }
  924. public void messageLogged(BuildEvent e) {
  925. }
  926. public void targetStarted(BuildEvent e) {
  927. }
  928. public void targetFinished(BuildEvent e) {
  929. }
  930. public void taskStarted(BuildEvent e) {
  931. }
  932. public void taskFinished(BuildEvent e) {
  933. }
  934. }
  935. ]]></source>
  936. <p>With a <code>monitor.properties</code> like this:</p>
  937. <source><![CDATA[
  938. # configuration for build monitor
  939. mail.transport.protocol=smtp
  940. mail.smtp.host=<host>
  941. mail.from=Will Glozer <will.glozer@jda.com>
  942. build.log=build.log
  943. build.failed.notify=true
  944. build.failed.email.to=will.glozer@jda.com
  945. build.failed.email.subject=Nightly build failed!
  946. build.succeeded.notify=true
  947. build.succeeded.email.to=will.glozer@jda.com
  948. build.succeeded.email.subject=Nightly build succeeded!
  949. ]]></source>
  950. <p><code>monitor.properties</code> should be placed right next
  951. to your compiled <code>BuildMonitor.class</code>. To use it,
  952. invoke Ant like:</p>
  953. <source><![CDATA[
  954. ant -listener BuildMonitor -logfile build.log
  955. ]]></source>
  956. <p>Make sure that <code>mail.jar</code> from JavaMail and
  957. <code>activation.jar</code> from the
  958. <a href="http://java.sun.com/products/javabeans/glasgow/jaf.html">Java
  959. Beans Activation Framework</a> are in your <code>CLASSPATH</code>.</p>
  960. </answer>
  961. </faq>
  962. <faq id="listener-properties">
  963. <question>How do I get at the properties that Ant was running
  964. with from inside BuildListener?</question>
  965. <answer>
  966. <p>You can get at a hashtable with all the properties that Ant
  967. has been using through the BuildEvent parameter. For
  968. example:</p>
  969. <source><![CDATA[
  970. public void buildFinished(BuildEvent e) {
  971. Hashtable table = e.getProject().getProperties();
  972. String buildpath = (String)table.get("build.path");
  973. ...
  974. }
  975. ]]></source>
  976. <p>This is more accurate than just reading the same property
  977. files that your project does, since it will give the correct
  978. results for properties that were specified on the Ant command line.</p>
  979. </answer>
  980. </faq>
  981. </faqsection>
  982. <faqsection title="Known Problems">
  983. <faq id="remove-cr">
  984. <question>&lt;chmod&gt; or &lt;exec&gt; doesn&apos;t work in Ant
  985. 1.3 on Unix</question>
  986. <answer>
  987. <p>The <code>antRun</code> script in <code>ANT_HOME/bin</code>
  988. has DOS instead of Unix line endings; you must remove the
  989. carriage-return characters from this file. This can be done by
  990. using Ant&apos;s <code>&lt;fixcrlf&gt;</code> task
  991. or something like:</p>
  992. <source><![CDATA[
  993. tr -d '\r' < $ANT_HOME/bin/antRun > /tmp/foo
  994. mv /tmp/foo $ANT_HOME/bin/antRun
  995. ]]></source>
  996. </answer>
  997. </faq>
  998. <faq id="javadoc-cannot-execute">
  999. <question>JavaDoc failed: java.io.IOException: javadoc: cannot execute</question>
  1000. <answer>
  1001. <p>There is a bug in the Solaris reference implementation of
  1002. 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>).
  1003. This also appears to be true under Linux. Moving the JDK to
  1004. the front of the PATH fixes the problem.</p>
  1005. </answer>
  1006. </faq>
  1007. <faq id="delegating-classloader">
  1008. <question>&lt;style&gt; or &lt;junit&gt; ignores my
  1009. &lt;classpath&gt;</question>
  1010. <answer>
  1011. <p>These tasks don&apos;t ignore your classpath setting, you
  1012. are facing a common problem with delegating classloaders.</p>
  1013. <p>First of all let&apos;s state that Ant adds all
  1014. <code>.jar</code> files from <code>ANT_HOME/lib</code> to
  1015. <code>CLASSPATH</code>, therefore &quot;in
  1016. <code>CLASSPATH</code>&quot; shall mean &quot;either in your
  1017. <code>CLASSPATH</code> environment variable or
  1018. <code>ANT_HOME/lib</code>&quot; for the rest of this
  1019. answer.</p>
  1020. <p>Technically the sentence above isn&apos;t true for Ant 1.6
  1021. and later anymore, but the result is the same. For the sake
  1022. of this discussion, <code>CLASSPATH</code> and
  1023. <code>ANT_HOME/lib</code> are identical.</p>
  1024. <p>This question collects a common type of problem: A task
  1025. needs an external library and it has a nested classpath
  1026. element so that you can point it to this external library, but
  1027. that doesn&apos;t work unless you put the external library into the
  1028. <code>CLASSPATH</code>.</p>
  1029. <p>The root of the problem is that the class that needs the
  1030. external library is on the <code>CLASSPATH</code>.</p>
  1031. <p>When you specify a nested <code>&lt;classpath&gt;</code> in
  1032. Ant, Ant creates a new class loader that uses the path you
  1033. have specified. It then tries to load additional classes from
  1034. this classloader.</p>
  1035. <p>In most cases - for example the two cases above - Ant
  1036. doesn&apos;t load the external library directly, it is the loaded
  1037. class that does so.</p>
  1038. <p>In the case of <code>&lt;junit&gt;</code> it is the task
  1039. implementation itself and in the case of
  1040. <code>&lt;style&gt;</code> it is the implementation of the
  1041. <code>org.apache.tools.ant.taskdefs.XSLTLiaison</code>
  1042. class.</p>
  1043. <p>Ant&apos;s class loader implementation uses Java&apos;s
  1044. delegation model, see <a
  1045. 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>
  1046. the paragraph</p>
  1047. <blockquote>The <code>ClassLoader</code> class uses a
  1048. delegation model to search for classes and resources. Each
  1049. instance of <code>ClassLoader</code> has an associated parent
  1050. class loader. When called upon to find a class or resource, a
  1051. <code>ClassLoader</code> instance will delegate the search for
  1052. the class or resource to its parent class loader before
  1053. attempting to find the class or resource itself. The virtual
  1054. machine&apos;s built-in class loader, called the bootstrap
  1055. class loader, does not itself have a parent but may serve as
  1056. the parent of a <code>ClassLoader</code>
  1057. instance.</blockquote>
  1058. <p>This means, Ant&apos;s class loader will consult the
  1059. bootstrap class loader first, which tries to load classes from
  1060. <code>CLASSPATH</code>. The bootstrap class loader
  1061. doesn&apos;t know anything about Ant&apos;s class loader or
  1062. even the path you have specified.</p>
  1063. <p>If the bootstrap class loader can load the class Ant has
  1064. asked it to load, this class will try to load the external
  1065. library from <code>CLASSPATH</code> as well - it doesn&apos;t
  1066. know anything else - and will not find it unless the library
  1067. is in <code>CLASSPATH</code> as well.</p>
  1068. <p>To solve this, you have two major options:</p>
  1069. <ol>
  1070. <li>put all external libraries you need in
  1071. <code>CLASSPATH</code> as well this is not what you want,
  1072. otherwise you wouldn&apos;t have found this FAQ entry.</li>
  1073. <li>remove the class that loads the external library from
  1074. the <code>CLASSPATH</code>.</li>
  1075. </ol>
  1076. <p><strong>Using The Second Option with Ant 1.5.4 and
  1077. Earlier:</strong></p>
  1078. <p>The easiest way to do this is to remove
  1079. <code>optional.jar</code> from <code>ANT_HOME/lib</code>. If
  1080. you do so, you will have to <code>&lt;taskdef&gt;</code> all
  1081. optional tasks and use nested <code>&lt;classpath&gt;</code>
  1082. elements in the <code>&lt;taskdef&gt;</code> tasks that point
  1083. to the new location of <code>optional.jar</code>. Also,
  1084. don&apos;t forget to add the new location of
  1085. <code>optional.jar</code> to the
  1086. <code>&lt;classpath&gt;</code> of your
  1087. <code>&lt;style&gt;</code> or <code>&lt;junit&gt;</code>
  1088. task.</p>
  1089. <p>If you want to avoid to <code>&lt;taskdef&gt;</code> all
  1090. optional tasks you need, the only other option is to remove
  1091. the classes that should not be loaded via the bootstrap class
  1092. loader from <code>optional.jar</code> and put them into a
  1093. separate archive. Add this separate archive to the
  1094. <code>&lt;classpath&gt;</code> of your
  1095. <code>&lt;style&gt;</code> or <code>&lt;junit&gt;</code> task
  1096. - and make sure the separate archive is not in
  1097. <code>CLASSPATH</code>.</p>
  1098. <p>In the case of <code>&lt;junit&gt;</code> you&apos;d have
  1099. to remove all classes that are in the
  1100. <code>org/apache/tools/ant/taskdefs/optional/junit</code>
  1101. directory, in the <code>&lt;style&gt;</code> case it is one of
  1102. the <code>*Liaison</code> classes in
  1103. <code>org/apache/tools/ant/taskdefs/optional</code>.</p>
  1104. <p><strong>Using The Second Option with Ant 1.6 and
  1105. later:</strong></p>
  1106. <p>In Ant 1.6 <code>optional.jar</code> has been split into
  1107. multiple jars, each one containing classes with the same
  1108. dependencies on external libraries. You can move the
  1109. "offending" jar out of ANT_HOME/lib. For the
  1110. <code>&lt;junit&gt;</code> task it would be
  1111. <code>ant-junit.jar</code> and for <code>&lt;style&gt;</code>
  1112. it would be <code>ant-trax.jar</code>,
  1113. <code>ant-xalan1.jar</code> or <code>ant-xslp.jar</code> -
  1114. depending on the processor you use.</p>
  1115. <p>If you use the option to break up <code>optional.jar</code>
  1116. for <code>&lt;junit&gt;</code> or remove
  1117. <code>ant-junit.jar</code>, you still have to use a
  1118. <code>&lt;taskdef&gt;</code> with a nested
  1119. <code>&lt;classpath&gt;</code> to define the junit task.</p>
  1120. </answer>
  1121. </faq>
  1122. <faq id="winxp-jdk14-ant14">
  1123. <question>When running Ant 1.4 on Windows XP and JDK 1.4, I get
  1124. various errors when trying to <code>&lt;exec&gt;</code>, fork
  1125. <code>&lt;java&gt;</code> or access environment
  1126. variables.</question>
  1127. <answer>
  1128. <p>Ant &lt; 1.5 doesn&apos;t recognize Windows XP as a flavor
  1129. of Windows that runs <code>CMD.EXE</code> instead of
  1130. <code>COMMAND.COM</code>. JDK 1.3 will tell Ant that Windows
  1131. XP is Windows 2000 so the problem doesn&apos;t show up
  1132. there.</p>
  1133. <p>Apart from upgrading to Ant 1.5 or better, setting the
  1134. environment variable <code>ANT_OPTS</code> to
  1135. <code>-Dos.name=Windows_NT</code> prior to invoking Ant has
  1136. been confirmed as a workaround.</p>
  1137. </answer>
  1138. </faq>
  1139. <faq id="1.5-cygwin-sh">
  1140. <question>The <code>ant</code> wrapper script of Ant 1.5 fails
  1141. for Cygwin if <code>ANT_HOME</code> is set to a Windows style
  1142. path.</question>
  1143. <answer>
  1144. <p>This problem has been reported only hours after Ant 1.5 has
  1145. been released, see <a
  1146. href="http://issues.apache.org/bugzilla/show_bug.cgi?id=10664">Bug
  1147. 10664</a> and all its duplicates.</p>
  1148. <p>A fixed version of the wrapper script can be found <a
  1149. href="http://ant.apache.org/old-releases/v1.5/errata/">here</a>.
  1150. Simply replace your script with this version.</p>
  1151. </answer>
  1152. </faq>
  1153. <faq id="1.5.2-zip-broken">
  1154. <question><code>&lt;zip&gt;</code> is broken in Ant 1.5.2.</question>
  1155. <answer>
  1156. <p>Yes, it is.</p>
  1157. <p>The problem reported by most people - see <a
  1158. href="http://issues.apache.org/bugzilla/show_bug.cgi?id=17648">Bug
  1159. 17648</a> and all its duplicates - is that Ant creates
  1160. archives that a partially unreadable by WinZIP. Luckily
  1161. <code>jar</code> deals with the archives and so the generated
  1162. jars/wars/ears will most likely work for you anyway.</p>
  1163. <p>There are additional problems, see bugs <a
  1164. href="http://issues.apache.org/bugzilla/show_bug.cgi?id=17780">Bug
  1165. 17780</a>, <a
  1166. href="http://issues.apache.org/bugzilla/show_bug.cgi?id=17871">Bug
  1167. 17871</a> and <a
  1168. href="http://issues.apache.org/bugzilla/show_bug.cgi?id=18403">Bug
  1169. 18403</a>. All of them are supposed to be fixed with Ant
  1170. 1.5.3 (and only 18403 should exist in 1.5.3beta1).</p>
  1171. </answer>
  1172. </faq>
  1173. <faq id="unknownelement.taskcontainer">
  1174. <question>
  1175. Why do my custom task containers see Unknown Elements in Ant 1.6
  1176. - they worked in Ant 1.5?
  1177. </question>
  1178. <answer>
  1179. <p>
  1180. The objects added in TaskContainer.addTask(Task task)
  1181. have changed from Tasks to UnknownElements.
  1182. </p>
  1183. <p>
  1184. There was a number of valid reasons for this change. But the backward
  1185. compatibility problems were not noticed until after Ant 1.6.0 was
  1186. released.
  1187. </p>
  1188. <p>
  1189. Your container class will need to be modified to check if the Task
  1190. is an UnknownElement and call perform on it to
  1191. convert it to a Task and to execute it.
  1192. (see apache.tools.ant.taskdefs.Sequential)
  1193. </p>
  1194. <p>
  1195. If you want to do more processing on the task,
  1196. you need to use the techniques in apache.tools.ant.taskdefs.Antlib#execute()
  1197. This does make use of one 1.6 method call (UE#getRealObject()),
  1198. you need to use UE#getTask() instead - this will
  1199. return null for non tasks (types like fileset id=x).
  1200. </p>
  1201. <p>
  1202. So.. iterate over the tasks, if they are UEs, convert them to
  1203. tasks, using UE#maybeConfigure and UE#getTask()
  1204. </p>
  1205. <source><![CDATA[
  1206. for (Iterator i = tasks.iterator(); i.hasNext();) {
  1207. Task t = (Task) i.next();
  1208. if (t instanceof UnknownElement) {
  1209. ((UnknownElement) t).maybeConfigure();
  1210. t = ((UnknownElement) t).getTask();
  1211. if (t == null) {
  1212. continue;
  1213. }
  1214. }
  1215. // .... original Custom code
  1216. }
  1217. ]]></source>
  1218. <p>
  1219. This approach should work for ant1.5 and ant1.6.
  1220. </p>
  1221. </answer>
  1222. </faq>
  1223. <faq id="java.exception.stacktrace">
  1224. <question>
  1225. The program I run via &lt;java&gt; throws an exception but I
  1226. can't seem to get the full stack trace.
  1227. </question>
  1228. <answer>
  1229. <p>This is a know bug that has been fixed after the release of
  1230. Ant 1.6.1.</p>
  1231. <p>As a workaround, run your &lt;java&gt; task with
  1232. <code>fork="true"</code> and Ant will display the full
  1233. trace.</p>
  1234. </answer>
  1235. </faq>
  1236. <faq id="junit-no-runtime-xml">
  1237. <question>
  1238. Using format=&quot;xml&quot;, &lt;junit&gt; fails with a
  1239. <code>NoClassDefFoundError</code> if forked.
  1240. </question>
  1241. <answer>
  1242. <p>The XML formatter needs the <a
  1243. href="http://www.w3.org/DOM/">DOM classes</a> to work. If you
  1244. are using JDK 1.4 or later they are included with your Java
  1245. Runtime and this problem won't occur. If you are running JDK
  1246. 1.3 or earlier, the DOM classes have to be on your
  1247. &lt;junit&gt; task's &lt;classpath&gt;.</p>
  1248. <p>Prior to Ant 1.6.0 Ant would include the DOM classes from
  1249. the XML parser that is used by Ant itself if you set the
  1250. includeAntRuntime attribute to true (the default). With Ant
  1251. 1.6.0 this has been changed as this behavior made it
  1252. impossible to use a different XML parser in your tests.</p>
  1253. <p>This means that you have to take care of the DOM classes
  1254. explicitly starting with Ant 1.6.0. If you don't need to set
  1255. up a different XML parser for your tests, the easiest solution
  1256. is to add</p>
  1257. <source><![CDATA[
  1258. <pathelement path="${ant.home}/lib/xml-apis.jar:${ant.home}/lib/xercesImpl.jar"/>
  1259. ]]></source>
  1260. <p>to your task's &lt;classpath&gt;.</p>
  1261. </answer>
  1262. </faq>
  1263. <faq id="xalan-jdk1.5">
  1264. <question>
  1265. <code>&lt;junitreport&gt;</code> doesn't work with JDK 1.5 but
  1266. worked fine with JDK 1.4.
  1267. </question>
  1268. <answer>
  1269. <p>While JDK 1.4.x contains a version of Xalan-J 2, JDK 1.5
  1270. (and later?) have <a
  1271. href="http://java.sun.com/j2se/1.5.0/compatibility.html#4959783">moved
  1272. to XSLTC</a>. Since this task uses Xalan's redirect
  1273. extensions for its internal stylesheet, Ant doesn't support
  1274. XSLTC yet. This means that you have to install <a
  1275. href="http://xml.apache.org/xalan-j/">Xalan-J 2</a> in order
  1276. to use this task with JDK 1.5.</p>
  1277. <p>Starting with Ant 1.6.2 <code>&lt;junitreport&gt;</code>
  1278. supports JDK 1.5.</p>
  1279. </answer>
  1280. </faq>
  1281. </faqsection>
  1282. </document>