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 62 KiB

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