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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110
  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. <tr>
  118. <td>1.5</td>
  119. <td>10 July 2002</td>
  120. </tr>
  121. </table>
  122. </answer>
  123. </faq>
  124. </faqsection>
  125. <faqsection title="Installation">
  126. <faq id="no-gnu-tar">
  127. <question>I get checksum errors when I try to extract the
  128. <code>tar.gz</code> distribution file. Why?</question>
  129. <answer>
  130. <p>Ant&apos;s distribution contains file names that are longer
  131. than 100 characters, which is not supported by the standard
  132. tar file format. Several different implementations of tar use
  133. different and incompatible ways to work around this
  134. restriction.</p>
  135. <p>Ant&apos;s &lt;tar&gt; task can create tar archives that use
  136. the GNU tar extension, and this has been used when putting
  137. together the distribution. If you are using a different
  138. version of tar (for example, the one shipping with Solaris),
  139. you cannot use it to extract the archive.</p>
  140. <p>The solution is to either install GNU tar, which can be
  141. found <a href="http://www.gnu.org/software/tar/tar.html">here</a>,
  142. or use the zip archive instead (you can extract it using
  143. <code>jar xf</code>).</p>
  144. </answer>
  145. </faq>
  146. </faqsection>
  147. <faqsection title="Using Ant">
  148. <faq id="always-recompiles">
  149. <question>Why does Ant always recompile all my Java files?</question>
  150. <answer>
  151. <p>In order to find out which files should be compiled, Ant
  152. compares the timestamps of the source files to those of the
  153. resulting <code>.class</code> files. Opening all source files
  154. to find out which package they belong to would be very
  155. inefficient. Instead, Ant expects you to place your
  156. source files in a directory hierarchy that mirrors your
  157. package hierarchy and to point Ant to the root of this
  158. directory tree with the <code>srcdir</code> attribute.</p>
  159. <p>Say you have <code>&lt;javac srcdir=&quot;src&quot;
  160. destdir=&quot;dest&quot;/&gt;</code>. If Ant finds a file
  161. <code>src/a/b/C.java</code>, it expects it to be in package
  162. <code>a.b</code> so that the resulting <code>.class</code>
  163. file is going to be <code>dest/a/b/C.class</code>.</p>
  164. <p>If your source-tree directory structure does not match your
  165. package structure, Ant&apos;s heuristic won&apos;t work, and
  166. it will recompile classes that are up-to-date. Ant is not the
  167. only tool that expects a source-tree layout like this.</p>
  168. <p>If you have Java source files that aren&apos;t declared to
  169. be part of any package, you can still use the <code>&lt;javac&gt;</code>
  170. task to compile these files correctly - just set the
  171. <code>srcdir</code> and <code>destdir</code> attributes to
  172. the actual directory the source
  173. files live in and the directory the class files should go into,
  174. respectively.</p>
  175. </answer>
  176. </faq>
  177. <faq id="passing-cli-args">
  178. <question>How do I pass parameters from the command line to my
  179. build file?</question>
  180. <answer>
  181. <p>Use properties. Using <code>ant
  182. -D<em>name</em>=<em>value</em></code> lets you define values for
  183. properties on the Ant command line. These properties can then be
  184. used within your build file as
  185. any normal property: <code>${<em>name</em>}</code> will put in
  186. <code><em>value</em></code>.</p>
  187. </answer>
  188. </faq>
  189. <faq id="jikes-switches">
  190. <question>How can I use Jikes-specific command-line
  191. switches?</question>
  192. <answer>
  193. <p>A couple of switches are supported via &quot;magic&quot;
  194. properties:</p>
  195. <table>
  196. <tr>
  197. <th>switch</th>
  198. <th>property</th>
  199. <th>default</th>
  200. </tr>
  201. <tr>
  202. <td>+E</td>
  203. <td>build.compiler.emacs</td>
  204. <td>false == not set</td>
  205. </tr>
  206. <tr>
  207. <td>+P</td>
  208. <td>build.compiler.pedantic</td>
  209. <td>false == not set</td>
  210. </tr>
  211. <tr>
  212. <td>+F</td>
  213. <td>build.compiler.fulldepend</td>
  214. <td>false == not set</td>
  215. </tr>
  216. <tr>
  217. <td><strong>(Only for Ant &lt; 1.4; replaced by the
  218. <code><strong>nowarn</strong></code>
  219. attribute of the <code><strong>&lt;javac&gt;</strong></code>
  220. task after that.)</strong><br></br>-nowarn</td>
  221. <td>build.compiler.warnings</td>
  222. <td>true == not set</td>
  223. </tr>
  224. </table>
  225. <p>With Ant &gt;= 1.5, you can also use nested
  226. <code>&lt;compilerarg&gt;</code> elements with the
  227. <code>&lt;javac&gt;</code> task.</p>
  228. </answer>
  229. </faq>
  230. <faq id="shell-redirect-1">
  231. <question>How do I include a &lt; character in my command-line arguments?</question>
  232. <answer>
  233. <p>The short answer is "Use: <code>&amp;lt;</code>".</p>
  234. <p>The long answer is that this probably won&apos;t do what you
  235. want anyway (see <a href="#shell-redirect-2">the next
  236. section</a>).</p>
  237. </answer>
  238. </faq>
  239. <faq id="shell-redirect-2">
  240. <question>How do I redirect standard input or standard output
  241. in the <code>&lt;exec&gt;</code> task?</question>
  242. <answer>
  243. <p>Say you want to redirect the standard input stream of the
  244. <code>cat</code> command to read from a file, something
  245. like:</p>
  246. <source><![CDATA[
  247. shell-prompt> cat < foo
  248. ]]></source>
  249. <p>and try to translate it into</p>
  250. <source><![CDATA[
  251. <exec executable="cat">
  252. <arg value="&lt;" />
  253. <arg value="foo" />
  254. </exec>
  255. ]]></source>
  256. <p>This will not do what you expect. The input redirection is
  257. performed by your shell, not the command itself, so this
  258. should read:</p>
  259. <source><![CDATA[
  260. <exec executable="/bin/sh">
  261. <arg value="-c" />
  262. <arg value="cat &lt; foo" />
  263. </exec>
  264. ]]></source>
  265. <p>Note that you must use the <code>value</code> attribute of
  266. <code>&lt;arg&gt;</code> in the last element, in order to have
  267. the command passed as a single, quoted argument. Alternatively,
  268. you can use:</p>
  269. <source><![CDATA[
  270. <exec executable="/bin/sh">
  271. <arg line='-c "cat &lt; foo"'/>
  272. </exec>
  273. ]]></source>
  274. <p>Note the double-quotes nested inside the single-quotes.</p>
  275. </answer>
  276. </faq>
  277. <faq id="batch-shell-execute">
  278. <question>How do I execute a batch file or shell script from Ant?</question>
  279. <answer>
  280. <p>On native Unix systems, you should be able to run shell scripts
  281. directly. On systems running a Unix-type shell (for example, Cygwin
  282. on Windows) execute the (command) shell instead - <code>cmd</code>
  283. for batch files, <code>sh</code> for shell scripts - then pass the
  284. batch file or shell script (plus any arguments to the script)
  285. as a single command, using the <code>/c</code> or
  286. <code>-c</code> switch, respectively. See
  287. <a href="#shell-redirect-2">the above section</a>
  288. for example <code>&lt;exec&gt;</code> tasks
  289. executing <code>sh</code>. For batch files, use something like:</p>
  290. <source><![CDATA[
  291. <exec dir="." executable="cmd" os="Windows NT">
  292. <arg line="/c test.bat"/>
  293. </exec>
  294. ]]></source>
  295. </answer>
  296. </faq>
  297. <faq id="defaultexcludes">
  298. <question>I&apos;ve used a <code>&lt;delete&gt;</code> task to delete
  299. unwanted
  300. SourceSafe control files (CVS files, editor backup files, etc.), but
  301. it doesn&apos;t seem to work; the files never get deleted. What&apos;s
  302. wrong?</question>
  303. <answer>
  304. <p>This is probably happening because, by default, Ant excludes
  305. SourceSafe control files (<code>vssver.scc</code>) and certain other
  306. files from FileSets.</p>
  307. <p>Here&apos;s what you probably did:</p>
  308. <source><![CDATA[
  309. <delete>
  310. <fileset dir="${build.src}" includes="**/vssver.scc"/>
  311. </delete>
  312. ]]></source>
  313. <p>You need to switch off the default exclusions,
  314. and it will work:</p>
  315. <source><![CDATA[
  316. <delete>
  317. <fileset dir="${build.src}" includes="**/vssver.scc"
  318. defaultexcludes="no"/>
  319. </delete>
  320. ]]></source>
  321. <p>For a complete listing of the patterns that are excluded
  322. by default, see <a href="manual/dirtasks.html#defaultexcludes">the user
  323. manual</a>.</p>
  324. </answer>
  325. </faq>
  326. <faq id="multi-conditions">
  327. <question>I want to execute a particular target only if
  328. multiple conditions are true.</question>
  329. <answer>
  330. <p>There are actually several answers to this question.</p>
  331. <p>If you have only one set and one unset property to test,
  332. you can specify both an <code>if</code> and an <code>unless</code>
  333. attribute for the target, and they will act as if they
  334. are &quot;anded&quot; together.</p>
  335. <p>If you are using a version of Ant 1.3 or earlier, the
  336. way to work with all other cases is to chain targets together
  337. to determine the specific state you want to test for.</p>
  338. <p>To see how this works, assume you have three properties:
  339. <code>prop1</code>, <code>prop2</code>, and <code>prop3</code>.
  340. You want to test that <code>prop1</code> and <code>prop2</code>
  341. are set, and that <code>prop3</code> is not. If the condition
  342. holds true you want to echo &quot;yes&quot;.</p>
  343. <p>Here is the implementation in Ant 1.3 and earlier:</p>
  344. <source><![CDATA[
  345. <target name="cond" depends="cond-if"/>
  346. <target name="cond-if" if="prop1">
  347. <antcall target="cond-if-2"/>
  348. </target>
  349. <target name="cond-if-2" if="prop2">
  350. <antcall target="cond-if-3"/>
  351. </target>
  352. <target name="cond-if-3" unless="prop3">
  353. <echo message="yes"/>
  354. </target>
  355. ]]></source>
  356. <p>Note: <code>&lt;antcall&gt;</code> tasks do <em>not</em> pass
  357. property changes back up to the environment they were called
  358. from, so you would&apos;nt be able to, for example, set a
  359. <code>result</code> property in the <code>cond-if-3</code> target,
  360. then do
  361. <code>&lt;echo message=&quot;result is ${result}&quot;/&gt;</code>
  362. in the <code>cond</code> target.</p>
  363. <p>Starting with Ant 1.4, you can use the
  364. <code>&lt;condition&gt;</code> task.</p>
  365. <source><![CDATA[
  366. <target name="cond" depends="cond-if,cond-else"/>
  367. <target name="check-cond">
  368. <condition property="cond-is-true">
  369. <and>
  370. <not>
  371. <equals arg1="${prop1}" arg2="$${prop1}" />
  372. </not>
  373. <not>
  374. <equals arg1="${prop2}" arg2="$${prop2}" />
  375. </not>
  376. <equals arg1="${prop3}" arg2="$${prop3}" />
  377. </and>
  378. </condition>
  379. </target>
  380. <target name="cond-if" depends="check-cond" if="cond-is-true">
  381. <echo message="yes"/>
  382. </target>
  383. <target name="cond-else" depends="check-cond" unless="cond-is-true">
  384. <echo message="no"/>
  385. </target>
  386. ]]></source>
  387. <p>This version takes advantage of two things:</p>
  388. <ul>
  389. <li>If a property <code>a</code> has not been set,
  390. <code>${a}</code> will evaluate to <code>${a}</code>.</li>
  391. <li>To get a literal <code>$</code> in Ant, you have to
  392. escape it with another <code>$</code> - this will also break
  393. the special treatment of the <code>${</code> sequence.</li>
  394. </ul>
  395. <p>Because testing for a literal <code>${property}</code> string
  396. isn&apos;t all that readable or easy to understand,
  397. post-1.4.1 Ant introduces the <code>&lt;isset&gt;</code> element
  398. to the <code>&lt;condition&gt;</code> task.</p>
  399. <p>Here is the previous example done using
  400. <code>&lt;isset&gt;</code>:</p>
  401. <source><![CDATA[
  402. <target name="check-cond">
  403. <condition property="cond-is-true">
  404. <and>
  405. <isset property="prop1"/>
  406. <isset property="prop2"/>
  407. <not>
  408. <isset property="prop3"/>
  409. </not>
  410. </and>
  411. </condition>
  412. </target>
  413. ]]></source>
  414. <p>The last option is to use a scripting language to set the
  415. properties. This can be particularly handy when you need much
  416. finer control than the simple conditions shown here but, of
  417. course, comes with the overhead of adding JAR files to support
  418. the language, to say nothing of the added maintenance in requiring
  419. two languages to implement a single system. See the
  420. <a href="manual/OptionalTasks/script.html">
  421. <code>&lt;script&gt;</code> task documentation</a> for more
  422. details.</p>
  423. </answer>
  424. </faq>
  425. <faq id="stop-dependency">
  426. <question>I have a target I want to skip if a property is set,
  427. so I have <code>unless=&quot;property&quot;</code> as an attribute
  428. of the target, but all the targets this target
  429. depends on are still executed. Why?</question>
  430. <answer>
  431. <p>The list of dependencies is generated by Ant before any of the
  432. targets are run. This allows dependent targets, such as an
  433. <code>init</code> target, to set properties that can control the
  434. execution of the targets higher in the dependency graph. This
  435. is a good thing.</p>
  436. <p>However, when your dependencies break down the
  437. higher-level task
  438. into several smaller steps, this behaviour becomes
  439. counter-intuitive. There are a couple of solutions available:
  440. </p>
  441. <ol>
  442. <li>Put the same condition on each of the dependent targets.</li>
  443. <li>Execute the steps using <code>&lt;antcall&gt;</code>,
  444. instead of specifying them inside the <code>depends</code>
  445. attribute.</li>
  446. </ol>
  447. </answer>
  448. </faq>
  449. <faq id="include-order">
  450. <question>In my <code>&lt;fileset&gt;</code>, I&apos;ve put in an
  451. <code>&lt;exclude&gt;</code> of all files followed by an
  452. <code>&lt;include&gt;</code> of just the files I want, but it
  453. isn&apos;t giving me any files at all. What&apos;s wrong?
  454. </question>
  455. <answer>
  456. <p>The order of the <code>&lt;include&gt;</code> and
  457. <code>&lt;exclude&gt;</code> tags within a <code>&lt;fileset&gt;</code>
  458. is ignored when the FileSet is created. Instead, all of the
  459. <code>&lt;include&gt;</code> elements are processed together,
  460. followed by all of the <code>&lt;exclude&gt;</code>
  461. elements. This means that the <code>&lt;exclude&gt;</code>
  462. elements only apply to the file list produced by the
  463. <code>&lt;include&gt;</code> elements.</p>
  464. <p>To get the files you want, focus on just the
  465. <code>&lt;include&gt;</code> patterns that would be necessary
  466. to get them. If you find you need to trim the list that the
  467. <code>&lt;include&gt;</code> elements
  468. produce, then use <code>&lt;exclude&gt;</code> elements.</p>
  469. </answer>
  470. </faq>
  471. <faq id="encoding">
  472. <question>How can I include national characters like German
  473. umlauts in my build file?</question>
  474. <answer>
  475. <p>You need to tell the XML parser which character encoding
  476. your build file uses, this is done inside the <a
  477. href="http://www.w3.org/TR/2000/REC-xml-20001006#sec-prolog-dtd">XML
  478. declaration</a>.</p>
  479. <p>By default the parser assumes you are using the UTF-8
  480. encoding instead of your platform's default. For most Western
  481. European countries you should set the encoding to
  482. <code>ISO-8859-1</code>. To do so, make the very first line
  483. of you build file read like</p>
  484. <source><![CDATA[
  485. <?xml version="1.0" encoding="ISO-8859-1" ?>
  486. ]]></source>
  487. </answer>
  488. </faq>
  489. </faqsection>
  490. <faqsection title="Ant and IDEs/Editors">
  491. <faq id="integration">
  492. <question>Is Ant supported by my IDE/Editor?</question>
  493. <answer>
  494. <p>See the <a href="external.html#IDE and Editor Integration">section
  495. on IDE integration</a> on our External Tools and Tasks page.</p>
  496. </answer>
  497. </faq>
  498. <faq id="emacs-mode">
  499. <question>Why doesn&apos;t (X)Emacs/vi/MacOS X&apos;s project builder
  500. correctly parse the error messages generated by Ant?</question>
  501. <answer>
  502. <p>Ant adds a &quot;banner&quot; with the name of the current
  503. task in front of all logging messages - and there are no built-in
  504. regular expressions in your editor that would account for
  505. this.</p>
  506. <p>You can disable this banner by invoking Ant with the
  507. <code>-emacs</code> switch. To make Ant autodetect
  508. Emacs&apos; compile mode, put this into your
  509. <code>.antrc</code> (contributed by Ville Skytt&#228;).</p>
  510. <source><![CDATA[
  511. # Detect (X)Emacs compile mode
  512. if [ "$EMACS" = "t" ] ; then
  513. ANT_ARGS="$ANT_ARGS -emacs"
  514. ANT_OPTS="$ANT_OPTS -Dbuild.compiler.emacs=true"
  515. fi
  516. ]]></source>
  517. <p>Alternatively, you can add the following snippet to your
  518. <code>.emacs</code> to make Emacs understand Ant&apos;s
  519. output.</p>
  520. <source><![CDATA[
  521. (require 'compile)
  522. (setq compilation-error-regexp-alist
  523. (append (list
  524. ;; works for jikes
  525. '("^\\s-*\\[[^]]*\\]\\s-*\\(.+\\):\\([0-9]+\\):\\([0-9]+\\):[0-9]+:[0-9]+:" 1 2 3)
  526. ;; works for javac
  527. '("^\\s-*\\[[^]]*\\]\\s-*\\(.+\\):\\([0-9]+\\):" 1 2))
  528. compilation-error-regexp-alist))
  529. ]]></source>
  530. <p>Yet another alternative that preserves most of Ant&apos;s
  531. formatting is to pipe Ant&apos;s output through the following Perl
  532. script by Dirk-Willem van Gulik:</p>
  533. <source><![CDATA[
  534. #!/usr/bin/perl
  535. #
  536. # May 2001 dirkx@apache.org - remove any
  537. # [foo] lines from the output; keeping
  538. # spacing more or less there.
  539. #
  540. $|=1;
  541. while(<STDIN>) {
  542. if (s/^(\s+)\[(\w+)\]//) {
  543. if ($2 ne $last) {
  544. print "$1\[$2\]";
  545. $s = ' ' x length($2);
  546. } else {
  547. print "$1 $s ";
  548. };
  549. $last = $2;
  550. };
  551. print;
  552. };
  553. ]]></source>
  554. </answer>
  555. </faq>
  556. </faqsection>
  557. <faqsection title="Advanced Issues">
  558. <faq id="dtd">
  559. <question>Is there a DTD that I can use to validate my build
  560. files?</question>
  561. <answer>
  562. <p>An incomplete DTD can be created by the
  563. <code>&lt;antstructure&gt;</code> task - but this one
  564. has a few problems:</p>
  565. <ul>
  566. <li>It doesn&apos;t know about required attributes. Only
  567. manual tweaking of this file can help here.</li>
  568. <li>It is not complete - if you add new tasks via
  569. <code>&lt;taskdef&gt;</code> it won&apos;t know about it. See
  570. <a href="http://www.sdv.fr/pages/casa/html/ant-dtd.en.html">this
  571. page</a> by Michel Casabianca for a solution to this
  572. problem. Note that the DTD you can download at this page
  573. is based on Ant 0.3.1.</li>
  574. <li>It may even be an invalid DTD. As Ant allows tasks
  575. writers to define arbitrary elements, name collisions will
  576. happen quite frequently - if your version of Ant contains
  577. the optional <code>&lt;test&gt;</code> and
  578. <code>&lt;junit&gt;</code> tasks, there are two XML
  579. elements named <code>test</code> (the task and the nested child
  580. element of <code>&lt;junit&gt;</code>) with different attribute
  581. lists. This problem cannot be solved; DTDs don&apos;t give a
  582. syntax rich enough to support this.</li>
  583. </ul>
  584. </answer>
  585. </faq>
  586. <faq id="xml-entity-include">
  587. <question>How do I include an XML snippet in my build file?</question>
  588. <answer>
  589. <p>You can use XML&apos;s way of including external files and let
  590. the parser do the job for Ant:</p>
  591. <source><![CDATA[
  592. <?xml version="1.0"?>
  593. <!DOCTYPE project [
  594. <!ENTITY common SYSTEM "file:./common.xml">
  595. ]>
  596. <project name="test" default="test" basedir=".">
  597. <target name="setup">
  598. ...
  599. </target>
  600. &common;
  601. ...
  602. </project>
  603. ]]></source>
  604. <p>will literally include the contents of <code>common.xml</code> where
  605. you&apos;ve placed the <code>&amp;common;</code> entity.</p>
  606. <p>In combination with a DTD, this would look like this:</p>
  607. <source><![CDATA[
  608. <!DOCTYPE project PUBLIC "-//ANT//DTD project//EN" "file:./ant.dtd" [
  609. <!ENTITY include SYSTEM "file:./header.xml">
  610. ]>
  611. ]]></source>
  612. </answer>
  613. </faq>
  614. <faq id="mail-logger">
  615. <question>How do I send an email with the result of my build
  616. process?</question>
  617. <answer>
  618. <p>If you are using a nightly build of Ant 1.5 after
  619. 2001-12-14, you can use the built-in MailLogger:</p>
  620. <source><![CDATA[
  621. ant -logger org.apache.tools.ant.listener.MailLogger
  622. ]]></source>
  623. <p>See the <a href="http://cvs.apache.org/viewcvs/~checkout~/jakarta-ant/docs/manual/listeners.html?content-type=text/html">Listeners
  624. &amp; Loggers</a> documentation for details on the properties
  625. required.</p>
  626. <p>For older versions of Ant, you can use a custom
  627. BuildListener that sends out an email
  628. in the buildFinished() method. Will Glozer
  629. &lt;will.glozer@jda.com&gt; has written such a listener based
  630. on <a href="http://java.sun.com/products/javamail/">JavaMail</a>.
  631. The source is:</p>
  632. <source><![CDATA[
  633. import java.io.*;
  634. import java.util.*;
  635. import javax.mail.*;
  636. import javax.mail.internet.*;
  637. import org.apache.tools.ant.*;
  638. /**
  639. * A simple listener that waits for a build to finish and sends an email
  640. * of the results. The settings are stored in "monitor.properties" and
  641. * are fairly self explanatory.
  642. *
  643. * @author Will Glozer
  644. * @version 1.05a 09/06/2000
  645. */
  646. public class BuildMonitor implements BuildListener {
  647. protected Properties props;
  648. /**
  649. * Create a new BuildMonitor.
  650. */
  651. public BuildMonitor() throws Exception {
  652. props = new Properties();
  653. InputStream is = getClass().getResourceAsStream("monitor.properties");
  654. props.load(is);
  655. is.close();
  656. }
  657. public void buildStarted(BuildEvent e) {
  658. }
  659. /**
  660. * Determine the status of the build and the actions to follow, now that
  661. * the build has completed.
  662. *
  663. * @param e Event describing the build status.
  664. */
  665. public void buildFinished(BuildEvent e) {
  666. Throwable th = e.getException();
  667. String status = (th != null) ? "failed" : "succeeded";
  668. try {
  669. String key = "build." + status;
  670. if (props.getProperty(key + ".notify").equalsIgnoreCase("false")) {
  671. return;
  672. }
  673. Session session = Session.getDefaultInstance(props, null);
  674. MimeMessage message = new MimeMessage(session);
  675. message.addRecipients(Message.RecipientType.TO, parseAddresses(
  676. props.getProperty(key + ".email.to")));
  677. message.setSubject(props.getProperty(key + ".email.subject"));
  678. BufferedReader br = new BufferedReader(new FileReader(
  679. props.getProperty("build.log")));
  680. StringWriter sw = new StringWriter();
  681. String line = br.readLine();
  682. while (line != null) {
  683. sw.write(line);
  684. sw.write("\n");
  685. line = br.readLine();
  686. }
  687. br.close();
  688. message.setText(sw.toString(), "UTF-8");
  689. sw.close();
  690. Transport transport = session.getTransport();
  691. transport.connect();
  692. transport.send(message);
  693. transport.close();
  694. } catch (Exception ex) {
  695. System.out.println("BuildMonitor failed to send email!");
  696. ex.printStackTrace();
  697. }
  698. }
  699. /**
  700. * Parse a comma separated list of internet email addresses.
  701. *
  702. * @param s The list of addresses.
  703. * @return Array of Addresses.
  704. */
  705. protected Address[] parseAddresses(String s) throws Exception {
  706. StringTokenizer st = new StringTokenizer(s, ",");
  707. Address[] addrs = new Address[st.countTokens()];
  708. for (int i = 0; i < addrs.length; i++) {
  709. addrs[i] = new InternetAddress(st.nextToken());
  710. }
  711. return addrs;
  712. }
  713. public void messageLogged(BuildEvent e) {
  714. }
  715. public void targetStarted(BuildEvent e) {
  716. }
  717. public void targetFinished(BuildEvent e) {
  718. }
  719. public void taskStarted(BuildEvent e) {
  720. }
  721. public void taskFinished(BuildEvent e) {
  722. }
  723. }
  724. ]]></source>
  725. <p>With a <code>monitor.properties</code> like this:</p>
  726. <source><![CDATA[
  727. # configuration for build monitor
  728. mail.transport.protocol=smtp
  729. mail.smtp.host=<host>
  730. mail.from=Will Glozer <will.glozer@jda.com>
  731. build.log=build.log
  732. build.failed.notify=true
  733. build.failed.email.to=will.glozer@jda.com
  734. build.failed.email.subject=Nightly build failed!
  735. build.succeeded.notify=true
  736. build.succeeded.email.to=will.glozer@jda.com
  737. build.succeeded.email.subject=Nightly build succeeded!
  738. ]]></source>
  739. <p><code>monitor.properties</code> should be placed right next
  740. to your compiled <code>BuildMonitor.class</code>. To use it,
  741. invoke Ant like:</p>
  742. <source><![CDATA[
  743. ant -listener BuildMonitor -logfile build.log
  744. ]]></source>
  745. <p>Make sure that <code>mail.jar</code> from JavaMail and
  746. <code>activation.jar</code> from the
  747. <a href="http://java.sun.com/products/javabeans/glasgow/jaf.html">Java
  748. Beans Activation Framework</a> are in your <code>CLASSPATH</code>.</p>
  749. </answer>
  750. </faq>
  751. <faq id="listener-properties">
  752. <question>How do I get at the properties that Ant was running
  753. with from inside BuildListener?</question>
  754. <answer>
  755. <p>You can get at a hashtable with all the properties that Ant
  756. has been using through the BuildEvent parameter. For
  757. example:</p>
  758. <source><![CDATA[
  759. public void buildFinished(BuildEvent e) {
  760. Hashtable table = e.getProject().getProperties();
  761. String buildpath = (String)table.get("build.path");
  762. ...
  763. }
  764. ]]></source>
  765. <p>This is more accurate than just reading the same property
  766. files that your project does, since it will give the correct
  767. results for properties that were specified on the Ant command line.</p>
  768. </answer>
  769. </faq>
  770. </faqsection>
  771. <faqsection title="Known Problems">
  772. <faq id="remove-cr">
  773. <question>&lt;chmod&gt; or &lt;exec&gt; doesn&apos;t work in Ant
  774. 1.3 on Unix</question>
  775. <answer>
  776. <p>The <code>antRun</code> script in <code>ANT_HOME/bin</code>
  777. has DOS instead of Unix line endings; you must remove the
  778. carriage-return characters from this file. This can be done by
  779. using Ant&apos;s <code>&lt;fixcrlf&gt;</code> task
  780. or something like:</p>
  781. <source><![CDATA[
  782. tr -d '\r' < $ANT_HOME/bin/antRun > /tmp/foo
  783. mv /tmp/foo $ANT_HOME/bin/antRun
  784. ]]></source>
  785. </answer>
  786. </faq>
  787. <faq id="javadoc-cannot-execute">
  788. <question>JavaDoc failed: java.io.IOException: javadoc: cannot execute</question>
  789. <answer>
  790. <p>There is a bug in the Solaris reference implementation of
  791. 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>).
  792. This also appears to be true under Linux. Moving the JDK to
  793. the front of the PATH fixes the problem.</p>
  794. </answer>
  795. </faq>
  796. <faq id="delegating-classloader">
  797. <question>&lt;style&gt; or &lt;junit&gt; ignores my
  798. &lt;classpath&gt;</question>
  799. <answer>
  800. <p>These tasks don&apos;t ignore your classpath setting, you
  801. are facing a common problem with delegating classloaders.</p>
  802. <p>First of all let&apos;s state that Ant adds all
  803. <code>.jar</code> files from <code>ANT_HOME/lib</code> to
  804. <code>CLASSPATH</code>, therefore &quot;in
  805. <code>CLASSPATH</code>&quot; shall mean &quot;either in your
  806. <code>CLASSPATH</code> environment variable or
  807. <code>ANT_HOME/lib</code>&quot; for the rest of this
  808. answer.</p>
  809. <p>This question collects a common type of problem: A task
  810. needs an external library and it has a nested classpath
  811. element so that you can point it to this external library, but
  812. that doesn&apos;t work unless you put the external library into the
  813. <code>CLASSPATH</code>.</p>
  814. <p>The root of the problem is that the class that needs the
  815. external library is on the <code>CLASSPATH</code>.</p>
  816. <p>When you specify a nested <code>&lt;classpath&gt;</code> in
  817. Ant, Ant creates a new class loader that uses the path you
  818. have specified. It then tries to load additional classes from
  819. this classloader.</p>
  820. <p>In most cases - for example the two cases above - Ant
  821. doesn&apos;t load the external library directly, it is the loaded
  822. class that does so.</p>
  823. <p>In the case of <code>&lt;junit&gt;</code> it is the task
  824. implementation itself and in the case of
  825. <code>&lt;style&gt;</code> it is the implementation of the
  826. <code>org.apache.tools.ant.taskdefs.XSLTLiaison</code>
  827. class.</p>
  828. <p>Ant&apos;s class loader implementation uses Java&apos;s
  829. delegation model, see <a
  830. href="http://java.sun.com/products/jdk/1.2/docs/api/java/lang/ClassLoader.html">http://java.sun.com/products/jdk/1.2/docs/api/java/lang/ClassLoader.html</a>
  831. the paragraph</p>
  832. <blockquote>The <code>ClassLoader</code> class uses a
  833. delegation model to search for classes and resources. Each
  834. instance of <code>ClassLoader</code> has an associated parent
  835. class loader. When called upon to find a class or resource, a
  836. <code>ClassLoader</code> instance will delegate the search for
  837. the class or resource to its parent class loader before
  838. attempting to find the class or resource itself. The virtual
  839. machine&apos;s built-in class loader, called the bootstrap
  840. class loader, does not itself have a parent but may serve as
  841. the parent of a <code>ClassLoader</code>
  842. instance.</blockquote>
  843. <p>This means, Ant&apos;s class loader will consult the
  844. bootstrap class loader first, which tries to load classes from
  845. <code>CLASSPATH</code>. The bootstrap class loader
  846. doesn&apos;t know anything about Ant&apos;s class loader or
  847. even the path you have specified.</p>
  848. <p>If the bootstrap class loader can load the class Ant has
  849. asked it to load, this class will try to load the external
  850. library from <code>CLASSPATH</code> as well - it doesn&apos;t
  851. know anything else - and will not find it unless the library
  852. is in <code>CLASSPATH</code> as well.</p>
  853. <p>To solve this, you have two major options:</p>
  854. <ol>
  855. <li>put all external libraries you need in
  856. <code>CLASSPATH</code> as well this is not what you want,
  857. otherwise you wouldn&apos;t have found this FAQ entry.</li>
  858. <li>remove the class that loads the external library from
  859. the <code>CLASSPATH</code>.</li>
  860. </ol>
  861. <p>The easiest way to do this is to remove
  862. <code>optional.jar</code> from <code>ANT_HOME/lib</code>. If
  863. you do so, you will have to <code>&lt;taskdef&gt;</code> all
  864. optional tasks and use nested <code>&lt;classpath&gt;</code>
  865. elements in the <code>&lt;taskdef&gt;</code> tasks that point
  866. to the new location of <code>optional.jar</code>. Also,
  867. don&apos;t forget to add the new location of
  868. <code>optional.jar</code> to the
  869. <code>&lt;classpath&gt;</code> of your
  870. <code>&lt;style&gt;</code> or <code>&lt;junit&gt;</code>
  871. task.</p>
  872. <p>If you want to avoid to <code>&lt;taskdef&gt;</code> all
  873. optional tasks you need, the only other option is to remove
  874. the classes that should not be loaded via the bootstrap class
  875. loader from <code>optional.jar</code> and put them into a
  876. separate archive. Add this separate archive to the
  877. <code>&lt;classpath&gt;</code> of your
  878. <code>&lt;style&gt;</code> or <code>&lt;junit&gt;</code> task
  879. - and make sure the separate archive is not in
  880. <code>CLASSPATH</code>.</p>
  881. <p>In the case of <code>&lt;junit&gt;</code> you&apos;d have
  882. to remove all classes that are in the
  883. <code>org/apache/tools/ant/taskdefs/optional/junit</code>
  884. directory, in the <code>&lt;style&gt;</code> case it is one of
  885. the <code>*Liaison</code> classes in
  886. <code>org/apache/tools/ant/taskdefs/optional</code>.</p>
  887. <p>If you use the option to break up <code>optional.jar</code>
  888. for <code>&lt;junit&gt;</code>, you still have to use a
  889. <code>&lt;taskdef&gt;</code> with a nested
  890. <code>&lt;classpath&gt;</code> to define the junit task.</p>
  891. </answer>
  892. </faq>
  893. <faq id="1.5-cygwin-sh">
  894. <question>The <code>ant</code> wrapper script of Ant 1.5 fails
  895. for Cygwin if <code>ANT_HOME</code> is set to a Windows style
  896. path.</question>
  897. <answer>
  898. <p>This problem has been reported only hours after Ant 1.5 has
  899. been released, see <a
  900. href="http://nagoya.apache.org/bugzilla/show_bug.cgi?id=10664">Bug
  901. 10664</a> and all it's duplicates.</p>
  902. <p>A fixed version of the wrapper script can be found <a
  903. href="http://jakarta.apache.org/builds/jakarta-ant/release/v1.5/errata/">here</a>.
  904. Simply replace your script with this version.</p>
  905. </answer>
  906. </faq>
  907. </faqsection>
  908. </document>