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

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