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

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