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

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