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

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