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

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