Developing Applications with Tomcat


Source Organisation


Directory Structure

A key recommendation is to separate the directory hierarchy containing your source code from the directory hierarchy containing your deployable application (described in the preceding section). Maintaining this separation has the following advantages:


As we will see, the ant development tool makes the creation and processing of such directory hierarchies nearly painless.

The actual directory and file hierarchy used to contain the source code of an application can be pretty much anything you like. However, the following organization has proven to be quite generally applicable, and is expected by the example build.xml configuration file that is discussed below. All of these components exist under a top level project source directory for your application:




Build.Xml Configuration File

We will be using the ant tool to manage the compilation of our Java source code files, and creation of the deployment hierarchy. Ant operates under the control of a build file, normally called build.xml, that defines the processing steps required. Like a Makefile, the build.xml file provides several "targets" that support optional development activities, such as creating the associated Javadoc documentation, erasing the deployment home directory so you can build your project from scratch, or creating the web application archive file so you can distribute your application.

To give you a head start, a basic build.xml file is provided (in the doc/appdev folder) that you can customize and install in the project source directory for your application. This file includes comments that describe the various targets that can be executed. The following targets are generally provided:




Batch Scripts

The primary script we will utilize is generically called the build script. It executes Ant, which reads and processes the build.xml file discussed above. Each time you execute the build script, you will specify the build "target" that you wish to execute. Users of a command line MAKE tool (which processes a makefile) will recognize this approach.

On Windows-based systems, the following script should be saved as file build.bat in the project source directory, and customzed as required:

echo off
rem Build Script for "My Application"
rem $Id: source.html,v 1.2 2000/03/28 00:44:11 craigmcc Exp $

rem Identify the custom class path we need
if "%CLASSPATH%" == "" goto noclasspath
set _CLASSPATH=%CLASSPATH%
set CLASSPATH=%CLASSPATH%;%TOMCAT_HOME%/classes
goto restclasspath
:noclasspath
set _CLASSPATH=
set CLASSPATH=%TOMCAT_HOME%/classes
:restclasspath
set CLASSPATH=%CLASSPATH%;%TOMCAT_HOME%/lib/ant.jar;%TOMCAT_HOME%/lib/xml.jar
set CLASSPATH=%CLASSPATH%;%TOMCAT_HOME%/lib/jasper.jar
set CLASSPATH=%CLASSPATH%;%TOMCAT_HOME%/lib/servlet.jar
set CLASSPATH=%CLASSPATH%;%TOMCAT_HOME%/lib/webserver.jar
rem Execute ANT to perform the requested build target
java -Dtomcat.home=%TOMCAT_HOME% org.apache.tools.ant.Main %1 %2 %3 %4 %5 %6 %7 %8 %9

rem Clean up CLASSPATH
set CLASSPATH=%_CLASSPATH%
set _CLASSPATH=


Build script customizations you might consider include:



NB. The batch files found in %TOMCAT_HOME%\bin have been written and tested under Windows NT. Some modifications may be necessary for operation on a Windows 98 platform.


Previous    Next