diff --git a/docs/ant_in_anger.html b/docs/ant_in_anger.html index 7cee64c92..a821f29bd 100644 --- a/docs/ant_in_anger.html +++ b/docs/ant_in_anger.html @@ -1,6 +1,6 @@
Firstly, here are some assumptions about the projects which this -document covers +document covers:
-Ant is also a great adjunct to an IDE -a way of doing all the housekeeping of +Ant is also a great adjunct to an IDE; a way of doing all the housekeeping of deployment and for clean, automated builds. But a good modern IDE is a productivity tool in its own right -one you should consider keeping using. Ant just lets you give the teams somewhat more freedom in IDE choice -"you can -use whatever you want in development, but ant for the deployment -builds" +use whatever you want in development, but Ant for the deployment +builds" Now that many modern open source and commercial IDEs +include Ant support (including jEdit, Forte, Eclipse and IDEA), +developers can use a great IDE, with Ant providing a rigorous and portable +build process integrated into the tool.
-With standard target names, it is easy to build encompassing ant build
+With standard target names, it is easy to build encompassing Ant build
files which just hand off the work to the classes below using the
-ant
+<ant>
task. For example. the clean target could be handed down to the intf and
impl subdirectories from a parent directory
@@ -114,11 +118,11 @@ task. For example. the clean target could be handed down to the intf an
</target>
<target name="clean-intf" >
- <ant dir="intf" target="clean" />
+ <ant dir="intf" target="clean" />
</target>
<target name="clean-impl">
- <ant dir="impl" target="clean" />
+ <ant dir="impl" target="clean" />
</target>
If you give targets a description tag, then calling ant
@@ -127,30 +131,39 @@ all tasks without a description as subtargets. Describing all your
entry points is therefore very useful, even before a project becomes big and complicated.
-Ant lets you call JUnit tasks, which unit test
-the code your team has written. Automated testing may seem like extra
-work at first, but JUnit makes writing unit tests so easy that you have
-almost no reason not to. Invest the time in learning how to use
-JUnit, write the test cases, and integrate them in a 'test' target from
-ant so that your daily or hourly team build can have the tests applied
-automatically.
+Ant lets you call JUnit
+tasks, which unit test the code your team has written. Automated testing
+may seem like extra work at first, but JUnit makes writing unit tests so
+easy that you have almost no reason not to. Invest the time in learning
+how to use JUnit, write the test cases, and integrate them in a 'test'
+target from Ant so that your daily or hourly team build can have the
+tests applied automatically. One of the free to download chapters of
+Java Development with Ant
+shows you how to use JUnit from inside Ant.
-Once code fetches from the code control system are added as another ant
-target, the integration test code can be a pure ant task run on any box
-dedicated to the task. This is ideal for verifying that the build and
-unit tests work on different targets from the usual development
-machines. For example, a Win95/Java1.1 combination could be used even
-though no developer would willingly use that configuration given the
-choice.
+
+Once you add a way to fetch code from the SCM system, either as an Ant
+task, in some shell script or batch file or via some continuous
+integration tool. the integration test code can be a pure Ant task run
+on any box dedicated to the task. This is ideal for verifying that the
+build and unit tests work on different targets from the usual
+development machines. For example, a Win95/Java1.1 combination could be
+used even though no developer would willingly use that configuration
+given the choice.
@@ -192,6 +209,43 @@ deriving great unit, system and regression tests -but your customers will love
you for shipping software that works.
+
-The common barriers to cross-platform ant are the use of command line
+The common barriers to cross-platform Ant are the use of command line
tools (exec tasks) which are not portable, path issues, and hard coding
in the location of things.
-
-Ant can Jar,
-Tar or Zip files for deployment, while
-the War task extends the jar task for
-better servlet deployment. Jlink is a jar
-generation file which lets you merge multiple sub jars. This is ideal
-for a build process in which separate jars are generated by sub
+Ant can Jar, Tar or Zip files for deployment, while the
+War task extends the jar task
+for better servlet deployment. Jlink is a
+jar generation file which lets you merge multiple sub jars. This is
+ideal for a build process in which separate jars are generated by sub
projects, yet the final output is a merged jar. Cab can be used on Win32 boxes to build a cab file
-which is useful if you have to target IE deployment.
+"manual/OptionalTasks/cab.html">Cab can be used on Win32 boxes to
+build a cab file which is useful if you still have to target IE deployment.
@@ -350,17 +409,24 @@ The ftp task lets you move stuff up to a
server. Beware of putting the ftp password in the build file -a property
file with tight access control is slightly better. The FixCRLF task is often a useful interim step if
-you need to ensure that files have unix file extensions before upload. A
+you need to ensure that files have Unix file extensions before upload. A
WebDav task has long been discussed, which would provide a more secure
upload to web servers, but it is still in the todo list. Rumour has it
-that there is such a task in the jakarta-slide libraries.
+that there is such a task in the jakarta-slide libraries. With MacOS X,
+Linux and Windows XP all supporting WebDAV file systems, you may even be able
+to use <copy> to deploy
+though a firewall.
-EJB deployment is aided by the ejb tasks. At the
-time of writing, only WebLogic was supported with these tasks -if your
-EJB server is not supported, extending the ejb tasks would benefit your
-project and the rest of the ant community.
+EJB deployment is aided by the ejb tasks,
+while the
+<serverdeploy>
+suite can deploy to multiple servers. The popularity of Ant has
+encouraged vendors to produce their own deployment tasks which they
+redistribute with their servers. For example, the Tomcat4.1 installation
+includes tasks to deploy, undeploy and reload web applications.
@@ -369,8 +435,13 @@ destination using Copy and Copydir , or just sending them to a person or
process using Mail or the attachment
aware MimeMail.
-In one project our team even used ant to build CD images through a build followed
-by a long set of Copy tasks, which worked surprisingly well.
+In one project our team even used Ant to build CD images through a build followed
+by a long set of Copy tasks, which worked surprisingly well, certainly
+easier than when we mailed them to the free email service on
+myrealbox.com, then pulled them down from the far end's web browser, which we
+were running over WinNT remote desktop connection, that being tunneled
+through SSH.
+
@@ -499,17 +570,17 @@ refactor the project directory structures.
@@ -517,7 +588,7 @@ Often an update will require changes to the build.xml files. Most
changes are intended to be backwards compatible, but sometimes an
incompatible change turns out to be
necessary. That is why doing the update in the lull after a big
-milestone is important. It is also why including ant.jar and related
+milestone is important. It is also why including Ant.jar and related
files in the CVS tree helps ensure that old versions of your software
can be still be built.
@@ -532,8 +603,8 @@ take this approach.
-Once you start extending ant with new tasks, it suddenly becomes much
-more tempting to pull down regular builds. The most recent ant builds
+Once you start extending Ant with new tasks, it suddenly becomes much
+more tempting to pull down regular builds. The most recent Ant builds
are invariably the the best platform for writing your extensions, as you
can take advantage of the regular enhancements to the foundational
classes. It also prevents you from wasting time working on something
@@ -546,9 +617,9 @@ task on Ant 0.8 in isolation, announcing its existence six months latter
and discovering that instead of adulation all you get are helpful
pointers to the existing implementation. The final benefit of being
involved with the process is that it makes it easier for your tasks to
-be added with the ant CVS tree, bringing forward the date when ant has
+be added with the Ant CVS tree, bringing forward the date when Ant has
taken on all the changes you needed to make to get your project to work.
-If that happens you can revert to an official ant release, and get on
+If that happens you can revert to an official Ant release, and get on
with all the other crises.
@@ -567,7 +638,7 @@ Installing with Ant.
-Because ant can read environment variables, copy, unzip and delete files
+Because Ant can read environment variables, copy, unzip and delete files
and make java and OS calls, it can be used for simple installation
tasks. For example, an installer for tomcat could extract the
environment variable TOMCAT_HOME, stop tomcat running, and copy a war
@@ -577,20 +648,20 @@ was wanted.
-The advantage of using ant is firstly that the same install targets
+The advantage of using Ant is firstly that the same install targets
can be used from your local build files (via an ant invocation
of the install.xml file), and secondly that a basic install target is
quite easy to write. The disadvantages of this approach are that the
-destination must have an up to date version of ant correctly
-pre-installed, and ant doesn't allow you to handle failures well -and a
+destination must have an up to date version of Ant correctly
+pre-installed, and Ant doesn't allow you to handle failures well -and a
good installer is all about handling when things go wrong, from files
-being in use to jar versions being different. This means that ant is not
+being in use to jar versions being different. This means that Ant is not
suited for shrink wrapped software, but it does work for deployment and
installation to your local servers.
-One major build project I was involved in had an ant install build file
+One major build project I was involved in had an Ant install build file
for the bluestone application server, which would shutdown all four
instances of the app server on a single machine, copy the new version of
the war file (with datestamp and buildstamp) to an archive directory,
@@ -601,32 +672,37 @@ firewall, we upped the ante in the deployment process by using the ftp
task to copy out the war and build files, then the telnet task to
remotely invoke the build file. The result was we had automated
recompile and redeploy to local servers from inside our IDE (Jedit) or
-the command line, which was simply invalualbe.
+the command line, which was simply invaluable. Imagine pressing a button
+on your IDE toolbar to build, unit test, deploy and then functional test
+your webapp.
-One extra trick I added later was a junit test case to run through
-the install check list. With tests to verify access permissions on network
-drives, approximate clock synchronisation between servers, DNS functionality,
-ability to spawn executables and all the other trouble spots
-, the install script could automatically do
-a system health test during install time and report problems. [The same tests
-could also be invoked from a JMX MBean, but that's another story].
+One extra trick I added later was a junit test case to run through the
+install check list. With tests to verify access permissions on network
+drives, approximate clock synchronisation between servers, DNS
+functionality, ability to spawn executables and all the other trouble
+spots, the install script could automatically do a system health test
+during install time and report problems. [The same tests could also be
+invoked from a JMX MBean, but that's another story].
+
-So, ant is not a substitute for a real installer tool, except in the
+So, Ant is not a substitute for a real installer tool, except in the
special case of servers you control, but in that context it does let
you integrate remote installation with your build.
+
+
-Before you go overboard with using XML inclusion, note that the ant task lets
-you call any target in any other build file -with all your property settings propagating down to
-that target. So you can actually have a suite of utility targets -"deploy-to-stack-a", "email-to-team",
-"cleanup-installation" which can be called from any of your main build files, perhaps with subtly changed
-parameters. Indeed, after a couple of projects you may be able to create a re-usable core build file which
-contains the core targets of a basic java development project -compile, debug, deploy- which project specific
+
+Before you go overboard with using XML inclusion, note that the
+<ant> task lets you call any target in any other build
+file -with all your property settings propagating down to that target.
+So you can actually have a suite of utility targets
+-"deploy-to-stack-a", "email-to-team", "cleanup-installation" which can
+be called from any of your main build files, perhaps with subtly changed
+parameters. Indeed, after a couple of projects you may be able to create
+a re-usable core build file which contains the core targets of a basic
+Java development project -compile, debug, deploy- which project specific
build files call with their own settings. If you can achive this then
-you are definately making your way up the software maturity ladder. NB,
-ant copies all your properties unless the inheritall attribute is set to false. Before that
-attribute existed you had to carefully name all property definitions in all build files to prevent unintentional
-overwriting of the invoked property by that of the caller, now you just have to remember to set
-inheritall="false" on all uses of the ant task.
+you are definately making your way up the software maturity ladder. With
+a bit of work you may progress from being a SEI CMM Level 0 organisation
+"Individual Heroics are not enough" to SEI CMM Level 1, "Projects only
+succeed due to individual heroics"
-
+
+
+NB, <ant> copies all your properties unless the
+inheritall attribute is set to false. Before that attribute
+existed you had to carefully name all property definitions in all build
+files to prevent unintentional overwriting of the invoked property by
+that of the caller, now you just have to remember to set
+inheritall="false" on all uses of the <ant> task.
+
+
Having a custom invocation script which runs off a CVS controlled
-library tree under PROJECT_HOME also lets you control ant versions
-across the team -developers can have other copies of ant if they want,
+library tree under PROJECT_HOME also lets you control Ant versions
+across the team -developers can have other copies of Ant if they want,
but the CVS tree always contains the jar set used to build your project.
-You can also write wrapper scripts which invoke the existing ant
+You can also write wrapper scripts which invoke the existing Ant
scripts. This is an easy way to extend them. The wrapper scripts can add
extra definitions and name explicit targets, redefine ANT_HOME and
generally make development easier. Note that "ant" in Windows is really
@@ -764,53 +851,67 @@ Write all code so that it can be called from Ant
This seems a bit strange and idealistic, but what it means is that you should
write all your java code as if it may be called as a library at some point in
-future. So do not place calls to System.exit() deep in the code -if you
+future. So do not place calls to System.exit() deep in the code -if you
want to exit a few functions in, raise an exception instead and have
-main() deal with it.
+main() deal with it.
-
+
+Moving one step further, consider proving an Ant Task interface to the
+code as a secondary, primary or even sole interface to the
+functionality. Ant actually makes a great bootloader for Java apps as it
+handles classpath setup, and you can re-use all the built in tasks for
+preamble and postamble work. Some projects, such as
+XDoclet only run under Ant, because
+that is the right place to be.
+
+
+
+
- Extend ant through new tasks
+ Extend Ant through new tasks
-If ant does not do what you want, you can use the
+If Ant does not do what you want, you can use the
exec and
java tasks or
inline scripting to extend it. In a
project with many build.xml files, you soon find that having a single
central place for implementing the functionality keeps maintenance
-overhead down. Implementing task extensions through java code seems
+overhead down. Implementing task extensions through Java code seems
extra effort at first, but gives extra benefits:-
-
+
+In a way, it is it this decoupling of functionality, "the tasks", from
+the declaration of use, "the build file", that has helped Ant succeed.
+If you have to get something complex done in Make or an IDE, you have a
+hairy makefile that everyone is scared of, or an IDE configuration that
+is invariably very brittle. But an Ant task is reusable and shareable
+among all Ant users. Many of the core and optional tasks in Ant today,
+tasks you do or will come to depend on, were written by people trying to
+solve their own pressing problems.
+
Embrace Automated Testing
@@ -158,23 +171,27 @@ Embrace Automated Testing
(alternatively "recriminate early, recriminate often")
Learn to Use and love the add-ons to Ant
+The Ant distribution is not the limit of the Ant universe, it is only
+the beginning. Look at the
+
+External Tools and Tasks page
+ for an up to date list. Here are some of them that .
+
+
+
+
+
+This tool audits your code and generates HTML reports of wherever any
+style rule gets broken. Nobody can hide from the code police now! tip:
+start using this early, so the corrections are less.
+
+This sourceforge project contains helper tasks that are kept separate
+from core Ant for ideological purity; the foreach and trycatch tasks in
+particular. These give you iteration and extra error handling. Also on
+the site is the <cc> task suite, that compile and link native code
+on a variety of platforms.
+
+
Cross Platform Ant
@@ -205,12 +259,12 @@ single workstation.
Command Line apps: Exec/ Apply
+Command Line apps: Exec/
+ Apply
The trouble with external invocation is that not all functions are found
cross platform, and those that are often have different names -DOS
@@ -225,7 +279,7 @@ clashes arising.
Both the command line invocation tasks let you specify which platform
you want the code to run on, so you could write different tasks for each
platform you are targeting. Alternatively, the platform differences
-could be handled inside some external code which ant calls. This can be
+could be handled inside some external code which Ant calls. This can be
some compiled down java in a new task, or an external script file.
Cross platform paths
@@ -248,7 +302,7 @@ that 'DOS-like pathnames are handled', 'Unix like paths are handled'.
Disk drives -'C:'- are handled on DOS-based boxes, but placing them in
the build.xml file ruins all chances of portability. Relative file paths
are much more portable. Semicolons work as path separators -a fact which
-is useful if your ant invocation wrapper includes a list of jars as a
+is useful if your Ant invocation wrapper includes a list of jars as a
defined property in the command line. In the build files you may find it
better to build a classpath by listing individual files (using location=
attributes), or by including a fileset of *.jar in the classpath
@@ -297,7 +351,11 @@ distributions, and is a simple download for . A Perl file with .pl extension, with the usual Unix
path to perl on the line 1 comment and marked as executable can be run
on Windows, OS/2 and Unix and hence called from Ant without issues. The
-perl code can be left to resolve its own platform issues.
+perl code can be left to resolve its own platform issues. Dont forget to
+set the line endings of the file to the appropriate platform when you
+redistribute Perl code; <fixCRLF>
+can do that for you.
Team Development Processes
@@ -319,7 +377,7 @@ Another good tactic is to use a unified directory tree, and add on extra
tools inside that tree. All references can be made relative to the tree.
If team members are expected to add a directory in the project to their
path, then command line tools can be included there -including those
-invoked by ant exec tasks. Put everything under source code control and
+invoked by Ant exec tasks. Put everything under source code control and
you have a one stop shop for getting a build/execute environment purely
from CVS or your equivalent.
@@ -328,21 +386,22 @@ from CVS or your equivalent.
Deploying with Ant
-One big difference between ant and older tools such as make is that the
-processes for deploying java to remote sites are reasonably well
-evolved in ant. That is because we all have to do it these days, so
+One big difference between Ant and older tools such as Make is that the
+processes for deploying Java to remote sites are reasonably well
+evolved in aAt. That is because we all have to do it these days, so
many people have put in the effort to make the tasks easier.
Directory Structures
@@ -386,44 +457,44 @@ another, and easy to clean up when desired.
The project contains sub directories
@@ -435,7 +506,7 @@ manifests, and a web folder for web content -JSP, html, images
and so on. Keeping the content in this folder (or sub heirarchy)
together makes it easier to test links before deployment. The actual
production of a deployment image -such as a war file- can be left to the
-appropriate ant task: there is no need to completely model your source tree
+appropriate Ant task: there is no need to completely model your source tree
upon the deployment heirarchy.
-
bin
-
- common binaries, scripts -put this on the path.
-
+ bin
+
+ common binaries, scripts -put this on the path.
+
-
build
-
- This is the tree for building; ant creates it and can empty it
- in the 'clean' project.
-
+ build
+
+ This is the tree for building; Ant creates it and can empty it
+ in the 'clean' project.
+
-
dist
-
- Distribution outputs go in here; the directory is created in ant
- and clean empties it out
-
+ dist
+
+ Distribution outputs go in here; the directory is created in Ant
+ and clean empties it out
+
-
doc
-
- Hand crafted documentation
-
+ doc
+
+ Hand crafted documentation
+
-
lib
-
- Imported Java libraries go in to this directory
-
+ lib
+
+ Imported Java libraries go in to this directory
+
-
src
-
- source goes in under this tree in a heirarchy which matches
- the package names. The dependency compilation of javac requires this.
-
+ src
+
+ source goes in under this tree in a heirarchy which matches
+ the package names. The dependency rules of <javac> requires this.
+
- Ant Update Policies
+ Ant Update Policies
-Once you start using ant, you should have a policy on when and how the
+Once you start using Ant, you should have a policy on when and how the
team updates their copies. A simple policy is "every official release
after whatever high stress milestone has pushed all unimportant tasks
(like sleep and seeing daylight) on the back burner". This insulates you
-from the changes and occasional instabilities that ant goes through
+from the changes and occasional instabilities that Ant goes through
during development. Its main disadvantage is that it isolates you from
-the new tasks and features that ant is constantly adding.
+the new tasks and features that Ant is constantly adding.
Tips and Tricks
-
- <!DOCTYPE project [
- <!ENTITY IncludeBuildCore SYSTEM "buildCore.xml">
- <!ENTITY IncludeBuildSecondary SYSTEM "buildSecondary.xml">
- ]>
-
- <target name="includedBuild">
- &IncludeBuildCore;
- &IncludeBuildSecondary;
- </target>
+ <!DOCTYPE project [
+ <!ENTITY propertiesAndPaths SYSTEM "propertiesAndPaths.xml">
+ <!ENTITY taskdefs SYSTEM "taskdefs.xml">
+ ]>
+
+ &propertiesAndPaths;
+ &taskdefs;
The cleaner method using XInclude/Xpath will let you include named
targets from one build file or another, using
@@ -704,27 +778,40 @@ targets from one build file or another, using
the xpointer syntax. You'll need to wait for the W3C proposals
to finalise and the java XML parsers to implement it before
using xpointer references.
+
- Putting it all together
-
+
+ Putting it all together
+
-What does an ant build process look like in this world? Assuming a
+What does an Ant build process look like in this world? Assuming a
single directory structure for simplicity, the build file
should contain a number of top level targets
@@ -840,27 +941,53 @@ Internal targets should be used to structure the process
-The switching between debug and release can be done using the 'if' and
-'unless' conditional flags on the targets, so that debug gets called
-unless 'project.mode.release' is defined.
+The switching between debug and release can be done by making
+init-release conditional on a property, such as release.build
+being set :-
+
+<target name="init-release" if="release.build">
+ <property name="build.debuglevel" value="lines,source"/>
+ </target>
+
+
+You then have dependent targets, such as "compile", depend on this
+conditional target; there the 'default' properties are set, and then the
+property is actually used. Because Ant properties are immutable,
+if the release target was executed its settings will override the
+default values:
+
+<target name="compile" depends="init,init-release">
+ <property name="build.debuglevel" value="lines,vars,source"/>
+ <echo>debug level=${build.debuglevel}</echo>
+ <javac destdir="${build.classes.dir}"
+ debug="true"
+ debuglevel="${build.debuglevel}"
+ includeAntRuntime="false"
+ srcdir="src">
+ <classpath refid="compile.classpath"/>
+ </javac>
+ </target>
+
+As a result, we now have a build where the release mode only includes
+the filename and line debug information (useful for bug reports), while
+the development system included variables too.
It is useful to define a project name property which can be echoed in -the init task. This lets you work out which ant file is breaking in a +the init task. This lets you work out which Ant file is breaking in a multi file build.
-What goes in to the internal ant tasks depends on your own projects. One +What goes in to the internal Ant tasks depends on your own projects. One very important tactic is 'keep path redefinition down through references' - you can reuse paths by giving them an ID and then referring to them via the 'refid' attribute you should only need to @@ -869,7 +996,7 @@ similarly.
-Once you have set up the directory structures, and defined the ant tasks +Once you have set up the directory structures, and defined the Ant tasks it is time to start coding. An early priority must be to set up the automated test process, as that not only helps ensures that the code works, it verifies that the build process is working. @@ -888,7 +1015,7 @@ system is often much harder work.
@@ -905,15 +1032,15 @@ conditional statements. If your build needs to handle exceptions then look at the sound listener as a simple example of how to write your own listener class. Complex conditional statements can be handled by having something else do the -tests and then build the appropriate ant task. XSLT can be used for +tests and then build the appropriate Ant task. XSLT can be used for this.
+ +For questions related to this document, use the Ant mailing list.
Copyright © 2000-2002 Apache Software Foundation. All rights