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

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