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 31 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  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&apos;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&apos;s mailing lists;
  23. hopefully, the structure is self-explanatory.</p>
  24. <p>If you don&apos;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&apos;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&apos;s
  42. homepage, you should get it to work without that. Just make
  43. sure all required jars are in the task&apos;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 Make, without Make&apos;s 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
  63. do an extremely good job at building things&quot;, or
  64. &quot;ants are very small and can carry a weight dozens of times
  65. 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 was
  74. created by James Duncan Davidson, who is also 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 is now 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, and became Apache Ant.</p>
  85. <p>The first version of Ant that was exposed to a larger audience
  86. was the one that shipped with Tomcat&apos;s 3.1 release on 19 April
  87. 2000. This version has later been referred 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, 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 source-tree directory structure does not match your
  161. package structure, Ant&apos;s heuristic won&apos;t work, and
  162. it will recompile classes that are up-to-date. Ant is not the
  163. only tool that expects a source-tree layout like this.</p>
  164. <p>If you have Java source files that aren&apos;t declared to
  165. be part of any package, you can still use the <code>&lt;javac&gt;</code>
  166. task to compile these files correctly - just set the
  167. <code>srcdir</code> and <code>destdir</code> attributes to
  168. the actual directory the source
  169. files live in and the directory the class files should go into,
  170. respectively.</p>
  171. </answer>
  172. </faq>
  173. <faq id="passing-cli-args">
  174. <question>How do I pass parameters from the command line to my
  175. build file?</question>
  176. <answer>
  177. <p>Use properties. Using <code>ant
  178. -D<em>name</em>=<em>value</em></code> lets you define values for
  179. properties on the Ant command line. These properties can then be
  180. used within your build file as
  181. any normal property: <code>${<em>name</em>}</code> will put in
  182. <code><em>value</em></code>.</p>
  183. </answer>
  184. </faq>
  185. <faq id="jikes-switches">
  186. <question>How can I use Jikes-specific command-line
  187. switches?</question>
  188. <answer>
  189. <p>A couple of switches are supported via &quot;magic&quot;
  190. properties:</p>
  191. <table>
  192. <tr>
  193. <th>switch</th>
  194. <th>property</th>
  195. <th>default</th>
  196. </tr>
  197. <tr>
  198. <td>+E</td>
  199. <td>build.compiler.emacs</td>
  200. <td>false == not set</td>
  201. </tr>
  202. <tr>
  203. <td>+P</td>
  204. <td>build.compiler.pedantic</td>
  205. <td>false == not set</td>
  206. </tr>
  207. <tr>
  208. <td>+F</td>
  209. <td>build.compiler.fulldepend</td>
  210. <td>false == not set</td>
  211. </tr>
  212. <tr>
  213. <td><strong>(Only for Ant &lt; 1.4; replaced by the
  214. <code><strong>nowarn</strong></code>
  215. attribute of the <code><strong>&lt;javac&gt;</strong></code>
  216. task after that.)</strong><br></br>-nowarn</td>
  217. <td>build.compiler.warnings</td>
  218. <td>true == not set</td>
  219. </tr>
  220. </table>
  221. </answer>
  222. </faq>
  223. <faq id="shell-redirect-1">
  224. <question>How do I include a &lt; character in my command-line arguments?</question>
  225. <answer>
  226. <p>The short answer is "Use: <code>&amp;lt;</code>".</p>
  227. <p>The long answer is that this probably won&apos;t do what you
  228. want anyway (see <a href="#shell-redirect-2">the next
  229. section</a>).</p>
  230. </answer>
  231. </faq>
  232. <faq id="shell-redirect-2">
  233. <question>How do I redirect standard input or standard output
  234. in the <code>&lt;exec&gt;</code> task?</question>
  235. <answer>
  236. <p>Say you want to redirect the standard input stream of the
  237. <code>cat</code> command to read from a file, something
  238. like:</p>
  239. <source><![CDATA[
  240. shell-prompt> cat < foo
  241. ]]></source>
  242. <p>and try to translate it into</p>
  243. <source><![CDATA[
  244. <exec executable="cat">
  245. <arg value="&lt;" />
  246. <arg value="foo" />
  247. </exec>
  248. ]]></source>
  249. <p>This will not do what you expect. The input redirection is
  250. performed by your shell, not the command itself, so this
  251. should read:</p>
  252. <source><![CDATA[
  253. <exec executable="/bin/sh">
  254. <arg value="-c" />
  255. <arg value="cat &lt; foo" />
  256. </exec>
  257. ]]></source>
  258. <p>Note that you must use the <code>value</code> attribute of
  259. <code>&lt;arg&gt;</code> in the last element, in order to have
  260. the command passed as a single, quoted argument. Alternatively,
  261. you can use:</p>
  262. <source><![CDATA[
  263. <exec executable="/bin/sh">
  264. <arg line='-c "cat &lt; foo"'/>
  265. </exec>
  266. ]]></source>
  267. <p>Note the double-quotes nested inside the single-quotes.</p>
  268. </answer>
  269. </faq>
  270. <faq id="batch-shell-execute">
  271. <question>How do I execute a batch file or shell script from Ant?</question>
  272. <answer>
  273. <p>Execute the command shell instead, then pass the batch file or
  274. shell script as a single command, using the <code>/c</code> or
  275. <code>-c</code> switch, respectively. See
  276. <a href="#shell-redirect-2">the above section</a>
  277. for example <code>&lt;exec&gt;</code> tasks
  278. executing <code>sh</code>. On Windows, use something like:</p>
  279. <source><![CDATA[
  280. <exec dir="." executable="cmd.exe" os="Windows NT">
  281. <arg line="/c test.bat"/>
  282. </exec>
  283. ]]></source>
  284. </answer>
  285. </faq>
  286. <faq id="defaultexcludes">
  287. <question>I&apos;ve used a <code>&lt;delete&gt;</code> task to delete
  288. unwanted
  289. SourceSafe control files (CVS files, editor backup files, etc.), but
  290. it doesn&apos;t seem to work; the files never get deleted. What&apos;s
  291. wrong?</question>
  292. <answer>
  293. <p>This is probably happening because, by default, Ant excludes
  294. SourceSafe control files (<code>vssver.scc</code>) and certain other
  295. files from FileSets.</p>
  296. <p>Here&apos;s what you probably did:</p>
  297. <source><![CDATA[
  298. <delete>
  299. <fileset dir="${build.src}" includes="**/vssver.scc"/>
  300. </delete>
  301. ]]></source>
  302. <p>You need to switch off the default exclusions,
  303. and it will work:</p>
  304. <source><![CDATA[
  305. <delete>
  306. <fileset dir="${build.src}" includes="**/vssver.scc"
  307. defaultexcludes="no"/>
  308. </delete>
  309. ]]></source>
  310. <p>For a complete listing of the patterns that are excluded
  311. by default, see <a href="manual/dirtasks.html#defaultexcludes">the user
  312. manual</a>.</p>
  313. </answer>
  314. </faq>
  315. <faq id="multi-conditions">
  316. <question>I want to execute a particular target only if
  317. multiple conditions are true.</question>
  318. <answer>
  319. <p>There are actually several answers to this question.</p>
  320. <p>If you have only one set and one unset property to test,
  321. you can specify both an <code>if</code> and an <code>unless</code>
  322. attribute for the target, and they will act as if they
  323. are &quot;anded&quot; together.</p>
  324. <p>If you are using a version of Ant 1.3 or earlier, the
  325. way to work with all other cases is to chain targets together
  326. to determine the specific state you want to test for.</p>
  327. <p>To see how this works, assume you have three properties:
  328. <code>prop1</code>, <code>prop2</code>, and <code>prop3</code>.
  329. You want to test that <code>prop1</code> and <code>prop2</code>
  330. are set, and that <code>prop3</code> is not. If the condition
  331. holds true you want to echo &quot;yes&quot;.</p>
  332. <p>Here is the implementation in Ant 1.3 and earlier:</p>
  333. <source><![CDATA[
  334. <target name="cond" depends="cond-if"/>
  335. <target name="cond-if" if="prop1">
  336. <antcall target="cond-if-2"/>
  337. </target>
  338. <target name="cond-if-2" if="prop2">
  339. <antcall target="cond-if-3"/>
  340. </target>
  341. <target name="cond-if-3" unless="prop3">
  342. <echo message="yes"/>
  343. </target>
  344. ]]></source>
  345. <p>Note: <code>&lt;antcall&gt;</code> tasks do <em>not</em> pass
  346. property changes back up to the environment they were called
  347. from, so you would&apos;nt be able to, for example, set a
  348. <code>result</code> property in the <code>cond-if-3</code> target,
  349. then do
  350. <code>&lt;echo message=&quot;result is ${result}&quot;/&gt;</code>
  351. in the <code>cond</code> target.</p>
  352. <p>Starting with Ant 1.4, you can use the
  353. <code>&lt;condition&gt;</code> task.</p>
  354. <source><![CDATA[
  355. <target name="cond" depends="cond-if,cond-else"/>
  356. <target name="check-cond">
  357. <condition property="cond-is-true">
  358. <and>
  359. <not>
  360. <equals arg1="${prop1}" arg2="$${prop1}" />
  361. </not>
  362. <not>
  363. <equals arg1="${prop2}" arg2="$${prop2}" />
  364. </not>
  365. <equals arg1="${prop3}" arg2="$${prop3}" />
  366. </and>
  367. </condition>
  368. </target>
  369. <target name="cond-if" depends="check-cond" if="cond-is-true">
  370. <echo message="yes"/>
  371. </target>
  372. <target name="cond-else" depends="check-cond" unless="cond-is-true">
  373. <echo message="no"/>
  374. </target>
  375. ]]></source>
  376. <p>This version takes advantage of two things:</p>
  377. <ul>
  378. <li>If a property <code>a</code> has not been set,
  379. <code>${a}</code> will evaluate to <code>${a}</code>.</li>
  380. <li>To get a literal <code>$</code> in Ant, you have to
  381. escape it with another <code>$</code> - this will also break
  382. the special treatment of the <code>${</code> sequence.</li>
  383. </ul>
  384. <p>Because testing for a literal <code>${property}</code> string
  385. isn&apos;t all that readable or easy to understand,
  386. post-1.4.1 Ant introduces the <code>&lt;isset&gt;</code> element
  387. to the <code>&lt;condition&gt;</code> task.</p>
  388. <p>Here is the previous example done using
  389. <code>&lt;isset&gt;</code>:</p>
  390. <source><![CDATA[
  391. <target name="check-cond">
  392. <condition property="cond-is-true">
  393. <and>
  394. <isset property="prop1"/>
  395. <isset property="prop2"/>
  396. <not>
  397. <isset property="prop3"/>
  398. </not>
  399. </and>
  400. </condition>
  401. </target>
  402. ]]></source>
  403. <p>The last option is to use a scripting language to set the
  404. properties. This can be particularly handy when you need much
  405. finer control than the simple conditions shown here but, of
  406. course, comes with the overhead of adding JAR files to support
  407. the language, to say nothing of the added maintenance in requiring
  408. two languages to implement a single system. See the
  409. <a href="manual/OptionalTasks/script.html">
  410. <code>&lt;script&gt;</code> task documentation</a> for more
  411. details.</p>
  412. </answer>
  413. </faq>
  414. <faq id="stop-dependency">
  415. <question>I have a target I want to skip if a property is set,
  416. so I have <code>unless=&quot;property&quot;</code> as an attribute
  417. of the target, but all the targets this target
  418. depends on are still executed. Why?</question>
  419. <answer>
  420. <p>The list of dependencies is generated by Ant before any of the
  421. targets are run. This allows dependent targets, such as an
  422. <code>init</code> target, to set properties that can control the
  423. execution of the targets higher in the dependency graph. This
  424. is a good thing.</p>
  425. <p>However, when your dependencies break down the
  426. higher-level task
  427. into several smaller steps, this behaviour becomes
  428. counter-intuitive. There are a couple of solutions available:
  429. </p>
  430. <ol>
  431. <li>Put the same condition on each of the dependent targets.</li>
  432. <li>Execute the steps using <code>&lt;antcall&gt;</code>,
  433. instead of specifying them inside the <code>depends</code>
  434. attribute.</li>
  435. </ol>
  436. </answer>
  437. </faq>
  438. <faq id="include-order">
  439. <question>In my <code>&lt;fileset&gt;</code>, I&apos;ve put in an
  440. <code>&lt;exclude&gt;</code> of all files followed by an
  441. <code>&lt;include&gt;</code> of just the files I want, but it
  442. isn&apos;t giving me any files at all. What&apos;s wrong?
  443. </question>
  444. <answer>
  445. <p>The order of the <code>&lt;include&gt;</code> and
  446. <code>&lt;exclude&gt;</code> tags within a <code>&lt;fileset&gt;</code>
  447. is ignored when the FileSet is created. Instead, all of the
  448. <code>&lt;include&gt;</code> elements are processed together,
  449. followed by all of the <code>&lt;exclude&gt;</code>
  450. elements. This means that the <code>&lt;exclude&gt;</code>
  451. elements only apply to the file list produced by the
  452. <code>&lt;include&gt;</code> elements.</p>
  453. <p>To get the files you want, focus on just the
  454. <code>&lt;include&gt;</code> patterns that would be necessary
  455. to get them. If you find you need to trim the list that the
  456. <code>&lt;include&gt;</code> elements
  457. produce, then use <code>&lt;exclude&gt;</code> elements.</p>
  458. </answer>
  459. </faq>
  460. </faqsection>
  461. <faqsection title="Ant and IDEs/Editors">
  462. <faq id="integration">
  463. <question>Is Ant supported by my IDE/Editor?</question>
  464. <answer>
  465. <p>See the <a href="external.html#IDE and Editor Integration">section
  466. on IDE integration</a> on our External Tools and Tasks page.</p>
  467. </answer>
  468. </faq>
  469. <faq id="emacs-mode">
  470. <question>Why doesn&apos;t (X)Emacs/vi/MacOS X&apos;s project builder
  471. correctly parse the error messages generated by Ant?</question>
  472. <answer>
  473. <p>Ant adds a &quot;banner&quot; with the name of the current
  474. task in front of all logging messages - and there are no built-in
  475. regular expressions in your editor that would account for
  476. this.</p>
  477. <p>You can disable this banner by invoking Ant with the
  478. <code>-emacs</code> switch. Alternatively, you can add the
  479. following snippet to your <code>.emacs</code> to make Emacs
  480. understand Ant&apos;s output.</p>
  481. <source><![CDATA[
  482. (require 'compile)
  483. (setq compilation-error-regexp-alist
  484. (append (list
  485. ;; works for jikes
  486. '("^\\s-*\\[[^]]*\\]\\s-*\\(.+\\):\\([0-9]+\\):\\([0-9]+\\):[0-9]+:[0-9]+:" 1 2 3)
  487. ;; works for javac
  488. '("^\\s-*\\[[^]]*\\]\\s-*\\(.+\\):\\([0-9]+\\):" 1 2))
  489. compilation-error-regexp-alist))
  490. ]]></source>
  491. <p>Yet another alternative that preserves most of Ant&apos;s
  492. formatting is to pipe Ant&apos;s output through the following Perl
  493. script by Dirk-Willem van Gulik:</p>
  494. <source><![CDATA[
  495. #!/usr/bin/perl
  496. #
  497. # May 2001 dirkx@apache.org - remove any
  498. # [foo] lines from the output; keeping
  499. # spacing more or less there.
  500. #
  501. $|=1;
  502. while(<STDIN>) {
  503. if (s/^(\s+)\[(\w+)\]//) {
  504. if ($2 ne $last) {
  505. print "$1\[$2\]";
  506. $s = ' ' x length($2);
  507. } else {
  508. print "$1 $s ";
  509. };
  510. $last = $2;
  511. };
  512. print;
  513. };
  514. ]]></source>
  515. </answer>
  516. </faq>
  517. </faqsection>
  518. <faqsection title="Advanced Issues">
  519. <faq id="dtd">
  520. <question>Is there a DTD that I can use to validate my build
  521. files?</question>
  522. <answer>
  523. <p>An incomplete DTD can be created by the
  524. <code>&lt;antstructure&gt;</code> task - but this one
  525. has a few problems:</p>
  526. <ul>
  527. <li>It doesn&apos;t know about required attributes. Only
  528. manual tweaking of this file can help here.</li>
  529. <li>It is not complete - if you add new tasks via
  530. <code>&lt;taskdef&gt;</code> it won&apos;t know about it. See
  531. <a href="http://www.sdv.fr/pages/casa/html/ant-dtd.en.html">this
  532. page</a> by Michel Casabianca for a solution to this
  533. problem. Note that the DTD you can download at this page
  534. is based on Ant 0.3.1.</li>
  535. <li>It may even be an invalid DTD. As Ant allows tasks
  536. writers to define arbitrary elements, name collisions will
  537. happen quite frequently - if your version of Ant contains
  538. the optional <code>&lt;test&gt;</code> and
  539. <code>&lt;junit&gt;</code> tasks, there are two XML
  540. elements named <code>test</code> (the task and the nested child
  541. element of <code>&lt;junit&gt;</code>) with different attribute
  542. lists. This problem cannot be solved; DTDs don&apos;t give a
  543. syntax rich enough to support this.</li>
  544. </ul>
  545. </answer>
  546. </faq>
  547. <faq id="xml-entity-include">
  548. <question>How do I include an XML snippet in my build file?</question>
  549. <answer>
  550. <p>You can use XML&apos;s way of including external files and let
  551. the parser do the job for Ant:</p>
  552. <source><![CDATA[
  553. <?xml version="1.0"?>
  554. <!DOCTYPE project [
  555. <!ENTITY common SYSTEM "file:./common.xml">
  556. ]>
  557. <project name="test" default="test" basedir=".">
  558. <target name="setup">
  559. ...
  560. </target>
  561. &common;
  562. ...
  563. </project>
  564. ]]></source>
  565. <p>will literally include the contents of <code>common.xml</code> where
  566. you&apos;ve placed the <code>&amp;common;</code> entity.</p>
  567. <p>In combination with a DTD, this would look like this:</p>
  568. <source><![CDATA[
  569. <!DOCTYPE project PUBLIC "-//ANT//DTD project//EN" "file:./ant.dtd" [
  570. <!ENTITY include SYSTEM "file:./header.xml">
  571. ]>
  572. ]]></source>
  573. </answer>
  574. </faq>
  575. <faq id="mail-logger">
  576. <question>How do I send an email with the result of my build
  577. process?</question>
  578. <answer>
  579. <p>If you are using a nightly build of Ant 1.5 after
  580. 2001-12-14, you can use the built-in MailLogger:</p>
  581. <source><![CDATA[
  582. ant -logger org.apache.tools.ant.listener.MailLogger
  583. ]]></source>
  584. <p>See the <a href="http://cvs.apache.org/viewcvs/~checkout~/jakarta-ant/docs/manual/listeners.html?content-type=text/html">Listeners
  585. &amp; Loggers</a> documentation for details on the properties
  586. required.</p>
  587. <p>For older versions of Ant, you can use a custom
  588. BuildListener that sends out an email
  589. in the buildFinished() method. Will Glozer
  590. &lt;will.glozer@jda.com&gt; has written such a listener based
  591. on <a href="http://java.sun.com/products/javamail/">JavaMail</a>.
  592. The source is:</p>
  593. <source><![CDATA[
  594. import java.io.*;
  595. import java.util.*;
  596. import javax.mail.*;
  597. import javax.mail.internet.*;
  598. import org.apache.tools.ant.*;
  599. /**
  600. * A simple listener that waits for a build to finish and sends an email
  601. * of the results. The settings are stored in "monitor.properties" and
  602. * are fairly self explanatory.
  603. *
  604. * @author Will Glozer
  605. * @version 1.05a 09/06/2000
  606. */
  607. public class BuildMonitor implements BuildListener {
  608. protected Properties props;
  609. /**
  610. * Create a new BuildMonitor.
  611. */
  612. public BuildMonitor() throws Exception {
  613. props = new Properties();
  614. InputStream is = getClass().getResourceAsStream("monitor.properties");
  615. props.load(is);
  616. is.close();
  617. }
  618. public void buildStarted(BuildEvent e) {
  619. }
  620. /**
  621. * Determine the status of the build and the actions to follow, now that
  622. * the build has completed.
  623. *
  624. * @param e Event describing the build tatus.
  625. */
  626. public void buildFinished(BuildEvent e) {
  627. Throwable th = e.getException();
  628. String status = (th != null) ? "failed" : "succeeded";
  629. try {
  630. String key = "build." + status;
  631. if (props.getProperty(key + ".notify").equalsIgnoreCase("false")) {
  632. return;
  633. }
  634. Session session = Session.getDefaultInstance(props, null);
  635. MimeMessage message = new MimeMessage(session);
  636. message.addRecipients(Message.RecipientType.TO, parseAddresses(
  637. props.getProperty(key + ".email.to")));
  638. message.setSubject(props.getProperty(key + ".email.subject"));
  639. BufferedReader br = new BufferedReader(new FileReader(
  640. props.getProperty("build.log")));
  641. StringWriter sw = new StringWriter();
  642. String line = br.readLine();
  643. while (line != null) {
  644. sw.write(line);
  645. sw.write("\n");
  646. line = br.readLine();
  647. }
  648. br.close();
  649. message.setText(sw.toString(), "UTF-8");
  650. sw.close();
  651. Transport transport = session.getTransport();
  652. transport.connect();
  653. transport.send(message);
  654. transport.close();
  655. } catch (Exception ex) {
  656. System.out.println("BuildMonitor failed to send email!");
  657. ex.printStackTrace();
  658. }
  659. }
  660. /**
  661. * Parse a comma separated list of internet email addresses.
  662. *
  663. * @param s The list of addresses.
  664. * @return Array of Addresses.
  665. */
  666. protected Address[] parseAddresses(String s) throws Exception {
  667. StringTokenizer st = new StringTokenizer(s, ",");
  668. Address[] addrs = new Address[st.countTokens()];
  669. for (int i = 0; i < addrs.length; i++) {
  670. addrs[i] = new InternetAddress(st.nextToken());
  671. }
  672. return addrs;
  673. }
  674. public void messageLogged(BuildEvent e) {
  675. }
  676. public void targetStarted(BuildEvent e) {
  677. }
  678. public void targetFinished(BuildEvent e) {
  679. }
  680. public void taskStarted(BuildEvent e) {
  681. }
  682. public void taskFinished(BuildEvent e) {
  683. }
  684. }
  685. ]]></source>
  686. <p>With a <code>monitor.properties</code> like this:</p>
  687. <source><![CDATA[
  688. # configuration for build monitor
  689. mail.transport.protocol=smtp
  690. mail.smtp.host=<host>
  691. mail.from=Will Glozer <will.glozer@jda.com>
  692. build.log=build.log
  693. build.failed.notify=true
  694. build.failed.email.to=will.glozer@jda.com
  695. build.failed.email.subject=Nightly build failed!
  696. build.succeeded.notify=true
  697. build.succeeded.email.to=will.glozer@jda.com
  698. build.succeeded.email.subject=Nightly build succeeded!
  699. ]]></source>
  700. <p><code>monitor.properties</code> should be placed right next
  701. to your compiled <code>BuildMonitor.class</code>. To use it,
  702. invoke Ant like:</p>
  703. <source><![CDATA[
  704. ant -listener BuildMonitor -logfile build.log
  705. ]]></source>
  706. <p>Make sure that <code>mail.jar</code> from JavaMail and
  707. <code>activation.jar</code> from the
  708. <a href="http://java.sun.com/products/javabeans/glasgow/jaf.html">Java
  709. Beans Activation Framework</a> are in your <code>CLASSPATH</code>.</p>
  710. </answer>
  711. </faq>
  712. <faq id="listener-properties">
  713. <question>How do I get at the properties that Ant was running
  714. with from inside BuildListener?</question>
  715. <answer>
  716. <p>You can get at a hashtable with all the properties that Ant
  717. has been using through the BuildEvent parameter. For
  718. example:</p>
  719. <source><![CDATA[
  720. public void buildFinished(BuildEvent e) {
  721. Hashtable table = e.getProject().getProperties();
  722. String buildpath = (String)table.get("build.path");
  723. ...
  724. }
  725. ]]></source>
  726. <p>This is more accurate than just reading the same property
  727. files that your project does, since it will give the correct
  728. results for properties that were specified on the Ant command line.</p>
  729. </answer>
  730. </faq>
  731. </faqsection>
  732. <faqsection title="Known Problems">
  733. <faq id="remove-cr">
  734. <question>&lt;chmod&gt; or &lt;exec&gt; doesn&apos;t work in Ant
  735. 1.3 on Unix</question>
  736. <answer>
  737. <p>The <code>antRun</code> script in <code>ANT_HOME/bin</code>
  738. has DOS instead of Unix line endings; you must remove the
  739. carriage-return characters from this file. This can be done by
  740. using Ant&apos;s <code>&lt;fixcrlf&gt;</code> task
  741. or something like:</p>
  742. <source><![CDATA[
  743. tr -d '\r' < $ANT_HOME/bin/antRun > /tmp/foo
  744. mv /tmp/foo $ANT_HOME/bin/antRun
  745. ]]></source>
  746. </answer>
  747. </faq>
  748. <faq id="javadoc-cannot-execute">
  749. <question>JavaDoc failed: java.io.IOException: javadoc: cannot execute</question>
  750. <answer>
  751. <p>There is a bug in the Solaris reference implementation of
  752. 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>).
  753. This also appears to be true under Linux. Moving the JDK to
  754. the front of the PATH fixes the problem.</p>
  755. </answer>
  756. </faq>
  757. </faqsection>
  758. </document>