You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

faq.xml 29 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  1. <?xml version="1.0"?>
  2. <document>
  3. <properties>
  4. <author email="bodewig@apache.org">Stefan Bodewig</author>
  5. <title>Frequently Asked Questions</title>
  6. </properties>
  7. <faqsection title="About this FAQ">
  8. <faq id="latest-version">
  9. <question>Where do I find the latest version of this
  10. document?</question>
  11. <answer>
  12. <p>The latest version can always be found at Ant's homepage
  13. <a href="http://jakarta.apache.org/ant/faq.html">http://jakarta.apache.org/ant/faq.html</a>.</p>
  14. </answer>
  15. </faq>
  16. <faq id="adding-faqs">
  17. <question>How can I contribute to this FAQ?</question>
  18. <answer>
  19. <p>The page you are looking it is generated from
  20. <a href="http://cvs.apache.org/viewcvs.cgi/~checkout~/jakarta-ant/xdocs/faq.xml">this</a>
  21. document. If you want to add a new question, please submit
  22. a patch against this document to one of Ant's mailing lists,
  23. the structure is hoped to be self-explaining.</p>
  24. <p>If you don't know how to create a patch, see the patches
  25. section of <a href="http://jakarta.apache.org/site/source.html">this
  26. page</a>.</p>
  27. </answer>
  28. </faq>
  29. <faq id="creating-faq">
  30. <question>How do you create the HTML version of this
  31. FAQ?</question>
  32. <answer>
  33. <p>We use
  34. <a href="http://jakarta.apache.org/velocity/anakia.html">Anakia</a>
  35. to render the HTML version from the original XML file.</p>
  36. <p>The Velocity stylesheets used to process the XML files can
  37. be found in the <code>xdocs/stylesheets</code> subdirectory of
  38. Ant's CVS repository - the build file <code>docs.xml</code> is
  39. used to drive Anakia. This file assumes that you have the
  40. <code>jakarta-site2</code> module checked out from CVS as
  41. well, but if you follow the instruction from Anakia's
  42. homepage, you should get it to work without that. Just make
  43. sure all required jars are in the task's classpath.</p>
  44. </answer>
  45. </faq>
  46. </faqsection>
  47. <faqsection title="General">
  48. <faq id="what-is-ant">
  49. <question>What is Apache Ant?</question>
  50. <answer>
  51. <p> Ant is a Java based build tool. In theory it is kind of
  52. like &quot;make&quot; without makes wrinkles and with the full
  53. portability of pure Java code.</p>
  54. </answer>
  55. </faq>
  56. <faq id="ant-name">
  57. <question>Why do you call it Ant?</question>
  58. <answer>
  59. <p>According to Ant&apos;s original author James Duncan
  60. Davidson, the name is an acronym for &quot;Another Neat
  61. Tool&quot;.</p>
  62. <p>Later explanations go along the lines of &quot;Ants are
  63. doing an extremely good job at building things&quot; or
  64. &quot;Ants are very small and can carry a weight a dozen times
  65. of their own&quot; - describing what Ant is intended to
  66. be.</p>
  67. </answer>
  68. </faq>
  69. <faq id="history">
  70. <question>Tell us a little bit about Ant&apos;s history.</question>
  71. <answer>
  72. <p>Initially Ant was part of the Tomcat code base when it was
  73. donated to the Apache Software Foundation - it has been
  74. created by James Duncan Davidson, who also is the original
  75. author of Tomcat. Ant was there to build Tomcat, nothing
  76. else.</p>
  77. <p>Soon thereafter several open source Java projects realized
  78. that Ant could solve the problems they had with makefiles.
  79. Starting with the projects hosted at Jakarta and the old Java
  80. Apache project, Ant spread like a virus and now is the build
  81. tool of choice for a lot of projects.</p>
  82. <p>In January 2000 Ant was moved to a separate CVS module and
  83. was promoted to a project of its own, independent of
  84. Tomcat. Ant became Apache Ant.</p>
  85. <p>The first version of Ant that was exposed a lager audience
  86. was the one that shipped with Tomcat&apos;s 3.1 release on 19 April
  87. 2000. This version has later been referenced to as Ant
  88. 0.3.1.</p>
  89. <p>The first official release of Ant as a stand alone product was
  90. Ant 1.1 released on 19 July 2000. The complete release
  91. history:</p>
  92. <table>
  93. <tr>
  94. <th>Ant Version</th>
  95. <th>Release Date</th>
  96. </tr>
  97. <tr>
  98. <td>1.1</td>
  99. <td>19 July 2000</td>
  100. </tr>
  101. <tr>
  102. <td>1.2</td>
  103. <td>24 October 2000</td>
  104. </tr>
  105. <tr>
  106. <td>1.3</td>
  107. <td>3 March 2001</td>
  108. </tr>
  109. <tr>
  110. <td>1.4</td>
  111. <td>3 September 2001</td>
  112. </tr>
  113. <tr>
  114. <td>1.4.1</td>
  115. <td>11 October 2001</td>
  116. </tr>
  117. </table>
  118. </answer>
  119. </faq>
  120. </faqsection>
  121. <faqsection title="Installation">
  122. <faq id="no-gnu-tar">
  123. <question>I get checksum errors when I try to extract the
  124. <code>tar.gz</code> distribution file. Why?</question>
  125. <answer>
  126. <p>Ant&apos;s distribution contains file names that are longer
  127. than 100 characters, which is not supported by the standard
  128. tar file format. Several different implementations of tar use
  129. different and incompatible ways to work around this
  130. restriction.</p>
  131. <p>Ant&apos;s &lt;tar&gt; task can create tar archives that use
  132. the GNU tar extension, and this has been used when putting
  133. together the distribution. If you are using a different
  134. version of tar (for example, the one shipping with Solaris),
  135. you cannot use it to extract the archive.</p>
  136. <p>The solution is to either install GNU tar, which can be
  137. found <a href="http://www.gnu.org/software/tar/tar.html">here</a>
  138. or use the zip archive instead (you can extract it using
  139. <code>jar xf</code>).</p>
  140. </answer>
  141. </faq>
  142. </faqsection>
  143. <faqsection title="Using Ant">
  144. <faq id="always-recompiles">
  145. <question>Why does Ant always recompile all my Java files?</question>
  146. <answer>
  147. <p>In order to find out which files should be compiled, Ant
  148. compares the timestamps of the source files to those of the
  149. resulting <code>.class</code> files. Opening all source files
  150. to find out which package they belong to would be very
  151. inefficient - instead of this, Ant expects you to place your
  152. source files in a directory hierarchy that mirrors your
  153. package hierarchy and to point Ant to the root of this
  154. directory tree with the <code>srcdir</code> attribute.</p>
  155. <p>Say you have <code>&lt;javac srcdir=&quot;src&quot;
  156. destdir=&quot;dest&quot; /&gt;</code>. If Ant finds a file
  157. <code>src/a/b/C.java</code> it expects it to be in package
  158. <code>a.b</code> so that the resulting <code>.class</code>
  159. file is going to be <code>dest/a/b/C.class</code>.</p>
  160. <p>If your setup is different, Ant&apos;s heuristic won&apos;t work and
  161. it will recompile classes that are up to date. Ant is not the
  162. only tool, that expects a source tree layout like this.</p>
  163. </answer>
  164. </faq>
  165. <faq id="passing-cli-args">
  166. <question>How do I pass parameters from the command line to my
  167. build file?</question>
  168. <answer>
  169. <p>Use properties: <code>ant
  170. -D&lt;name&gt;=&lt;value&gt;</code> lets you define values for
  171. properties. These can then be used within your build file as
  172. any normal property: <code>${&lt;name&gt;}</code> will put in
  173. <code>&lt;value&gt;</code>.</p>
  174. </answer>
  175. </faq>
  176. <faq id="jikes-switches">
  177. <question>How can I use Jikes specific command line
  178. switches?</question>
  179. <answer>
  180. <p>A couple of switches are supported via magic
  181. properties:</p>
  182. <table>
  183. <tr>
  184. <th>switch</th>
  185. <th>property</th>
  186. <th>default</th>
  187. </tr>
  188. <tr>
  189. <td>+E</td>
  190. <td>build.compiler.emacs</td>
  191. <td>false == not set</td>
  192. </tr>
  193. <tr>
  194. <td>+P</td>
  195. <td>build.compiler.pedantic</td>
  196. <td>false == not set</td>
  197. </tr>
  198. <tr>
  199. <td>+F</td>
  200. <td>build.compiler.fulldepend</td>
  201. <td>false == not set</td>
  202. </tr>
  203. <tr>
  204. <td><strong>only for Ant &lt; 1.4, replaced by the nowarn
  205. attribute of javac after that</strong> -nowarn</td>
  206. <td>build.compiler.warnings</td>
  207. <td>true == not set</td>
  208. </tr>
  209. </table>
  210. </answer>
  211. </faq>
  212. <faq id="shell-redirect-1">
  213. <question>How do I include a &lt; character in my command line arguments?</question>
  214. <answer>
  215. <p>The short answer is "Use <code>&amp;lt;</code>".</p>
  216. <p>The long answer is, that this probably won't do what you
  217. want anyway, see <a href="#shell-redirect-2">the next
  218. section</a>.</p>
  219. </answer>
  220. </faq>
  221. <faq id="shell-redirect-2">
  222. <question>How do I redirect standard input or standard output
  223. in the <code>&lt;exec&gt;</code> task?</question>
  224. <answer>
  225. <p>Say you want to redirect the standard input stream of the
  226. <code>cat</code> command to read from a file, something
  227. like</p>
  228. <source><![CDATA[
  229. shell-prompt> cat < foo
  230. ]]></source>
  231. <p>and try to translate it into</p>
  232. <source><![CDATA[
  233. <exec executable="cat">
  234. <arg value="&lt;" />
  235. <arg value="foo" />
  236. </exec>
  237. ]]></source>
  238. <p>This will not do what you expect. The input-redirection is
  239. performed by your shell, not the command itself, so this
  240. should read:</p>
  241. <source><![CDATA[
  242. <exec executable="/bin/sh">
  243. <arg value="-c" />
  244. <arg value="cat &lt; foo" />
  245. </exec>
  246. ]]></source>
  247. <p>Note, that you must use the <code>value</code> attribute of
  248. <code>&lt;arg&gt;</code> in the last element.</p>
  249. </answer>
  250. </faq>
  251. <faq id="defaultexcludes">
  252. <question>I've made a &lt;delete&gt; task to delete unwanted
  253. sourcesafe control files (CVS files, editor backup files), but
  254. it doesn't seem to work. The files never get deleted. What's
  255. wrong?</question>
  256. <answer>
  257. <p>This is probably happening because by default, Ant excludes
  258. SourceSafe control files (<code>vssver.scc</code>) and other
  259. files from FileSets.</p>
  260. <p>Here's what you probably did:</p>
  261. <source><![CDATA[
  262. <delete>
  263. <fileset dir="${build.src}" includes="**/vssver.scc"/>
  264. </delete>
  265. ]]></source>
  266. <p>You need to switch off the default exclusions and it will work:</p>
  267. <source><![CDATA[
  268. <delete>
  269. <fileset dir="${build.src}" includes="**/vssver.scc"
  270. defaultexcludes="no"/>
  271. </delete>
  272. ]]></source>
  273. <p>For a complete listing of the patterns that are excluded
  274. by default, see <a href="manual/dirtasks.html">the user
  275. manual</a>.</p>
  276. </answer>
  277. </faq>
  278. <faq id="multi-conditions">
  279. <question>I want to execute a particular target only if
  280. multiple conditions are true.</question>
  281. <answer>
  282. <p>There are actually several answers to this question.</p>
  283. <p>If you have only one set and one unset property to test,
  284. you can put both an <code>if</code> and an <code>unless</code>
  285. attribute into the target. The target will act as if they
  286. are &quot;anded&quot; together.</p>
  287. <p>If you are using a version of Ant 1.3 or earlier, the
  288. way to work with all other cases is to chain targets together
  289. to determine the specific state you wish to test for.</p>
  290. <p>To see how this works, assume you have three properties,
  291. <code>prop1</code>, <code>prop2</code>, and <code>prop3</code>.
  292. You want to test that <code>prop1</code> and <code>prop2</code>
  293. are set, but that <code>prop3</code> is not. If the condition
  294. holds true you want to echo &quot;yes&quot;.</p>
  295. <p>Here is the implementation in Ant 1.3 and earlier:</p>
  296. <source><![CDATA[
  297. <target name="cond" depends="cond-if"/>
  298. <target name="cond-if" if="prop1">
  299. <antcall target="cond-if-2"/>
  300. </target>
  301. <target name="cond-if-2" if="prop2">
  302. <antcall target="cond-if-3"/>
  303. </target>
  304. <target name="cond-if-3" unless="prop3">
  305. <echo message="yes"/>
  306. </target>
  307. ]]></source>
  308. <p>Note that <code>&lt;antcall&gt;</code> tasks do not pass
  309. property changes back up to the environment they were called
  310. from.</p>
  311. <p>Starting with Ant 1.4, you can use the
  312. <code>&lt;condition&gt;</code> task.</p>
  313. <source><![CDATA[
  314. <target name="cond" depends="cond-if,cond-else"/>
  315. <target name="check-cond">
  316. <condition property="cond-is-true">
  317. <and>
  318. <not>
  319. <equals arg1="${prop1}" arg2="$${prop1}" />
  320. </not>
  321. <not>
  322. <equals arg1="${prop2}" arg2="$${prop2}" />
  323. </not>
  324. <equals arg1="${prop3}" arg2="$${prop3}" />
  325. </and>
  326. </condition>
  327. </target>
  328. <target name="cond-if" depends="check-cond" if="cond-is-true">
  329. <echo message="yes"/>
  330. </target>
  331. <target name="cond-else" depends="check-cond" unless="cond-is-true">
  332. <echo message="no"/>
  333. </target>
  334. ]]></source>
  335. <p>This version takes advantage of two things:</p>
  336. <ul>
  337. <li>If a property <code>a</code> has not been set,
  338. <code>${a}</code> will evaluate to <code>${a}</code>.</li>
  339. <li>To get a literal <code>$</code> in Ant, you have to
  340. escape it with another <code>$</code> - this will also break
  341. the special treatment of the sequence <code>${</code>.</li>
  342. </ul>
  343. <p>This is neither readable, nor easy to understand, therefore
  344. post-1.4.1 Ant introduces the <code>&lt;isset&gt;</code> element
  345. to the <code>&lt;condition&gt;</code> task.</p>
  346. <p>Here is the previous example done using
  347. <code>&lt;isset&gt;</code>:</p>
  348. <source><![CDATA[
  349. <target name="check-cond">
  350. <condition property="cond-is-true">
  351. <and>
  352. <isset property="prop1"/>
  353. <isset property="prop2"/>
  354. <not>
  355. <isset property="prop3"/>
  356. </not>
  357. </and>
  358. </condition>
  359. </target>
  360. ]]></source>
  361. <p>The last option is to use a scripting language to set the
  362. properties. This can be particularly handy when you need much
  363. better control than the simple conditions shown here, but of
  364. course comes with the overhead of adding JAR files to support
  365. the language, to say nothing of the added maintenance in requiring
  366. two languages to implement a single system.</p>
  367. </answer>
  368. </faq>
  369. <faq id="stop-dependency">
  370. <question>I have a target I want to skip if a variable is set,
  371. so I have <code>unless=&quot;variable&quot;</code> as an attribute
  372. of the target. The trouble is that all of the targets that this target
  373. depends on are still executed. Why?</question>
  374. <answer>
  375. <p>The list of dependencies is generated by Ant before any of the
  376. targets are run. This allows dependent targets such as an
  377. <code>init</code> target to set properties that can control the
  378. execution of the targets higher in the dependency graph. This
  379. is a good thing.</p>
  380. <p>When your dependencies actually break down the higher level task
  381. into several simpler steps, though, this behaviour becomes
  382. counterintuitive. There are a couple of solutions available:
  383. </p>
  384. <ol>
  385. <li>Put the same condition on each of the dependent targets.</li>
  386. <li>Execute the steps using <code>&lt;antcall&gt;</code>
  387. instead of specifying them inside the <code>depends</code>
  388. attribute.</li>
  389. </ol>
  390. </answer>
  391. </faq>
  392. <faq id="include-order">
  393. <question>In my fileset, I've put in an
  394. <code>&lt;exclude&gt;</code> of all files followed by an
  395. <code>&lt;include&gt;</code> of just the files I want, but it
  396. isn't giving me anything at all. What's wrong?
  397. </question>
  398. <answer>
  399. <p>The order of the <code>&lt;include&gt;</code> and
  400. <code>&lt;exclude&gt;</code> tags within a fileset is ignored
  401. when the fileset is created. Instead, all of the
  402. <code>&lt;include&gt;</code> elements are processed together,
  403. followed by all of the <code>&lt;exclude&gt;</code>
  404. elements. This means that the <code>&lt;exclude&gt;</code>
  405. elements only apply to the file list produced by the
  406. <code>&lt;include&gt;</code> elements.</p>
  407. <p>To get the files you want, focus on just the
  408. <code>&lt;include&gt;</code> patterns that would be necessary
  409. to get them. If you need to trim the list that the includes
  410. would produce, use excludes.</p>
  411. </answer>
  412. </faq>
  413. </faqsection>
  414. <faqsection title="Ant and IDEs/Editors">
  415. <faq id="integration">
  416. <question>Is Ant supported by my IDE/Editor?</question>
  417. <answer>
  418. <p>See the <a href="external.html#IDE and Editor Integration">section
  419. on IDE integration</a> on our external tools page.</p>
  420. </answer>
  421. </faq>
  422. <faq id="emacs-mode">
  423. <question>Why doesn&apos;t (X)Emacs/vi/MacOS X's project builder
  424. parse the error messages generated by Ant correctly?</question>
  425. <answer>
  426. <p>Ant adds a &quot;banner&quot; with the name of the current
  427. task in front of all messages - and there are no built-in
  428. regular expressions in your Editor that would account for
  429. this.</p>
  430. <p>You can disable this banner by invoking Ant with the
  431. <code>-emacs</code> switch. Alternatively you can add the
  432. following snippet to your <code>.emacs</code> to make Emacs
  433. understand Ant&apos;s output.</p>
  434. <source><![CDATA[
  435. (require 'compile)
  436. (setq compilation-error-regexp-alist
  437. (append (list
  438. ;; works for jikes
  439. '("^\\s-*\\[[^]]*\\]\\s-*\\(.+\\):\\([0-9]+\\):\\([0-9]+\\):[0-9]+:[0-9]+:" 1 2 3)
  440. ;; works for javac
  441. '("^\\s-*\\[[^]]*\\]\\s-*\\(.+\\):\\([0-9]+\\):" 1 2))
  442. compilation-error-regexp-alist))
  443. ]]></source>
  444. <p>Yet another alternative that preserves most of Ant's
  445. formatting is to pipe Ant's output through the following Perl
  446. script by Dirk-Willem van Gulik:</p>
  447. <source><![CDATA[
  448. #!/usr/bin/perl
  449. #
  450. # May 2001 dirkx@apache.org - remove any
  451. # [foo] lines from the output; keeping
  452. # spacing more or less there.
  453. #
  454. $|=1;
  455. while(<STDIN>) {
  456. if (s/^(\s+)\[(\w+)\]//) {
  457. if ($2 ne $last) {
  458. print "$1\[$2\]";
  459. $s = ' ' x length($2);
  460. } else {
  461. print "$1 $s ";
  462. };
  463. $last = $2;
  464. };
  465. print;
  466. };
  467. ]]></source>
  468. </answer>
  469. </faq>
  470. </faqsection>
  471. <faqsection title="Advanced issues">
  472. <faq id="dtd">
  473. <question>Is there a DTD that I can use to validate my build
  474. files?</question>
  475. <answer>
  476. <p>An incomplete DTD can be created by the
  477. <code>&lt;antstructure&gt;</code> task - but this one
  478. has a few problems:</p>
  479. <ul>
  480. <li>It doesn&apos;t know about required attributes. Only
  481. manual tweaking of this file can help here.</li>
  482. <li>It is not complete - if you add new tasks via
  483. <code>&lt;taskdef&gt;</code> it won&apos;t know about it. See
  484. <a href="http://www.sdv.fr/pages/casa/html/ant-dtd.en.html">this
  485. page</a> by Michel Casabianca for a solution to this
  486. problem. Note that the DTD you can download at this page
  487. is based on Ant 0.3.1.</li>
  488. <li>It may even be an invalid DTD. As Ant allows tasks
  489. writers to define arbitrary elements, name collisions will
  490. happen quite frequently - if your version of Ant contains
  491. the optional <code>&lt;test&gt;</code> and
  492. <code>&lt;junit&gt;</code> tasks, there are two XML
  493. elements named test (the task and the nested child element
  494. of <code>&lt;junit&gt;</code>) with different attribute
  495. lists. This problem cannot be solved, DTDs don&apos;t give a
  496. syntax rich enough to support this.</li>
  497. </ul>
  498. </answer>
  499. </faq>
  500. <faq id="xml-entity-include">
  501. <question>How do I include an XML snippet in my build file?</question>
  502. <answer>
  503. <p>You can use XML&apos;s way of including external files and let
  504. the parser do the job for Ant:</p>
  505. <source><![CDATA[
  506. <?xml version="1.0"?>
  507. <!DOCTYPE project [
  508. <!ENTITY common SYSTEM "file:./common.xml">
  509. ]>
  510. <project name="test" default="test" basedir=".">
  511. <target name="setup">
  512. ...
  513. </target>
  514. &common;
  515. ...
  516. </project>
  517. ]]></source>
  518. <p>will literally include the contents of <code>common.xml</code> where
  519. you&apos;ve placed the <code>&amp;common;</code> entity.</p>
  520. <p>In combination with a DTD, this would look like this:</p>
  521. <source><![CDATA[
  522. <!DOCTYPE project PUBLIC "-//ANT//DTD project//EN" "file:./ant.dtd" [
  523. <!ENTITY include SYSTEM "file:./header.xml">
  524. ]>
  525. ]]></source>
  526. </answer>
  527. </faq>
  528. <faq id="mail-logger">
  529. <question>How do I send an email with the result of my build
  530. process?</question>
  531. <answer>
  532. <p>If you are using a nightly-build of Ant 1.5 after
  533. 2001-12-14, you can use the built-in MailLogger.</p>
  534. <source><![CDATA[
  535. ant -logger org.apache.tools.ant.listener.MailLogger
  536. ]]></source>
  537. <p>See the <a href="http://cvs.apache.org/viewcvs/~checkout~/jakarta-ant/docs/manual/listeners.html?content-type=text/html">Listener
  538. &amp; Logger documentation</a> for details on the properties
  539. required.</p>
  540. <p>For older versions of Ant you can use a custom
  541. BuildListener, that sends out an email
  542. in the buildFinished() method. Will Glozer
  543. &lt;will.glozer@jda.com&gt; has written such a listener based
  544. on <a href="http://java.sun.com/products/javamail/">JavaMail</a>,
  545. the source is</p>
  546. <source><![CDATA[
  547. import java.io.*;
  548. import java.util.*;
  549. import javax.mail.*;
  550. import javax.mail.internet.*;
  551. import org.apache.tools.ant.*;
  552. /**
  553. * A simple listener that waits for a build to finish and sends an email
  554. * of the results. The settings are stored in "monitor.properties" and
  555. * are fairly self explanatory.
  556. *
  557. * @author Will Glozer
  558. * @version 1.05a 09/06/2000
  559. */
  560. public class BuildMonitor implements BuildListener {
  561. protected Properties props;
  562. /**
  563. * Create a new BuildMonitor.
  564. */
  565. public BuildMonitor() throws Exception {
  566. props = new Properties();
  567. InputStream is = getClass().getResourceAsStream("monitor.properties");
  568. props.load(is);
  569. is.close();
  570. }
  571. public void buildStarted(BuildEvent e) {
  572. }
  573. /**
  574. * Determine the status of the build and the actions to follow, now that
  575. * the build has completed.
  576. *
  577. * @param e Event describing the build tatus.
  578. */
  579. public void buildFinished(BuildEvent e) {
  580. Throwable th = e.getException();
  581. String status = (th != null) ? "failed" : "succeeded";
  582. try {
  583. String key = "build." + status;
  584. if (props.getProperty(key + ".notify").equalsIgnoreCase("false")) {
  585. return;
  586. }
  587. Session session = Session.getDefaultInstance(props, null);
  588. MimeMessage message = new MimeMessage(session);
  589. message.addRecipients(Message.RecipientType.TO, parseAddresses(
  590. props.getProperty(key + ".email.to")));
  591. message.setSubject(props.getProperty(key + ".email.subject"));
  592. BufferedReader br = new BufferedReader(new FileReader(
  593. props.getProperty("build.log")));
  594. StringWriter sw = new StringWriter();
  595. String line = br.readLine();
  596. while (line != null) {
  597. sw.write(line);
  598. sw.write("\n");
  599. line = br.readLine();
  600. }
  601. br.close();
  602. message.setText(sw.toString(), "UTF-8");
  603. sw.close();
  604. Transport transport = session.getTransport();
  605. transport.connect();
  606. transport.send(message);
  607. transport.close();
  608. } catch (Exception ex) {
  609. System.out.println("BuildMonitor failed to send email!");
  610. ex.printStackTrace();
  611. }
  612. }
  613. /**
  614. * Parse a comma separated list of internet email addresses.
  615. *
  616. * @param s The list of addresses.
  617. * @return Array of Addresses.
  618. */
  619. protected Address[] parseAddresses(String s) throws Exception {
  620. StringTokenizer st = new StringTokenizer(s, ",");
  621. Address[] addrs = new Address[st.countTokens()];
  622. for (int i = 0; i < addrs.length; i++) {
  623. addrs[i] = new InternetAddress(st.nextToken());
  624. }
  625. return addrs;
  626. }
  627. public void messageLogged(BuildEvent e) {
  628. }
  629. public void targetStarted(BuildEvent e) {
  630. }
  631. public void targetFinished(BuildEvent e) {
  632. }
  633. public void taskStarted(BuildEvent e) {
  634. }
  635. public void taskFinished(BuildEvent e) {
  636. }
  637. }
  638. ]]></source>
  639. <p>With a <code>monitor.properties</code> like this</p>
  640. <source><![CDATA[
  641. # configuration for build monitor
  642. mail.transport.protocol=smtp
  643. mail.smtp.host=<host>
  644. mail.from=Will Glozer <will.glozer@jda.com>
  645. build.log=build.log
  646. build.failed.notify=true
  647. build.failed.email.to=will.glozer@jda.com
  648. build.failed.email.subject=Nightly build failed!
  649. build.succeeded.notify=true
  650. build.succeeded.email.to=will.glozer@jda.com
  651. build.succeeded.email.subject=Nightly build succeeded!
  652. ]]></source>
  653. <p><code>monitor.properties</code> should be placed right next
  654. to your compiled <code>BuildMonitor.class</code>. To use it,
  655. invoke Ant like</p>
  656. <source><![CDATA[
  657. ant -listener BuildMonitor -logfile build.log
  658. ]]></source>
  659. <p>Make sure that <code>mail.jar</code> from JavaMail and
  660. <code>activation.jar</code> from the
  661. <a href="http://java.sun.com/products/javabeans/glasgow/jaf.html">Java
  662. Beans Activation Framework</a> in your <code>CLASSPATH</code>.</p>
  663. </answer>
  664. </faq>
  665. <faq id="listener-properties">
  666. <question>How do I get at the properties that Ant was running
  667. with from inside BuildListener?</question>
  668. <answer>
  669. <p>You can get at a hashtable with all the properties that Ant
  670. has been using through the BuildEvent parameter. For
  671. example:</p>
  672. <source><![CDATA[
  673. public void buildFinished(BuildEvent e) {
  674. Hashtable table = e.getProject().getProperties();
  675. String buildpath = (String)table.get("build.path");
  676. ...
  677. }
  678. ]]></source>
  679. <p>This is more accurate than just reading the same property
  680. files that your project does, since it will give the correct
  681. results for properties that are specified on the command line
  682. when running Ant.</p>
  683. </answer>
  684. </faq>
  685. </faqsection>
  686. <faqsection title="Known problems">
  687. <faq id="remove-cr">
  688. <question>&lt;chmod&gt; or &lt;exec&gt; don&apos;t work in Ant
  689. 1.3 on Unix</question>
  690. <answer>
  691. <p>The <code>antRun</code> script in <code>ANT_HOME/bin</code>
  692. has DOS instead of Unix line endings, you must remove the
  693. carriage return characters from this file. This can be done by
  694. using Ant&apos;s &lt;fixcrlf&gt; task or something like:</p>
  695. <source><![CDATA[
  696. tr -d '\r' < $ANT_HOME/bin/antRun > /tmp/foo
  697. mv /tmp/foo $ANT_HOME/bin/antRun
  698. ]]></source>
  699. </answer>
  700. </faq>
  701. <faq id="javadoc-cannot-execute">
  702. <question>JavaDoc failed: java.io.IOException: javadoc: cannot execute</question>
  703. <answer>
  704. <p>There is a bug in the Solaris reference implementation of
  705. 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>.
  706. This also appears to be true under Linux, moving the JDK to
  707. the front of the PATH fixes the problem.</p>
  708. </answer>
  709. </faq>
  710. </faqsection>
  711. </document>