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.

ant_in_anger.html 30 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  1. <head>
  2. <title>
  3. Ant in Anger
  4. </title>
  5. </head>
  6. <body bgcolor="#FFFFFF" text="#000000">
  7. <h1 align="center">Ant in Anger:
  8. </h1>
  9. <h2 align="center">
  10. Using Ant in a Production Development System
  11. </h2>
  12. <h4 align="center">
  13. Steve Loughran (<a href="mailto:steve_l@iseran.com">steve_l@iseran.com</a>)
  14. </h4>
  15. <a name="introduction">
  16. <h2>Introduction</h2>
  17. </a>
  18. <a href="http://jakarta.apache.org/ant/">Ant</a>
  19. can be an invaluable tool in a team development process -or it can
  20. be yet another source of problems in that ongoing crises we call
  21. development . This
  22. document contains some strategies and tactics for making the most of
  23. ant. It is moderately frivolous in places, and lacks almost any actual
  24. examples of ant xml. The lack of examples is entirely deliberate -it
  25. keeps document maintenance costs down. Most of the concepts covered
  26. don't need the detail about XML representations, as it is processes we
  27. are concerned about, not syntax. Finally, please be aware that the
  28. comments here are only suggestions which need to be customised to meet
  29. your own needs, not strict rules about what should and should not be
  30. done.
  31. <p>
  32. Firstly, here are some assumptions about the projects which this
  33. document covers
  34. <ul>
  35. <li> Pretty much pure Java.
  36. <li> Team efforts, usually with the petulant prima-donnas all us Java
  37. programmers become once we realise how much in demand we are.
  38. <li> A fairly distributed development team -spread across locations and
  39. maybe time zones.
  40. <li> Separate sub projects -from separate beans in a big
  41. enterprise application to separate enterprise applications which need to
  42. be vaguely aware of each other.
  43. <li> Significant mismatch between expectations and time available to
  44. deliver. 'Last Week' is the ideal delivery date handed down from above,
  45. next century the date coming up from below.
  46. <li> Everyone is struggling to keep up with platform and tool evolution.
  47. <li> Extensive use of external libraries, both open and closed source.
  48. </ul>
  49. What that all means is that there is no time to spend getting things
  50. right, you don't have that tight control on how the rest of the team
  51. works and the development process is often more one of chaos minimisation
  52. than anything else. The role of ant in such projects is to ensure that
  53. the build, test and deploy processes run smoothly, leaving you with all
  54. the other problems.
  55. <a name="core">
  56. <h2>Core Practices</h2>
  57. </a>
  58. <h3>
  59. Clarify what you want ant to do</h3>
  60. Ant is not a silver bullet. It is just another rusty bullet in the armory of
  61. development tools available at your disposal. Its primary purpose is to
  62. accelerate the construction and deployment of Java projects. You could certainly
  63. extend ant to do anything Java makes possible -it is easy to imagine writing an
  64. image processing task to help in web site deployment by shrinking and
  65. recompressing jpeg files, for example. But that would be pushing the boundary of
  66. what ant is really intended to do -so should be considered with care.
  67. <P>
  68. Ant is also a great adjunct to an IDE -a way of doing all the housekeeping of
  69. deployment and for clean, automated builds. But a good modern IDE is a
  70. productivity tool in its own right -one you should consider keeping using. Ant
  71. just lets you give the teams somewhat more freedom in IDE choice -&quot;you can
  72. use whatever you want in development, but ant for the deployment
  73. builds&quot;
  74. <h3>
  75. Define standard targets
  76. </h3>
  77. When you have multiple sub projects, define a standard set of targets.
  78. Projects with a split between interface and implementation jar files
  79. could consider <b>impl</b> and <b>intf</b> targets -with separate
  80. <b>debug-impl</b>and <b>debug-intf</b> targets for the debug version.
  81. And of course, the ubiquitous <b>clean</b> target.
  82. <P>
  83. With standard target names, it is easy to build encompassing ant build
  84. files which just hand off the work to the classes below using the
  85. <a href="manual/CoreTasks/ant.html">ant</a>
  86. task. For example. the clean target could be handed down to the intf and
  87. impl subdirectories from a parent directory
  88. <pre>&lt;target name=&quot;clean&quot; depends=&quot;clean-intf, clean-impl&quot;&gt;
  89. &lt;/target&gt;
  90. &lt;target name=&quot;clean-intf&quot; &gt;
  91. &lt;ant dir=&quot;intf&quot; target=&quot;clean&quot; /&gt;
  92. &lt;/target&gt;
  93. &lt;target name=&quot;clean-impl&quot;&gt;
  94. &lt;ant dir=&quot;impl&quot; target=&quot;clean&quot; /&gt;
  95. &lt;/target&gt; </pre>
  96. <h3>
  97. Extend ant through new tasks
  98. </h3>
  99. If ant does not do what you want, you can use the
  100. <a href="manual/CoreTasks/exec.html">exec</a> and
  101. <a href="manual/CoreTasks/java.html">java</a> tasks or
  102. <a href="manual/OptionalTasks/script.html">inline scripting</a> to extend it. In a
  103. project with many build.xml files, you soon find that having a single
  104. central place for implementing the functionality keeps maintenance
  105. overhead down. Implementing task extensions through java code seems
  106. extra effort at first, but gives extra benefits:-
  107. <ul>
  108. <li>Cross platform support can be added later without changing any
  109. build.xml files</li>
  110. <li>The code can be submitted to the ant project itself, for other
  111. people to use and maintain</li>
  112. <li>It keeps the build files simpler</li>
  113. </ul>
  114. <h3>
  115. Embrace Automated Testing
  116. </h3>
  117. <b>(alternatively "recriminate early, recriminate often")</b>
  118. <p>
  119. Ant lets you call <a href="manual/OptionalTasks/junit.html">JUnit</a> tasks, which unit test
  120. the code your team has written. Automated testing may seem like extra
  121. work at first, but JUnit makes writing unit tests so easy that you have
  122. almost no reason not to. Invest the time in learning how to use
  123. JUnit, write the test cases, and integrate them in a 'test' target from
  124. ant so that your daily or hourly team build can have the tests applied
  125. automatically.
  126. <p>
  127. Once code fetches from the code control system are added as another ant
  128. target, the integration test code can be a pure ant task run on any box
  129. dedicated to the task. This is ideal for verifying that the build and
  130. unit tests work on different targets from the usual development
  131. machines. For example, a Win95/Java1.1 combination could be used even
  132. though no developer would willingly use that configuration given the
  133. choice.
  134. <p>
  135. System tests are harder to automate than unit tests, but if you can
  136. write java code to stress large portions of the system -even if the code
  137. can not run as JUnit tasks- then the <a href= "manual/CoreTasks/java.html">java</a>
  138. task can be used to invoke them. It is best to specify that you want a
  139. new JVM for these tests, so that a significant crash does not break the
  140. full build.
  141. <a name="crossplatform">
  142. <h2>
  143. Cross Platform Ant
  144. </h2>
  145. </a>
  146. Ant is the best foundation for cross platform Java development and
  147. testing to date. But if you are not paying attention, it is possible to
  148. produce build files which only work on one platform -or indeed, one
  149. single workstation.
  150. <p>
  151. The common barriers to cross-platform ant are the use of command line
  152. tools (exec tasks) which are not portable, path issues, and hard coding
  153. in the location of things.
  154. <h3>Command Line apps: <a href="manual/CoreTasks/exec.html">Exec</a>/ <a href=
  155. "manual/CoreTasks/execon.html">ExecOn</a></h3>
  156. The trouble with external invocation is that not all functions are found
  157. cross platform, and those that are often have different names -DOS
  158. descendants often expect .exe or .bat at the end of files. That can be
  159. bad if you explicitly include the extension in the naming of the command
  160. (don't!), good when it lets you keep the unix and DOS versions of an
  161. executable in the same bin directory of the project without name
  162. clashes arising.
  163. <p>
  164. Both the command line invocation tasks let you specify which platform
  165. you want the code to run on, so you could write different tasks for each
  166. platform you are targeting. Alternatively, the platform differences
  167. could be handled inside some external code which ant calls. This can be
  168. some compiled down java in a new task, or an external script file.
  169. <h3>Cross platform paths</h3>
  170. Unix paths use forward slashes between directories and a colon to
  171. split entries. Thus
  172. <i>"/bin/java/lib/xerces.jar:/bin/java/lib/ant.jar"</i> is
  173. a path in unix. In Windows the path must use semicolon separators,
  174. colons being used to specify disk drives, and backslash separators
  175. <i>"c:\bin\java\lib\xerces.jar;c:\bin\java\lib\ant.jar"</i>.
  176. This difference between platforms (indeed, the whole java classpath
  177. paradigm) can cause hours of fun.
  178. <p>
  179. Ant reduces path problems; but does not eliminate them entirely. You
  180. need to put in some effort too. The rules for handling path names are
  181. that 'DOS-like pathnames are handled', 'Unix like paths are handled'.
  182. Disk drives -'C:'- are handled on DOS-based boxes, but placing them in
  183. the build.xml file ruins all chances of portability. Relative file paths
  184. are much more portable. Semicolons work as path separators -a fact which
  185. is useful if your ant invocation wrapper includes a list of jars as a
  186. defined property in the command line. In the build files you may find it
  187. better to build a classpath by listing individual files (using location=
  188. attributes), or by including a fileset of *.jar in the classpath
  189. definition.
  190. <p>
  191. Note that DOS descended file systems are case insensitive (apart from
  192. the obscure aberration of the WinNT posix subsystem run against NTFS),
  193. and that Windows pretends that all file extensions with four or more
  194. letters are also three letter extensions (try DELETE *.jav in your java
  195. directories to see a disastrous example of this).
  196. <p>
  197. Ant's policy on case sensitivity is whatever the underlying file system
  198. implements *VERIFY*, and its handling of file extensions is that *.jav does not
  199. find any .java files. The Java compiler is of course case sensitive -you can
  200. not have a class 'ExampleThree' implemented in "examplethree.java".
  201. <p>
  202. Some tasks only work on one platform -<a href= "manual/CoreTasks/chmod.html">
  203. Chmod</a> being a classic example. These tasks usually result in just a
  204. warning message on an unsupported platform -the rest of the target's
  205. tasks will still be called. Other tasks degrade their functionality on
  206. platforms or Java versions. In particular, any task which adjusts the
  207. timestamp of files can not do so properly on Java 1.1. Tasks which can
  208. do that - <a href="manual/CoreTasks/get.html">Get<a>, <a
  209. href="manual/CoreTasks/touch.html">Touch</a> and <A href="manual/CoreTasks/unzip.html">
  210. Unjar/Unwar/Unzip</a> for example, degrade their functionality on
  211. Java1.1, usually resorting to the current timestamp instead.
  212. <p>
  213. Finally, Perl makes a good place to wrap up Java invocations cross
  214. platform, rather than batch files. It is included in most Unix
  215. distributions, and is a simple download for <a href=
  216. "http://www.activestate.com/Products/ActivePerl/"> Win32 platforms from
  217. ActiveState</a>. A Perl file with .pl extension, with the usual Unix
  218. path to perl on the line 1 comment and marked as executable can be run
  219. on Windows, OS/2 and Unix and hence called from Ant without issues. The
  220. perl code can be left to resolve its own platform issues.
  221. <a name="team">
  222. <h2>Team Development Processes</h2>
  223. </a>
  224. Even if each team member is allowed their choice of IDE/editor, or even
  225. OS, you need to set a baseline of functionality on each box. In
  226. particular, the JDKs and jars need to be in perfect sync. Ideally pick
  227. the latest stable Java/JDK version available on all developer/target
  228. systems and stick with it for a while. Consider assigning one person to
  229. be the contact point for all tools coming in -particularly open source
  230. tools when a new build is available on a nightly basis. Unless needed,
  231. these tools should only really be updated monthly, or when a formal
  232. release is made.
  233. <p>
  234. Another good tactic is to use a unified directory tree, and add on extra
  235. tools inside that tree. All references can be made relative to the tree.
  236. If team members are expected to add a directory in the project to their
  237. path, then command line tools can be included there -including those
  238. invoked by ant exec tasks. Put everything under source code control and
  239. you have a one stop shop for getting a build/execute environment purely
  240. from CVS or your equivalent.
  241. <a name="deploying">
  242. <h2>Deploying with Ant</h2>
  243. </a>
  244. One big difference between ant and older tools such as make is that the
  245. processes for deploying java to remote sites are reasonably well
  246. evolved in ant. That is because we all have to do it these days, so
  247. many people have put in the effort to make the tasks easier.
  248. <p>
  249. Ant can <a href="manual/CoreTasks/jar.html">Jar</a>, <a href= "manual/CoreTasks/tar.html">
  250. Tar</a> or <a href="manual/CoreTasks/zip.html">Zip</a> files for deployment, while
  251. the <a href="manual/CoreTasks/war.html">War</a> task extends the jar task for
  252. better servlet deployment. <a href = "jlink.html" >Jlink</a> is a jar
  253. generation file which lets you merge multiple sub jars. This is ideal
  254. for a build process in which separate jars are generated by sub
  255. projects, yet the final output is a merged jar. <a href=
  256. "manual/OptionalTasks/cab.html">Cab</a> can be used on Win32 boxes to build a cab file
  257. which is useful if you have to target IE deployment.
  258. <p>
  259. The <a href = "index.html#ftp">ftp</a> task lets you move stuff up to a
  260. server. Beware of putting the ftp password in the build file -a property
  261. file with tight access control is slightly better. The <a href=
  262. "manual/CoreTasks/fixcrlf.html">FixCRLF task</a> is often a useful interim step if
  263. you need to ensure that files have unix file extensions before upload. A
  264. WebDav task has long been discussed, which would provide a more secure
  265. upload to web servers, but it is still in the todo list. If DAV is your
  266. required upload mechanism, why not take up the challenge?
  267. <p>
  268. EJB deployment is aided by the <a href="manual/OptionalTasks/ejb.html">ejb tasks</a>. At the
  269. time of writing, only WebLogic was supported with these tasks -if your
  270. EJB server is not supported, extending the ejb tasks would benefit your
  271. project and the rest of the ant community.
  272. <p>
  273. Finally, there are of course the fallbacks of just copying files to a
  274. destination using <a href="manual/CoreTasks/copy.html">Copy</a> and <a href =
  275. "index.html#copydir">Copydir</a> , or just sending them to a person or
  276. process using <a href= "manual/CoreTasks/mail.html">Mail</a>.
  277. <a name="directories">
  278. <h2> Directory Structures</h2>
  279. </a>
  280. How you structure your directory tree is very dependent upon the
  281. project. Here are some directory layout patterns which can be used as
  282. starting points.
  283. <h3>Simple Project</h3>
  284. The project contains sub directories
  285. <table width="100%">
  286. <tr>
  287. <td><b>bin</b>
  288. </td>
  289. <td>common binaries, scripts -put this on the path.
  290. </td>
  291. </tr>
  292. <tr>
  293. <td><b>build</b>
  294. </td>
  295. <td>This is the tree for building; ant creates it and can empty it
  296. in the 'clean' project.
  297. </td>
  298. </tr>
  299. <tr>
  300. <td><b>dist</b>
  301. </td>
  302. <td>Distribution outputs go in here; the directory is created in ant
  303. and clean empties it out
  304. </td>
  305. </tr>
  306. <tr>
  307. <td><b>doc</b>
  308. </td>
  309. <td>Hand crafted documentation
  310. </td>
  311. </tr>
  312. <tr>
  313. <td><b>lib</b>
  314. </td>
  315. <td>Imported Java libraries go in to this directory
  316. </td>
  317. </tr>
  318. <tr>
  319. <td><b>src</b>
  320. </td>
  321. <td>source goes in under this tree
  322. </td>
  323. </tr>
  324. </table>
  325. The bin, lib, doc and src directories should be under source code control.
  326. Slight variations include an extra tree of content to be included in the
  327. distribution jars -inf files, images, etc. Javadoc output can be
  328. directed to a doc/ folder beneath build/, or to doc/javadoc.
  329. <h3>Interface and Implementation split</h3>
  330. If the interface is split from the implementation code then this can be
  331. supported with minor changes just by having a separate build path for
  332. the interface directory -or better still just in the jar construction:
  333. one jar for interface and one jar for implementation.
  334. <h3>Loosely Coupled Sub Projects</h3>
  335. In the loosely coupled approach multiple projects can have their own
  336. copy of the tree, with their own source code access rights.
  337. One difference to consider is only having one instance of the bin and
  338. lib directories across all projects. This is sometimes good -it helps
  339. keep copies of xerces.jar in sync, and sometimes bad -it can update
  340. foundational jar files before unit testing is complete.
  341. <p>
  342. To still have a single build across the sub projects, use parent
  343. build.xml files which call down into the sub projects.
  344. <p>
  345. This style works well if different teams have different code
  346. access/commitment rights. The risk is that by giving extra leeway to the
  347. sub projects, you can end up with incompatible source, libraries, build
  348. processes and just increase your workload and integration grief all round.
  349. <h3>Integrated sub projects</h3>
  350. Tightly coupled projects have all the source in the same tree; different
  351. projects own different subdirectories. Build files can be moved down to
  352. those subdirectores (say src/com/iseran/core and src/com/iseran/extras),
  353. or kept at the top -with independent build files named core.xml and
  354. extras.xml
  355. <p>
  356. This project style works well if everyone trusts each other and the
  357. sub projects are not too huge or complex. The risk is that a split to a
  358. more loosely coupled design will become a requirement as the projects
  359. progress -but by the time this is realised schedule pressure and
  360. intertwined build files make executing the split well nigh impossible.
  361. If that happens then just keep with it until there is the time to
  362. refactor the project directory structures.
  363. <a name="antupdate">
  364. <h2>
  365. Ant Update Policies
  366. </h2>
  367. </a>
  368. Once you start using ant, you should have a policy on when and how the
  369. team updates their copies. A simple policy is "every official release
  370. after whatever high stress milestone has pushed all unimportant tasks
  371. (like sleep and seeing daylight) on the back burner". This insulates you
  372. from the changes and occasional instabilities that ant goes through
  373. during development. Its main disadvantage is that it isolates you from
  374. the new tasks and features that ant is constantly adding.
  375. <p>
  376. Often an update will require changes to the build.xml files. Most
  377. changes are intended to be backwards compatible, but sometimes an
  378. incompatible change turns out to be
  379. necessary. That is why doing the update in the lull after a big
  380. milestone is important. It is also why including ant.jar and related
  381. files in the CVS tree helps ensure that old versions of your software
  382. can be still be built.
  383. <p>
  384. The most aggressive strategy is to get a weekly or daily snapshot of the
  385. ant source, build it up and use it. This forces you to tweak the
  386. build.xml files more regulary, as new tasks and attributes can take
  387. while to stabilise. You really have to want the new features, enjoy
  388. gratuitous extra work or take pleasure in upsetting your colleagues to
  389. take this approach.
  390. <p>
  391. Once you start extending ant with new tasks, it suddenly becomes much
  392. more tempting to pull down regular builds. The most recent ant builds
  393. are invariably the the best platform for writing your extensions, as you
  394. can take advantage of the regular enhancements to the foundational
  395. classes. It also prevents you from wasting time working on something
  396. which has already been done. A newly submitted task to do something
  397. complex such as talk to EJB engines, SOAP servers or just convert a text
  398. file to uppercase may be almost exactly what you need -so take it,
  399. enhance it and offer up the enhancements to the rest of the world. This
  400. is certainly better than starting work on your 'text case converter'
  401. task on Ant 0.8 in isolation, announcing its existence six months latter
  402. and discovering that instead of adulation all you get are helpful
  403. pointers to the existing implementation.
  404. <p>
  405. You should also get on the <a href =
  406. "mailto:ant-dev-subscribe@jakarta.apache.org" > ant-dev mailing list
  407. </a>, as it is where the other developers post their work, problems and
  408. experience. The volume can be quite high: 40+ messages a day, so
  409. consider routing it to an email address you don't use for much else. And
  410. don't make everyone on the team subscribe; it can be too much of a
  411. distraction.
  412. <a name="tips">
  413. <h2>
  414. Tips and Tricks</h2>
  415. </a>
  416. <dl>
  417. <dt><b>
  418. get
  419. </b><dd>
  420. The <a href="manual/CoreTasks/get.html">get</a> task can fetch any URL, so be used
  421. to trigger remote server side code during the build process, from remote
  422. server restarts to sending SMS/pager messages to the developer
  423. cellphones
  424. <dt><b>
  425. i18n
  426. </b><dd>
  427. Internationalisation is always trouble. Ant helps here with the <A href=
  428. "manual/OptionalTasks/native2ascii.html">native2ascii</a> task which can escape out all non
  429. ascii characters into unicode. You can use this to write java files
  430. which include strings (and indeed comments) in your own non-ASCII
  431. language and then use native2ascii to convert to ascii prior to feeding
  432. through javac. The rest of i18n and l12n is left to you...
  433. <dt><b>
  434. Use Property Files
  435. </b><dd>
  436. Use external property files to keep per-user settings out the build
  437. files -especially passwords. Property files can also be used to
  438. dynamically set a number of properties based on the value of a single
  439. property, simply by dyamically generating the property filename from the
  440. source property. They can also be used as a source of constants across
  441. multiple build files.
  442. <dt><b>
  443. Faster compiles with Jikes
  444. </b><dd>
  445. The <a href="http://www.jikes.org/">jikes compiler</a> is usually much
  446. faster than javac, and does dependency checking. Get it. Then set
  447. build.compiler to "jikes" for it to be used in your build files.
  448. <dt><b>
  449. #include targets to simplify multi build.xml projects
  450. </b><dd>
  451. You can import XML files into a build file using the XML parser itself.
  452. This lets a multi-project development program share code through reference,
  453. rather than cut and paste re-use. It also lets one build up a file of
  454. standard tasks which can be reused over time. Because the import
  455. mechanism is at a level below which ant is aware, treat it as
  456. equivalent to the #include mechanism of the 'legacy' languages C and
  457. C++.
  458. <p>
  459. There are two inclusion mechanisms, an ugly one for all parsers and a
  460. clean one. For now, the ugly
  461. method is the most portable:-
  462. <pre>
  463. &lt;!DOCTYPE project [
  464. &lt;!ENTITY IncludeBuildCore SYSTEM &quot;buildCore.xml&quot;&gt;
  465. &lt;!ENTITY IncludeBuildSecondary SYSTEM &quot;buildSecondary.xml&quot;&gt;
  466. %IncludeBuildCore;
  467. %IncludeBuildSecondary;
  468. ]&gt;
  469. &lt;target name=&quot;includedBuild&quot;&gt;
  470. &amp;IncludeBuildCore;
  471. &amp;IncludeBuildSecondary;
  472. &lt;/target&gt;
  473. </pre>
  474. The clean method using XInclude/Xpath will let you include named
  475. targets from one build file or another, using
  476. <a href="http://www.w3.org/XML/Linking">
  477. the xpointer syntax</a>. You'll need to wait for the W3C proposals
  478. to finalise and the java XML parsers to implement it before
  479. using xpointer references.
  480. <dt><b>
  481. Implement complex Ant builds through XSL
  482. </b><dd>
  483. XSLT can be used to dynamically generate build.xml files from a source
  484. xml file, with the <a href="manual/CoreTasks/style.html">Style</a> task controlling
  485. the transform. This is the current recommended strategy for creating
  486. complex build files dynamically.
  487. <dt><b>
  488. Change the invocation scripts
  489. </b><dd>
  490. By writing your own invocation script -using the DOS, Unix or Perl
  491. script as a starting point- you can modify a ant behavior for an
  492. individual project. For example, you can use an alternate variable to
  493. ANT_HOME as the base, extend the classpath differently, or dynamically
  494. create a new command line property 'project.interfaces' from all .jar
  495. files in an interfaces directory.
  496. <p>
  497. Having a custom invocation script which runs off a CVS controlled
  498. library tree under PROJECT_HOME also lets you control ant versions
  499. across the team -developers can have other copies of ant if they want,
  500. but the CVS tree always contains the jar set used to build your project.
  501. <p>
  502. You can also write wrapper scripts which invoke the existing ant
  503. scripts. This is an easy way to extend them. The wrapper scripts can add
  504. extra definitions and name explicit targets, redefine ANT_HOME and
  505. generally make development easier. Note that "ant" in Windows is really
  506. "ant.bat", so should be invoked from another batch file with a "CALL
  507. ant" statement -otherwise it never returns to your wrapper.
  508. <dt><b>
  509. Write all code so that it can be called from Ant
  510. </b><dd>
  511. This seems a bit strange and idealistic, but what it means is that you should
  512. write all your java code as if it may be called as a library at some point in
  513. future. So do not place calls to <b>System.exit()</b> deep in the code -if you
  514. want to exit a few functions in, raise an exception instead and have
  515. <b>main()</b> deal with it.
  516. <dt><b>
  517. Use Antidote as the invocation tool
  518. </b><dd>
  519. Even if you edit ant files by hand, Antidote makes a good execution tool
  520. because it eliminates the startup time of the JVM, perhaps even some of
  521. the XML parsing delays.
  522. </dl>
  523. <a name="puttingtogether">
  524. <h2>
  525. Putting it all together
  526. </h2>
  527. </a>
  528. What does an ant build process look like in this world? Assuming a
  529. single directory structure for simplicity, the build file
  530. should contain a number of top level targets
  531. <ul>
  532. <li>build - do an (incremental) build
  533. <li>test - run the junit tests
  534. <li>clean - clean out the output directories
  535. <li>deploy - ship the jars, wars, whatever to the execution system
  536. <li>publish - output the source and binaries to any distribution site
  537. <li>fetch - get the latest source from the cvs tree
  538. <li>docs/javadocs - do the documenation
  539. <li>all - clean, fetch, build, test, docs, deploy
  540. <li>main - the default build process (usually build or build & test)
  541. </ul>
  542. Sub projects 'web', 'bean-1', 'bean-2' can be given their own build
  543. files -web.xml, bean-1.xml, bean-2.xml- with the same entry points.
  544. Extra toplevel tasks related to databases, web site images and the like
  545. should be considered if they are part of the process.
  546. <p>
  547. Debug/release switching can be handled with separate initialisation
  548. targets called before the compile tasks which define the appropriate
  549. properties.
  550. <p>
  551. Internal targets should be used to structure the process
  552. <ul>
  553. <li> init - initialise properties, extra-tasks, read in per-user
  554. property files.
  555. <li> init-debug - initialise debug properties
  556. <li> init-release - initialise release properties
  557. <li> compile - do the actual compilation
  558. <li> link/jar - make the jars or equivalent
  559. <li> staging - any pre-deployment process in which the output is dropped
  560. off then tested before being moved to the production site.
  561. </ul>
  562. The switching between debug and release can be done using the 'if' and
  563. 'unless' conditional flags on the targets, so that debug gets called
  564. unless 'project.mode.release' is defined.
  565. <p>
  566. It is useful to define a project name property which can be echoed in
  567. the init task. This lets you work out which ant file is breaking in a
  568. multi file build.
  569. <p>
  570. What goes in to the internal ant tasks depends on your own projects. One
  571. very important tactic is 'keep path redefinition down through
  572. references' - you can reuse paths by giving them an ID and then
  573. referring to them via the 'refid' attribute you should only need to
  574. define a shared classpath once in the file; filesets can be reused
  575. similarly.
  576. <p>
  577. Once you have set up the directory structures, and defined the ant tasks
  578. it is time to start coding. An early priority must be to set up the
  579. automated test process, as that not only helps ensures that the code
  580. works, it verifies that the build process is working.
  581. <p>
  582. And that's it. The build file shouldn't need changing as new source
  583. files get added, only when you want to change the deliverables or part
  584. of the build process. At some point you may want to massively
  585. restructure the entire build process, restructuring projects and the
  586. like, but even then the build file you have should act as a foundation
  587. for a split build file process -just pull out the common properties into
  588. a properties file all build files read in, keep the target names unified
  589. and keep going with the project. Restructuring the source code control
  590. system is often much harder work.
  591. <h2>The Limits of Ant</h2>
  592. Before you start adopting ant as the sole mechanism for the build
  593. process, you need to be aware of what it doesn't do.
  594. <p>
  595. <h3>It's not a scripting language</h3>
  596. Ant lets you declare what you want done, with a bit of testing of the
  597. platform and class libraries first to enable some platform specific
  598. builds to take place. It does not let you specify how to handle things
  599. going wrong (a listener class can do that), or support complex
  600. conditional statements.
  601. <p>
  602. If your build needs to handle exceptions then look at the sound listener
  603. as a simple example of how to write your own listener class. Complex
  604. conditional statements can be handled by having something else do the
  605. tests and then build the appropriate ant task. XSLT can be used for
  606. this.
  607. <h3>It's not Make</h3>
  608. Some of the features of make, specifically inference rules and
  609. dependency checking are not included in ant. That's because they are
  610. 'different' ways of doing a build. Make requires you to state
  611. dependencies and the build steps, ant wants you to state tasks and the
  612. order between them, the tasks themselves can do depedency checking or
  613. not. A full java build using Jikes is so fast that dependency checking
  614. is relatively moot, while many of the other tasks (but not all), compare
  615. the timestamp of the source file with that of the destination file
  616. before acting.
  617. <h3>It's not meant to be a nice language for humans</h3>
  618. XML isnt a nice representation of information for humans. It's a
  619. reasonable representation for programs, and text editors and source code
  620. management systems can all handle it nicely. But a complex ant file can
  621. get ugly because XML is a bit ugly, and a complex build is, well,
  622. complicated. Use XML comments so that the file you wrote last month
  623. still makes sense when you get back to it, and use Antidote to edit the
  624. files if you prefer it.
  625. <h2>Endpiece</h2>
  626. Software development is meant to be fun. Being in the maelstrom of a
  627. tight project with the stress of integration and trying to code
  628. everything up for an insane deadline can be fun -it is certainly
  629. exhilirating. Adding a bit of automation to the process may make things
  630. less chaotic, and bit less entertaining, but it is a start to putting
  631. you in control of your development process. You can still have fun, you
  632. should just have less to worry about, a shorter build/test/deploy cycle
  633. and more time to spend on feature creep or important things like skiing.
  634. So get out there and have fun!
  635. <a name="reading">
  636. <h2>Further Reading</h2>
  637. </a>
  638. <ul>
  639. <li>
  640. <a
  641. href="http://www.martinfowler.com/articles/continuousIntegration.html">
  642. Continuous Integration</a>; Martin Fowler. <br>
  643. A paper on using ant within a software project
  644. running a continuous integration/testing proces.
  645. <li> Refactoring; Martin Fowler, ISBN: 0201485672 <br>
  646. Covers JUnit as well as tactics for making some headway with the mess of
  647. code you will soon have.
  648. </ul>
  649. <hr>
  650. <p align="center">Copyright &copy; 2000 Apache Software Foundation. All rights
  651. Reserved.</p>