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

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