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.html 86 KiB

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