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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  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="General">
  8. <faq id="what-is-ant">
  9. <question>What is Ant?</question>
  10. <answer>
  11. <p> Ant is a Java based build tool. In theory it is kind of
  12. like &quot;make&quot; without makes wrinkles and with the full
  13. portability of pure Java code.</p>
  14. </answer>
  15. </faq>
  16. <faq id="ant-name">
  17. <question>Why do you call it Ant?</question>
  18. <answer>
  19. <p>According to Ant&apos;s original author James Duncan
  20. Davidson, the name is an acronym for &quot;Another Neat
  21. Tool&quot;.</p>
  22. <p>Later explanations go along the lines of &quot;Ants are
  23. doing an extremely good job at building things&quot; or
  24. &quot;Ants are very small and can carry a weight a dozen times
  25. of their own&quot; - describing what Ant is intended to
  26. be.</p>
  27. </answer>
  28. </faq>
  29. <faq id="history">
  30. <question>Tell us a little bit about Ant&apos;s history.</question>
  31. <answer>
  32. <p>Initially Ant was part of the Tomcat code base when it was
  33. donated to the Apache Software Foundation - it has been
  34. created by James Duncan Davidson, who also is the original
  35. author of Tomcat. Ant was there to build Tomcat, nothing
  36. else.</p>
  37. <p>Soon thereafter several open source Java projects realized
  38. that Ant could solve the problems they had with makefiles.
  39. Starting with the projects hosted at Jakarta and the old Java
  40. Apache project, Ant spread like a virus and now is the build
  41. tool of choice for a lot of projects.</p>
  42. <p>In January 2000 Ant was moved to a separate CVS module and
  43. was promoted to a project of its own, independent of
  44. Tomcat.</p>
  45. <p>The first version of Ant that was exposed a lager audience
  46. was the one that shipped with Tomcat&apos;s 3.1 release on 19 April
  47. 2000. This version has later been referenced to as Ant
  48. 0.3.1.</p>
  49. <p>The first official release of Ant as a stand alone product was
  50. Ant 1.1 released on 19 July 2000. The complete release
  51. history:</p>
  52. <table>
  53. <tr>
  54. <th>Ant Version</th>
  55. <th>Release Date</th>
  56. </tr>
  57. <tr>
  58. <td>1.1</td>
  59. <td>19 July 2000</td>
  60. </tr>
  61. <tr>
  62. <td>1.2</td>
  63. <td>24 October 2000</td>
  64. </tr>
  65. <tr>
  66. <td>1.3</td>
  67. <td>3 March 2001</td>
  68. </tr>
  69. <tr>
  70. <td>1.4</td>
  71. <td>3 September 2001</td>
  72. </tr>
  73. </table>
  74. </answer>
  75. </faq>
  76. </faqsection>
  77. <faqsection title="Installation">
  78. <faq id="no-gnu-tar">
  79. <question>I get checksum errors when I try to extract the
  80. <code>tar.gz</code> distribution file. Why?</question>
  81. <answer>
  82. <p>Ant&apos;s distribution contains file names that are longer
  83. than 100 characters, which is not supported by the standard
  84. tar file format. Several different implementations of tar use
  85. different and incompatible ways to work around this
  86. restriction.</p>
  87. <p>Ant&apos;s &lt;tar&gt; task can create tar archives that use
  88. the GNU tar extension, and this has been used when putting
  89. together the distribution. If you are using a different
  90. version of tar (for example, the one shipping with Solaris),
  91. you cannot use it to extract the archive.</p>
  92. <p>The solution is to either install GNU tar, which can be
  93. found <a href="http://www.gnu.org/software/tar/tar.html">here</a>
  94. or use the zip archive instead (you can extract it using
  95. <code>jar xf</code>).</p>
  96. </answer>
  97. </faq>
  98. </faqsection>
  99. <faqsection title="Using Ant">
  100. <faq id="always-recompiles">
  101. <question>Why does Ant always recompile all my Java files?</question>
  102. <answer>
  103. <p>In order to find out which files should be compiled, Ant
  104. compares the timestamps of the source files to those of the
  105. resulting <code>.class</code> files. Opening all source files
  106. to find out which package they belong to would be very
  107. inefficient - instead of this, Ant expects you to place your
  108. source files in a directory hierarchy that mirrors your
  109. package hierarchy and to point Ant to the root of this
  110. directory tree with the <code>srcdir</code> attribute.</p>
  111. <p>Say you have <code>&lt;javac srcdir=&quot;src&quot;
  112. destdir=&quot;dest&quot; /&gt;</code>. If Ant finds a file
  113. <code>src/a/b/C.java</code> it expects it to be in package
  114. <code>a.b</code> so that the resulting <code>.class</code>
  115. file is going to be <code>dest/a/b/C.class</code>.</p>
  116. <p>If your setup is different, Ant&apos;s heuristic won&apos;t work and
  117. it will recompile classes that are up to date. Ant is not the
  118. only tool, that expects a source tree layout like this.</p>
  119. </answer>
  120. </faq>
  121. <faq id="passing-cli-args">
  122. <question>How do I pass parameters from the command line to my
  123. build file?</question>
  124. <answer>
  125. <p>Use properties: <code>ant
  126. -D&lt;name&gt;=&lt;value&gt;</code> lets you define values for
  127. properties. These can then be used within your build file as
  128. any normal property: <code>${&lt;name&gt;}</code> will put in
  129. <code>&lt;value&gt;</code>.</p>
  130. </answer>
  131. </faq>
  132. <faq id="jikes-switches">
  133. <question>How can I use Jikes specific command line
  134. switches?</question>
  135. <answer>
  136. <p>A couple of switches are supported via magic
  137. properties:</p>
  138. <table>
  139. <tr>
  140. <th>switch</th>
  141. <th>property</th>
  142. <th>default</th>
  143. </tr>
  144. <tr>
  145. <td>+E</td>
  146. <td>build.compiler.emacs</td>
  147. <td>false == not set</td>
  148. </tr>
  149. <tr>
  150. <td>+P</td>
  151. <td>build.compiler.pedantic</td>
  152. <td>false == not set</td>
  153. </tr>
  154. <tr>
  155. <td>+F</td>
  156. <td>build.compiler.fulldepend</td>
  157. <td>false == not set</td>
  158. </tr>
  159. <tr>
  160. <td><strong>only for Ant &lt; 1.4, replaced by the nowarn
  161. attribute of javac after that</strong> -nowarn</td>
  162. <td>build.compiler.warnings</td>
  163. <td>true == not set</td>
  164. </tr>
  165. </table>
  166. </answer>
  167. </faq>
  168. <faq id="shell-redirect-1">
  169. <question>How do I include a &lt; character in my command line arguments?</question>
  170. <answer>
  171. <p>The short answer is "Use <code>&amp;lt;</code>".</p>
  172. <p>The long answer is, that this probably won't do what you
  173. want anyway, see <a href="#shell-redirect-2">the next
  174. section</a>.</p>
  175. </answer>
  176. </faq>
  177. <faq id="shell-redirect-2">
  178. <question>How do I redirect standard input or standard output
  179. in the <code>&lt;exec&gt;</code> task?</question>
  180. <answer>
  181. <p>Say you want to redirect the standard input stream of the
  182. <code>cat</code> command to read from a file, something
  183. like</p>
  184. <source><![CDATA[
  185. shell-prompt> cat < foo
  186. ]]></source>
  187. <p>and try to translate it into</p>
  188. <source><![CDATA[
  189. <exec executable="cat">
  190. <arg value="&lt;" />
  191. <arg value="foo" />
  192. </exec>
  193. ]]></source>
  194. <p>This will not do what you expect. The input-redirection is
  195. performed by your shell, not the command itself, so this
  196. should read:</p>
  197. <source><![CDATA[
  198. <exec executable="/bin/sh">
  199. <arg value="-c" />
  200. <arg value="cat &lt; foo" />
  201. </exec>
  202. ]]></source>
  203. <p>Note, that you must use the <code>value</code> attribute of
  204. <code>&lt;arg&gt;</code> in the last element.</p>
  205. </answer>
  206. </faq>
  207. </faqsection>
  208. <faqsection title="Ant and IDEs/Editors">
  209. <faq id="integration">
  210. <question>Is Ant supported by my IDE/Editor?</question>
  211. <answer>
  212. <p>See the <a href="external.html#IDE and Editor Integration">section
  213. on IDE integration</a> on our external tools page.</p>
  214. </answer>
  215. </faq>
  216. <faq id="emacs-mode">
  217. <question>Why doesn&apos;t (X)Emacs/vi/MacOS X's project builder
  218. parse the error messages generated by Ant correctly?</question>
  219. <answer>
  220. <p>Ant adds a &quot;banner&quot; with the name of the current
  221. task in front of all messages - and there are no built-in
  222. regular expressions in your Editor that would account for
  223. this.</p>
  224. <p>You can disable this banner by invoking Ant with the
  225. <code>-emacs</code> switch. Alternatively you can add the
  226. following snippet to your <code>.emacs</code> to make Emacs
  227. understand Ant&apos;s output.</p>
  228. <source><![CDATA[
  229. (require 'compile)
  230. (setq compilation-error-regexp-alist
  231. (append (list
  232. ;; works for jikes
  233. '("^\\s-*\\[[^]]*\\]\\s-*\\(.+\\):\\([0-9]+\\):\\([0-9]+\\):[0-9]+:[0-9]+:" 1 2 3)
  234. ;; works for javac
  235. '("^\\s-*\\[[^]]*\\]\\s-*\\(.+\\):\\([0-9]+\\):" 1 2))
  236. compilation-error-regexp-alist))
  237. ]]></source>
  238. <p>Yet another alternative that preserves most of Ant's
  239. formatting is to pipe Ant's output through the following Perl
  240. script by Dirk-Willem van Gulik:</p>
  241. <source><![CDATA[
  242. #!/usr/bin/perl
  243. #
  244. # May 2001 dirkx@apache.org - remove any
  245. # [foo] lines from the output; keeping
  246. # spacing more or less there.
  247. #
  248. $|=1;
  249. while(<STDIN>) {
  250. if (s/^(\s+)\[(\w+)\]//) {
  251. if ($2 ne $last) {
  252. print "$1\[$2\]";
  253. $s = ' ' x length($2);
  254. } else {
  255. print "$1 $s ";
  256. };
  257. $last = $2;
  258. };
  259. print;
  260. };
  261. ]]></source>
  262. </answer>
  263. </faq>
  264. </faqsection>
  265. <faqsection title="Advanced issues">
  266. <faq id="dtd">
  267. <question>Is there a DTD that I can use to validate my build
  268. files?</question>
  269. <answer>
  270. <p>An incomplete DTD can be created by the
  271. <code>&lt;antstructure&gt;</code> task - but this one
  272. has a few problems:</p>
  273. <ul>
  274. <li>It doesn&apos;t know about required attributes. Only
  275. manual tweaking of this file can help here.</li>
  276. <li>It is not complete - if you add new tasks via
  277. <code>&lt;taskdef&gt;</code> it won&apos;t know about it. See
  278. <a href="http://www.sdv.fr/pages/casa/html/ant-dtd.en.html">this
  279. page</a> by Michel Casabianca for a solution to this
  280. problem. Note that the DTD you can download at this page
  281. is based on Ant 0.3.1.</li>
  282. <li>It may even be an invalid DTD. As Ant allows tasks
  283. writers to define arbitrary elements, name collisions will
  284. happen quite frequently - if your version of Ant contains
  285. the optional <code>&lt;test&gt;</code> and
  286. <code>&lt;junit&gt;</code> tasks, there are two XML
  287. elements named test (the task and the nested child element
  288. of <code>&lt;junit&gt;</code>) with different attribute
  289. lists. This problem cannot be solved, DTDs don&apos;t give a
  290. syntax rich enough to support this.</li>
  291. </ul>
  292. </answer>
  293. </faq>
  294. <faq id="xml-entity-include">
  295. <question>How do I include an XML snippet in my build file?</question>
  296. <answer>
  297. <p>You can use XML&apos;s way of including external files and let
  298. the parser do the job for Ant:</p>
  299. <source><![CDATA[
  300. <?xml version="1.0"?>
  301. <!DOCTYPE project [
  302. <!ENTITY common SYSTEM "file:./common.xml">
  303. ]>
  304. <project name="test" default="test" basedir=".">
  305. <target name="setup">
  306. ...
  307. </target>
  308. &common;
  309. ...
  310. </project>
  311. ]]></source>
  312. <p>will literally include the contents of <code>common.xml</code> where
  313. you&apos;ve placed the <code>&amp;common;</code> entity.</p>
  314. <p>In combination with a DTD, this would look like this:</p>
  315. <source><![CDATA[
  316. <!DOCTYPE project PUBLIC "-//ANT//DTD project//EN" "file:./ant.dtd" [
  317. <!ENTITY include SYSTEM "file:./header.xml">
  318. ]>
  319. ]]></source>
  320. </answer>
  321. </faq>
  322. <faq id="mail-logger">
  323. <question>How do I send an email with the result of my build
  324. process?</question>
  325. <answer>
  326. <p>You can use a custom BuildListener, that sends out an email
  327. in the buildFinished() method. Will Glozer
  328. &lt;will.glozer@jda.com&gt; has written such a listener based
  329. on <a href="http://java.sun.com/products/javamail/">JavaMail</a>,
  330. the source is</p>
  331. <source><![CDATA[
  332. import java.io.*;
  333. import java.util.*;
  334. import javax.mail.*;
  335. import javax.mail.internet.*;
  336. import org.apache.tools.ant.*;
  337. /**
  338. * A simple listener that waits for a build to finish and sends an email
  339. * of the results. The settings are stored in "monitor.properties" and
  340. * are fairly self explanatory.
  341. *
  342. * @author Will Glozer
  343. * @version 1.05a 09/06/2000
  344. */
  345. public class BuildMonitor implements BuildListener {
  346. protected Properties props;
  347. /**
  348. * Create a new BuildMonitor.
  349. */
  350. public BuildMonitor() throws Exception {
  351. props = new Properties();
  352. InputStream is = getClass().getResourceAsStream("monitor.properties");
  353. props.load(is);
  354. is.close();
  355. }
  356. public void buildStarted(BuildEvent e) {
  357. }
  358. /**
  359. * Determine the status of the build and the actions to follow, now that
  360. * the build has completed.
  361. *
  362. * @param e Event describing the build tatus.
  363. */
  364. public void buildFinished(BuildEvent e) {
  365. Throwable th = e.getException();
  366. String status = (th != null) ? "failed" : "succeeded";
  367. try {
  368. String key = "build." + status;
  369. if (props.getProperty(key + ".notify").equalsIgnoreCase("false")) {
  370. return;
  371. }
  372. Session session = Session.getDefaultInstance(props, null);
  373. MimeMessage message = new MimeMessage(session);
  374. message.addRecipients(Message.RecipientType.TO, parseAddresses(
  375. props.getProperty(key + ".email.to")));
  376. message.setSubject(props.getProperty(key + ".email.subject"));
  377. BufferedReader br = new BufferedReader(new FileReader(
  378. props.getProperty("build.log")));
  379. StringWriter sw = new StringWriter();
  380. String line = br.readLine();
  381. while (line != null) {
  382. sw.write(line);
  383. sw.write("\n");
  384. line = br.readLine();
  385. }
  386. br.close();
  387. message.setText(sw.toString(), "UTF-8");
  388. sw.close();
  389. Transport transport = session.getTransport();
  390. transport.connect();
  391. transport.send(message);
  392. transport.close();
  393. } catch (Exception ex) {
  394. System.out.println("BuildMonitor failed to send email!");
  395. ex.printStackTrace();
  396. }
  397. }
  398. /**
  399. * Parse a comma separated list of internet email addresses.
  400. *
  401. * @param s The list of addresses.
  402. * @return Array of Addresses.
  403. */
  404. protected Address[] parseAddresses(String s) throws Exception {
  405. StringTokenizer st = new StringTokenizer(s, ",");
  406. Address[] addrs = new Address[st.countTokens()];
  407. for (int i = 0; i < addrs.length; i++) {
  408. addrs[i] = new InternetAddress(st.nextToken());
  409. }
  410. return addrs;
  411. }
  412. public void messageLogged(BuildEvent e) {
  413. }
  414. public void targetStarted(BuildEvent e) {
  415. }
  416. public void targetFinished(BuildEvent e) {
  417. }
  418. public void taskStarted(BuildEvent e) {
  419. }
  420. public void taskFinished(BuildEvent e) {
  421. }
  422. }
  423. ]]></source>
  424. <p>With a <code>monitor.properties</code> like this</p>
  425. <source><![CDATA[
  426. # configuration for build monitor
  427. mail.transport.protocol=smtp
  428. mail.smtp.host=<host>
  429. mail.from=Will Glozer <will.glozer@jda.com>
  430. build.log=build.log
  431. build.failed.notify=true
  432. build.failed.email.to=will.glozer@jda.com
  433. build.failed.email.subject=Nightly build failed!
  434. build.succeeded.notify=true
  435. build.succeeded.email.to=will.glozer@jda.com
  436. build.succeeded.email.subject=Nightly build succeeded!
  437. ]]></source>
  438. <p><code>monitor.properties</code> should be placed right next
  439. to your compiled <code>BuildMonitor.class</code>. To use it,
  440. invoke Ant like</p>
  441. <source><![CDATA[
  442. ant -listener BuildMonitor
  443. ]]></source>
  444. <p>Make sure that <code>mail.jar</code> from JavaMail and
  445. <code>activation.jar</code> from the
  446. <a href="http://java.sun.com/products/javabeans/glasgow/jaf.html">Java
  447. Beans Activation Framework</a> in your <code>CLASSPATH</code>.</p>
  448. </answer>
  449. </faq>
  450. </faqsection>
  451. <faqsection title="Known problems">
  452. <faq id="remove-cr">
  453. <question>&lt;chmod&gt; or &lt;exec&gt; don&apos;t work in Ant
  454. 1.3 on Unix</question>
  455. <answer>
  456. <p>The <code>antRun</code> script in <code>ANT_HOME/bin</code>
  457. has DOS instead of Unix line endings, you must remove the
  458. carriage return characters from this file. This can be done by
  459. using Ant&apos;s &lt;fixcrlf&gt; task or something like:</p>
  460. <source><![CDATA[
  461. tr -d '\r' < $ANT_HOME/bin/antRun > /tmp/foo
  462. mv /tmp/foo $ANT_HOME/bin/antRun
  463. ]]></source>
  464. </answer>
  465. </faq>
  466. <faq id="javadoc-cannot-execute">
  467. <question>JavaDoc failed: java.io.IOException: javadoc: cannot execute</question>
  468. <answer>
  469. <p>There is a bug in the Solaris reference implementation of
  470. 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>.
  471. This also appears to be true under Linux, moving the JDK to
  472. the front of the PATH fixes the problem.</p>
  473. </answer>
  474. </faq>
  475. </faqsection>
  476. </document>