Buildmanagement for J2EE applications using ANT HOW-TO| Introduction |
written by Johannes Fiala, johannes.fiala@fwd.at in May 2003
This HOW-TO covers my experiences how to setup an effective build management structure using ANT.
I'll cover the following aspects how to use ant to build your Tomcat (or other J2EE container) webapp:
- Build & Run - the basic setup of buildmanagement
- Why & how you should use one build.xml file for all your webapps
- How to compile your libraries using ant
- Checkout libraries from a central lib-repository
- Building applications for different locations easily (e.g. development site, test site, production site)
After reading this HOWTO, you should be able to effectively build and deploy your web applications in a complex environment.
Note: This guide should apply to all J2EE containers (except the section about deploying using the manager app of Tomcat).
|
| Update history |
Update 2003-05-10:
Finished Jspc and Deployment sections.
Update 2003-05-04, 22:00 CEDT:
Entered first ideas.
|
| Tested configurations |
I tested the following configurations:
- Tomcat 4.1.24 with JDK 1.4 on Win 2K Professional/Win NT 4.0
- Tomcat 5.0.1 with JDK 1.4 on Win 2K Professional
|
| Prerequisites |
To use this document, you should download and install (=unzip) the following components:
- Tomcat 4.1.24 or Tomcat 5.0.1
- Ant 1.5.2 or higher
|
| Basic setup of ANT |
After you've installed Tomcat and Ant, you are ready to set up your build environment. Let's suppose
you've installed
- Tomcat to "c:/tomcat-4.1.24"
- Ant to "c:/ant-1.5.2"
We'll be using the following identifiers for the paths referred in this document:
- %DEV_HOME% = Your personal development directory (e.g. c:/develop/)
- %MYAPP_HOME% = Your first project (e.g. c:/develop/devapp/myapp)
- %CATALINA_HOME% = Your Tomcat installation directory (e.g. c:/tomcat-4.1.24)
- %ANT_HOME% = Your Tomcat installation directory (e.g. c:/ant-1.5.2)
First, copy %CATALINA_HOME%/server/lib/catalina-ant.jar to %ANT_HOME%/lib/catalina-ant.jar.
Next, you have to setup your development-directory. We'll refer to this directory in this document using "MYAPP_HOME"
For example you can create the directory %DEV_HOME%/devapp/myapp.
Next, create the following directories beneath c:/develop/devapp/myapp:
- src
will contain the sourcecode of your servlets.
- jsp
will contain the jsps you write.
- web
will contain all your static resources (images, stylesheets, javascript-files, configuration files, WEB-INF/**)
- build
will be used by ant to store all files needed to complete the build
- docs
will contain the javadocs generated from your sourcefiles.
- dist
will contain the finished WAR file to be deployed to your J2EE container
|
| First steps running ant |
After you've created both build.properties and build.xml at your %MYAPP_HOME%-directory, you're ready to start ant:
Go to %MYAPP_HOME% and enter the following to run the first ant build:
ant
ant will do run the following tasks for you: prepare, jspc, merge-descriptors and compile.
Here comes an explanation of the tasks run automatically:
prepare: prepares the build by cleaning up temporary directories and removing generated files from the last build.
jspc: runs the JspC compiler to create Java Servlets out of your JSPs.
merge-descriptors: will create %MYAPP_HOME%/web/WEB-INF/web.xml containing correct references to your JSPs. It takes the original web.xml file from %MYAPP_HOME%/jsp/web-template.xml.
compile: will run a full compile of your Java Servlets at %MYAPP_HOME%/src.
| JspC issues |
"JspC" refers to the Jasper2 compiler, which convers your JSP files into Java servlets.
I used the directory src to store the generated JSPs as well as my Java servlet source files.
The next task, "compile" will then compile your servlet files as well as the generated Jsp-Servlets to class files.
This means Tomcat will never be required to perform a compilation of one of your JSPs at the server side, which
leaves you off with two big advantages:
- There's no increase in response time when accessing a JSP for the first time, as the Jsp has already been compiled.
- If there is a low-level error in your JSP, you will notice when JspC is run, not at the server.
|
| Issues you might run into |
JspC duplicate naming bug in TC 4.1.18 and before
If you're using a version prior to TC 4.1.24, there have been issues if you used subdirectories and had a jsp with the same filename in multiple directories.
These issues (which have been caused by naming conflicts) have been resolved with TC 4.1.24.
JavaEncoding parameter hardcoded to UTF-8
If you want to generate your Jsps with a different encoding than UTF-8, you might be interested in downloading
the patch which allows you to use any encoding you want to set:
Patch for javaEncoding using JspC
|
| CLASSPATH issues |
The following directories will be included in your CLASSPATH for compilation of your source-files:
- all JAR files stored at %DEV_HOME%/devapp/lib in your CLASSPATH.
- the most important Tomcat JAR files
|
| Building a distribution (WAR file) |
You can create a WAR file using ant dist. This will create a WAR file containing all contents of the %MYAPP_HOME%/build directory conveniently packed into an WAR archive.
You can then deploy this WAR file at your favorite J2EE server.
|
|
| Using a central JAR-file (libraries) repository |
The following section explains how to allow for one central repository containing all your libraries used for all your projects.
This has the BIG advantage of ensuring consistency across all your apps. If you need different versions of specific JAR-files, you can still do so by naming them differently (including the version identifier).
download the build.xml file with library repository support
To start using the library repository in your myapp-project, you have to do the following:
- download the build.xml file from above.
- create a file lib.xml containing references to the JAR files of the repository you actually need in your project.
- run
ant to have the library files you mentioned in lib.xml copied automatically into /build/WEB-INF/lib.
The lib.xml file is really simple and has to use the following syntax:
 |  |  |  |
<include name="mylib.jar" />
<include name="mylib-custom.jar" />
...
|  |  |  |  |
You only have ONE version of your build.xml file (copied to every project path, e.g. %MYAPP_HOME%).
You specify the different libraries required for each project at the project level (e.g. %MYAPP_HOME%/lib.xml).
Thus, you have ONE central lib repository from which project "checks out" the libraries it needs
to be included in its WAR file.
|
| Using different locations |
As your project grows, you'll soon have the requirement to have multiple locations you want to deploy your
applications to.
A typical location scenario of a mid-size project would be as follows:
- Development location (e.g. local Tomcat installation on a Windows PC)
- Testing location (e.g. Tomcat installation on a Linux machine
- Production location (e.g. Tomcat installation on a Linux machine
download the build.xml file with libraries and multiple location support
Different locations usually use different directory names and different dns-names/urls to be used.
This requires you to use different properties files and maybe different context configurations for each location.
To manage different locations easily and just with simple ant commands, I've set up the following idea:
- Use separate folders for the static files which are specific to a location (e.g. /web-dev, /web-production).
- Keep the web folder for static files which apply to all locations (/web).
Additionally, set up separate command files, which will overwrite the build.properties file with the build.properties for the
current location and start ant with a location parameter set, which indicates which directory will contain the static files for this
location.
These are the only files/directories you have to set up to achieve this:
| Setting up directories for each location |
Directories for each location
- web-dev
will contain static resources specific to the location "dev"
- web-production
will contain static resources specific to the location "dev"
|
| Further applications of the location idea |
You can also use the location idea to allow for build for different Tomcat versions. I created a location dev5
to compile and deploy to TC 5.0.1 to perform routine tests against the next version of Tomcat. Of course you can also do this to
perform regression tests within the TC 4.x branch.
|
|
| Open issues |
InitParameters don't get inited after deploy
Sometimes when I do ant undeploy and then ant deploy, the new Context doesn't get inited properly, i.e. the InitParameters
are set to null instead of their "real" values in context.xml. Often repeating the undeploy/deploy with a small time lag after undeploy helps.
As last resort you have to stop and start Tomcat...
Broken JAR files (ClassNotFoundException/ClassFormatException)
Rarely, I get a ClassNotFoundException or ClassFormatException (truncated jar file) after doing undeploy/deploy.
Luckily this only occurs rarely. Stopping and starting tomcat helps.
|
| Credits |
- Yoav Shapira: thx for your extensive support at the userlist.
- Craig McClanahan: thx for writing the manager app.
- XML/XSLT layout: The XSLT layout has been copied and adapted from the original Tomcat project.
|
|