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

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