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

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