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

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