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

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