| 1 | <html><head><META http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><title>Application Developer's Guide - Source Organization</title><meta value="Craig R. McClanahan" name="author"><meta value="craigmcc@apache.org" name="email"></head><body vlink="#525D76" alink="#525D76" link="#525D76" text="#000000" bgcolor="#ffffff"><table cellspacing="0" width="100%" border="0"><!--PAGE HEADER--><tr><td><!--PROJECT LOGO--><a href="http://tomcat.apache.org/"><img border="0" alt=" | 
|---|
| 2 |       The Apache Tomcat Servlet/JSP Container | 
|---|
| 3 |     " align="right" src="../images/tomcat.gif"></a></td><td><font face="arial,helvetica,sanserif"><h1>Apache Tomcat 6.0</h1></font></td><td><!--APACHE LOGO--><a href="http://www.apache.org/"><img border="0" alt="Apache Logo" align="right" src="../images/asf-logo.gif"></a></td></tr></table><table cellspacing="4" width="100%" border="0"><!--HEADER SEPARATOR--><tr><td colspan="2"><hr size="1" noshade></td></tr><tr><!--LEFT SIDE NAVIGATION--><td nowrap="true" valign="top" width="20%"><p><strong>Links</strong></p><ul><li><a href="../index.html">Docs Home</a></li></ul><p><strong>Contents</strong></p><ul><li><a href="index.html">Contents</a></li><li><a href="introduction.html">Introduction</a></li><li><a href="installation.html">Installation</a></li><li><a href="deployment.html">Deployment</a></li><li><a href="source.html">Source Code</a></li><li><a href="processes.html">Processes</a></li><li><a href="sample/">Example App</a></li></ul></td><!--RIGHT SIDE MAIN BODY--><td align="left" valign="top" width="80%"><table cellspacing="4" width="100%" border="0"><tr><td valign="top" align="left"><h1>Application Developer's Guide</h1><h2>Source Organization</h2></td><td nowrap="true" valign="top" align="right"><small><a href="printer/source.html"><img alt="Printer Friendly Version" border="0" src="../images/printer.gif"><br>print-friendly<br>version | 
|---|
| 4 |                     </a></small></td></tr></table><table cellpadding="2" cellspacing="0" border="0"><tr><td bgcolor="#525D76"><font face="arial,helvetica.sanserif" color="#ffffff"><a name="Directory Structure"><strong>Directory Structure</strong></a></font></td></tr><tr><td><blockquote> | 
|---|
| 5 |  | 
|---|
| 6 |     <blockquote><em> | 
|---|
| 7 |     <p>The description below uses the variable name $CATALINA_BASE to refer the | 
|---|
| 8 |     base directory against which most relative paths are resolved. If you have | 
|---|
| 9 |     not configured Tomcat 6 for multiple instances by setting a CATALINA_BASE | 
|---|
| 10 |     directory, then $CATALINA_BASE will be set to the value of $CATALINA_HOME, | 
|---|
| 11 |     the directory into which you have installed Tomcat 6.</p> | 
|---|
| 12 |     </em></blockquote> | 
|---|
| 13 |  | 
|---|
| 14 | <p>A key recommendation of this manual is to separate the directory | 
|---|
| 15 | hierarchy containing your source code (described in this section) from | 
|---|
| 16 | the directory hierarchy containing your deployable application | 
|---|
| 17 | (described in the preceding section).  Maintaining this separation has | 
|---|
| 18 | the following advantages:</p> | 
|---|
| 19 | <ul> | 
|---|
| 20 | <li>The contents of the source directories can be more easily administered, | 
|---|
| 21 |     moved, and backed up if the "executable" version of the application | 
|---|
| 22 |     is not intermixed. | 
|---|
| 23 |     <br><br></li> | 
|---|
| 24 | <li>Source code control is easier to manage on directories that contain | 
|---|
| 25 |     only source files. | 
|---|
| 26 |     <br><br></li> | 
|---|
| 27 | <li>The files that make up an installable distribution of your | 
|---|
| 28 |     application are much easier to select when the deployment | 
|---|
| 29 |     hierarchy is separate.</li> | 
|---|
| 30 | </ul> | 
|---|
| 31 |  | 
|---|
| 32 | <p>As we will see, the <code>ant</code> development tool makes the creation | 
|---|
| 33 | and processing of such directory hierarchies nearly painless.</p> | 
|---|
| 34 |  | 
|---|
| 35 | <p>The actual directory and file hierarchy used to contain the source code | 
|---|
| 36 | of an application can be pretty much anything you like.  However, the | 
|---|
| 37 | following organization has proven to be quite generally applicable, and is | 
|---|
| 38 | expected by the example <code>build.xml</code> configuration file that | 
|---|
| 39 | is discussed below.  All of these components exist under a top level | 
|---|
| 40 | <em>project source directory</em> for your application:</p> | 
|---|
| 41 | <ul> | 
|---|
| 42 | <li><strong>docs/</strong> - Documentation for your application, in whatever | 
|---|
| 43 |     format your development team is using.<br><br></li> | 
|---|
| 44 | <li><strong>src/</strong> - Java source files that generate the servlets, | 
|---|
| 45 |     beans, and other Java classes that are unique to your application. | 
|---|
| 46 |     If your source code is organized in packages (<strong>highly</strong> | 
|---|
| 47 |     recommended), the package hierarchy should be reflected as a directory | 
|---|
| 48 |     structure underneath this directory.<br><br></li> | 
|---|
| 49 | <li><strong>web/</strong> - The static content of your web site (HTML pages, | 
|---|
| 50 |     JSP pages, JavaScript files, CSS stylesheet files, and images) that will | 
|---|
| 51 |     be accessible to application clients.  This directory will be the | 
|---|
| 52 |     <em>document root</em> of your web application, and any subdirectory | 
|---|
| 53 |     structure found here will be reflected in the request URIs required to | 
|---|
| 54 |     access those files.<br><br></li> | 
|---|
| 55 | <li><strong>web/WEB-INF/</strong> - The special configuration files required | 
|---|
| 56 |     for your application, including the web application deployment descriptor | 
|---|
| 57 |     (<code>web.xml</code>, defined in the  | 
|---|
| 58 |     <a href="http://java.sun.com/products/servlet">Servlet Specification</a>),  | 
|---|
| 59 |     tag library descriptors for custom tag libraries | 
|---|
| 60 |     you have created, and other resource files you wish to include within | 
|---|
| 61 |     your web application.  Even though this directory appears to be a | 
|---|
| 62 |     subdirectory of your <em>document root</em>, the Servlet Specification | 
|---|
| 63 |     prohibits serving the contents of this directory (or any file it contains) | 
|---|
| 64 |     directly to a client request.  Therefore, this is a good place to store | 
|---|
| 65 |     configuration information that is sensitive (such as database connection | 
|---|
| 66 |     usernames and passwords), but is required for your application to | 
|---|
| 67 |     operate successfully.</li> | 
|---|
| 68 | </ul> | 
|---|
| 69 |  | 
|---|
| 70 | <p>During the development process, two additional directories will be | 
|---|
| 71 | created on a temporary basis:</p> | 
|---|
| 72 | <ul> | 
|---|
| 73 | <li><strong>build/</strong> - When you execute a default build | 
|---|
| 74 |     (<code>ant</code>), this directory will contain an exact image | 
|---|
| 75 |     of the files in the web application archive for this application. | 
|---|
| 76 |     Tomcat 6 allows you to deploy an application in an unpacked | 
|---|
| 77 |     directory like this, either by copying it to the | 
|---|
| 78 |     <code>$CATALINA_BASE/webapps</code> directory, or by <em>installing</em> | 
|---|
| 79 |     it via the "Manager" web application.  The latter approach is very | 
|---|
| 80 |     useful during development, and will be illustrated below. | 
|---|
| 81 |     <br><br></li> | 
|---|
| 82 | <li><strong>dist/</strong> - When you execute the <code>ant dist</code> | 
|---|
| 83 |     target, this directory will be created.  It will create an exact image | 
|---|
| 84 |     of the binary distribution for your web application, including an license | 
|---|
| 85 |     information, documentation, and README files that you have prepared.</li> | 
|---|
| 86 | </ul> | 
|---|
| 87 |  | 
|---|
| 88 | <p>Note that these two directories should <strong>NOT</strong> be archived in | 
|---|
| 89 | your source code control system, because they are deleted and recreated (from | 
|---|
| 90 | scratch) as needed during development.  For that reason, you should not edit | 
|---|
| 91 | any source files in these directories if you want to maintain a permanent | 
|---|
| 92 | record of the changes, because the changes will be lost the next time that a | 
|---|
| 93 | build is performed.</p> | 
|---|
| 94 |  | 
|---|
| 95 |   <table cellpadding="2" cellspacing="0" border="0"><tr><td bgcolor="#828DA6"><font face="arial,helvetica.sanserif" color="#ffffff"><a name="External Dependencies"><strong>External Dependencies</strong></a></font></td></tr><tr><td><blockquote> | 
|---|
| 96 |  | 
|---|
| 97 |   <p>What do you do if your application requires JAR files (or other | 
|---|
| 98 |   resources) from external projects or packages?  A common example is that | 
|---|
| 99 |   you need to include a JDBC driver in your web application, in order to | 
|---|
| 100 |   operate.</p> | 
|---|
| 101 |  | 
|---|
| 102 |   <p>Different developers take different approaches to this problem. | 
|---|
| 103 |   Some will encourage checking a copy of the JAR files you depend on into | 
|---|
| 104 |   the source code control archives for every application that requires those | 
|---|
| 105 |   JAR files.  However, this can cause significant management issues when you | 
|---|
| 106 |   use the same JAR in many applications - particular when faced with a need | 
|---|
| 107 |   to upgrade to a different version of that JAR file.</p> | 
|---|
| 108 |  | 
|---|
| 109 |   <p>Therefore, this manual recommends that you <strong>NOT</strong> store | 
|---|
| 110 |   a copy of the packages you depend on inside the source control archives | 
|---|
| 111 |   of your applications.  Instead, the external dependencies should be | 
|---|
| 112 |   integrated as part of the process of <strong>building</strong> your | 
|---|
| 113 |   application.  In that way, you can always pick up the appropriate version | 
|---|
| 114 |   of the JAR files from wherever your development system administrator has | 
|---|
| 115 |   installed them, without having to worry about updating your application | 
|---|
| 116 |   every time the version of the dependent JAR file is changed.</p> | 
|---|
| 117 |  | 
|---|
| 118 |   <p>In the example Ant <code>build.xml</code> file, we will demonstrate | 
|---|
| 119 |   how to define <em>build properties</em> that let you configure the locations | 
|---|
| 120 |   of the files to be copied, without having to modify <code>build.xml</code> | 
|---|
| 121 |   when these files change.  The build properties used by a particular | 
|---|
| 122 |   developer can be customized on a per-application basis, or defaulted to | 
|---|
| 123 |   "standard" build properties stored in the developer's home directory.</p> | 
|---|
| 124 |  | 
|---|
| 125 |   <p>In many cases, your development system administrator will have already | 
|---|
| 126 |   installed the required JAR files into Tomcat 6's <code>lib</code> directory. | 
|---|
| 127 |   If this has been done, you need | 
|---|
| 128 |   to take no actions at all - the example <code>build.xml</code> file | 
|---|
| 129 |   automatically constructs a compile classpath that includes these files.</p> | 
|---|
| 130 |  | 
|---|
| 131 |   </blockquote></td></tr></table> | 
|---|
| 132 |  | 
|---|
| 133 | </blockquote></td></tr></table><table cellpadding="2" cellspacing="0" border="0"><tr><td bgcolor="#525D76"><font face="arial,helvetica.sanserif" color="#ffffff"><a name="Source Code Control"><strong>Source Code Control</strong></a></font></td></tr><tr><td><blockquote> | 
|---|
| 134 |  | 
|---|
| 135 | <p>As mentioned earlier, it is highly recommended that you place all of the | 
|---|
| 136 | source files that comprise your application under the management of a | 
|---|
| 137 | source code control system like the Concurrent Version System (CVS).  If you | 
|---|
| 138 | elect to do this, every directory and file in the source hierarchy should be | 
|---|
| 139 | registered and saved -- but none of the generated files.  If you register | 
|---|
| 140 | binary format files (such as images or JAR libraries), be sure to indicate | 
|---|
| 141 | this to your source code control system.</p> | 
|---|
| 142 |  | 
|---|
| 143 | <p>We recommended (in the previous section) that you should not store the | 
|---|
| 144 | contents of the <code>build/</code> and <code>dist/</code> directories | 
|---|
| 145 | created by your development process in the source code control system.  An | 
|---|
| 146 | easy way to tell CVS to ignore these directories is to create a file named | 
|---|
| 147 | <code>.cvsignore</code> (note the leading period) in your top-level source | 
|---|
| 148 | directory, with the following contents:</p> | 
|---|
| 149 | <div align="left"><table border="0" cellpadding="0" cellspacing="4"><tr><td height="1" width="1" bgcolor="#023264"><img border="0" hspace="0" vspace="0" height="1" width="1" src="../images/void.gif"></td><td height="1" bgcolor="#023264"><img border="0" hspace="0" vspace="0" height="1" width="1" src="../images/void.gif"></td><td height="1" width="1" bgcolor="#023264"><img border="0" hspace="0" vspace="0" height="1" width="1" src="../images/void.gif"></td></tr><tr><td width="1" bgcolor="#023264"><img border="0" hspace="0" vspace="0" height="1" width="1" src="../images/void.gif"></td><td height="1" bgcolor="#ffffff"><pre> | 
|---|
| 150 | build | 
|---|
| 151 | dist | 
|---|
| 152 | build.properties | 
|---|
| 153 | </pre></td><td width="1" bgcolor="#023264"><img border="0" hspace="0" vspace="0" height="1" width="1" src="../images/void.gif"></td></tr><tr><td height="1" width="1" bgcolor="#023264"><img border="0" hspace="0" vspace="0" height="1" width="1" src="../images/void.gif"></td><td height="1" bgcolor="#023264"><img border="0" hspace="0" vspace="0" height="1" width="1" src="../images/void.gif"></td><td height="1" width="1" bgcolor="#023264"><img border="0" hspace="0" vspace="0" height="1" width="1" src="../images/void.gif"></td></tr></table></div> | 
|---|
| 154 |  | 
|---|
| 155 | <p>The reason for mentioning <code>build.properties</code> here will be | 
|---|
| 156 | explained in the <a href="processes.html">Processes</a> section.</p> | 
|---|
| 157 |  | 
|---|
| 158 | <p>Detailed instructions for your source code control environment are beyond | 
|---|
| 159 | the scope of this manual.  However, the following steps are followed when | 
|---|
| 160 | using a command-line CVS client:</p> | 
|---|
| 161 | <ul> | 
|---|
| 162 | <li>To refresh the state of your source code to that stored in the | 
|---|
| 163 |     the source repository, go to your project source directory, and | 
|---|
| 164 |     execute <code>cvs update -dP</code>. | 
|---|
| 165 |     <br><br></li> | 
|---|
| 166 | <li>When you create a new subdirectory in the source code hierarchy, register | 
|---|
| 167 |     it in CVS with a command like <code>cvs add {subdirname}</code>. | 
|---|
| 168 |     <br><br></li> | 
|---|
| 169 | <li>When you first create a new source code file, navigate to the directory | 
|---|
| 170 |     that contains it, and register the new file with a command like | 
|---|
| 171 |     <code>cvs add {filename}</code>. | 
|---|
| 172 |     <br><br></li> | 
|---|
| 173 | <li>If you no longer need a particular source code file, navigate to the | 
|---|
| 174 |     containing directory and remove the file.  Then, deregister it in CVS | 
|---|
| 175 |     with a command like <code>cvs remove {filename}</code>. | 
|---|
| 176 |     <br><br></li> | 
|---|
| 177 | <li>While you are creating, modifying, and deleting source files, changes | 
|---|
| 178 |     are not yet reflected in the server repository.  To save your changes in | 
|---|
| 179 |     their current state, go to the project source directory | 
|---|
| 180 |     and execute <code>cvs commit</code>.  You will be asked to write a brief | 
|---|
| 181 |     description of the changes you have just completed, which will be stored | 
|---|
| 182 |     with the new version of any updated source file.</li> | 
|---|
| 183 | </ul> | 
|---|
| 184 |  | 
|---|
| 185 | <p>CVS, like other source code control systems, has many additional features | 
|---|
| 186 | (such as the ability to tag the files that made up a particular release, and | 
|---|
| 187 | support for multiple development branches that can later be merged).  See the | 
|---|
| 188 | links and references in the <a href="introduction.html">Introduction</a> for | 
|---|
| 189 | more information.</p> | 
|---|
| 190 |  | 
|---|
| 191 | </blockquote></td></tr></table><table cellpadding="2" cellspacing="0" border="0"><tr><td bgcolor="#525D76"><font face="arial,helvetica.sanserif" color="#ffffff"><a name="BUILD.XML Configuration File"><strong>BUILD.XML Configuration File</strong></a></font></td></tr><tr><td><blockquote> | 
|---|
| 192 |  | 
|---|
| 193 | <p>We will be using the <strong>ant</strong> tool to manage the compilation of | 
|---|
| 194 | our Java source code files, and creation of the deployment hierarchy.  Ant | 
|---|
| 195 | operates under the control of a build file, normally called | 
|---|
| 196 | <code>build.xml</code>, that defines the processing steps required.  This | 
|---|
| 197 | file is stored in the top-level directory of your source code hierarchy, and | 
|---|
| 198 | should be checked in to your source code control system.</p> | 
|---|
| 199 |  | 
|---|
| 200 | <p>Like a Makefile, the <code>build.xml</code> file provides several | 
|---|
| 201 | "targets" that support optional development activities (such as creating | 
|---|
| 202 | the associated Javadoc documentation, erasing the deployment home directory | 
|---|
| 203 | so you can build your project from scratch, or creating the web application | 
|---|
| 204 | archive file so you can distribute your application.  A well-constructed | 
|---|
| 205 | <code>build.xml</code> file will contain internal documentation describing | 
|---|
| 206 | the targets that are designed for use by the developer, versus those targets | 
|---|
| 207 | used internally.  To ask Ant to display the project documentation, change to | 
|---|
| 208 | the directory containing the <code>build.xml</code> flie and type:</p> | 
|---|
| 209 | <div align="left"><table border="0" cellpadding="0" cellspacing="4"><tr><td height="1" width="1" bgcolor="#023264"><img border="0" hspace="0" vspace="0" height="1" width="1" src="../images/void.gif"></td><td height="1" bgcolor="#023264"><img border="0" hspace="0" vspace="0" height="1" width="1" src="../images/void.gif"></td><td height="1" width="1" bgcolor="#023264"><img border="0" hspace="0" vspace="0" height="1" width="1" src="../images/void.gif"></td></tr><tr><td width="1" bgcolor="#023264"><img border="0" hspace="0" vspace="0" height="1" width="1" src="../images/void.gif"></td><td height="1" bgcolor="#ffffff"><pre> | 
|---|
| 210 | ant -projecthelp | 
|---|
| 211 | </pre></td><td width="1" bgcolor="#023264"><img border="0" hspace="0" vspace="0" height="1" width="1" src="../images/void.gif"></td></tr><tr><td height="1" width="1" bgcolor="#023264"><img border="0" hspace="0" vspace="0" height="1" width="1" src="../images/void.gif"></td><td height="1" bgcolor="#023264"><img border="0" hspace="0" vspace="0" height="1" width="1" src="../images/void.gif"></td><td height="1" width="1" bgcolor="#023264"><img border="0" hspace="0" vspace="0" height="1" width="1" src="../images/void.gif"></td></tr></table></div> | 
|---|
| 212 |  | 
|---|
| 213 | <p>To give you a head start, a <a href="build.xml.txt">basic build.xml file</a> | 
|---|
| 214 | is provided that you can customize and install in the project source directory | 
|---|
| 215 | for your application.  This file includes comments that describe the various | 
|---|
| 216 | targets that can be executed.  Briefly, the following targets are generally | 
|---|
| 217 | provided:</p> | 
|---|
| 218 | <ul> | 
|---|
| 219 | <li><strong>clean</strong> - This target deletes any existing | 
|---|
| 220 |     <code>build</code> and <code>dist</code> directories, so that they | 
|---|
| 221 |     can be reconstructed from scratch.  This allows you to guarantee that | 
|---|
| 222 |     you have not made source code modifications that will result in | 
|---|
| 223 |     problems at runtime due to not recompiling all affected classes. | 
|---|
| 224 |     <br><br></li> | 
|---|
| 225 | <li><strong>compile</strong> - This target is used to compile any source code | 
|---|
| 226 |     that has been changed since the last time compilation took place.  The | 
|---|
| 227 |     resulting class files are created in the <code>WEB-INF/classes</code> | 
|---|
| 228 |     subdirectory of your <code>build</code> directory, exactly where the | 
|---|
| 229 |     structure of a web application requires them to be.  Because | 
|---|
| 230 |     this command is executed so often during development, it is normally | 
|---|
| 231 |     made the "default" target so that a simple <code>ant</code> command will | 
|---|
| 232 |     execute it. | 
|---|
| 233 |     <br><br></li> | 
|---|
| 234 | <li><strong>all</strong> - This target is a short cut for running the | 
|---|
| 235 |     <code>clean</code> target, followed by the <code>compile</code> target. | 
|---|
| 236 |     Thus, it guarantees that you will recompile the entire application, to | 
|---|
| 237 |     ensure that you have not unknowingly introduced any incompatible changes. | 
|---|
| 238 |     <br><br></li> | 
|---|
| 239 | <li><strong>javadoc</strong> - This target creates Javadoc API documentation | 
|---|
| 240 |     for the Java classes in this web application.  The example | 
|---|
| 241 |     <code>build.xml</code> file assumes you want to include the API | 
|---|
| 242 |     documentation with your app distribution, so it generates the docs | 
|---|
| 243 |     in a subdirectory of the <code>dist</code> directory.  Because you normally | 
|---|
| 244 |     do not need to generate the Javadocs on every compilation, this target is | 
|---|
| 245 |     usually a dependency of the <code>dist</code> target, but not of the | 
|---|
| 246 |     <code>compile</code> target. | 
|---|
| 247 |     <br><br></li> | 
|---|
| 248 | <li><strong>dist</strong> - This target creates a distribution directory for | 
|---|
| 249 |     your application, including any required documentation, the Javadocs for | 
|---|
| 250 |     your Java classes, and a web application archive (WAR) file that will be | 
|---|
| 251 |     delivered to system administrators who wish to install your application. | 
|---|
| 252 |     Because this target also depends on the <code>deploy</code> target, the | 
|---|
| 253 |     web application archive will have also picked up any external dependencies | 
|---|
| 254 |     that were included at deployment time.</li> | 
|---|
| 255 | </ul> | 
|---|
| 256 |  | 
|---|
| 257 | <p>For interactive development and testing of your web application using | 
|---|
| 258 | Tomcat 6, the following additional targets are defined:</p> | 
|---|
| 259 | <ul> | 
|---|
| 260 | <li><strong>install</strong> - Tell the currently running Tomcat 6 to make | 
|---|
| 261 |     the application you are developing immediately available for execution | 
|---|
| 262 |     and testing.  This action does not require Tomcat 6 to be restarted, but | 
|---|
| 263 |     it is also not remembered after Tomcat is restarted the next time. | 
|---|
| 264 |     <br><br></li> | 
|---|
| 265 | <li><strong>reload</strong> - Once the application is installed, you can | 
|---|
| 266 |     continue to make changes and recompile using the <code>compile</code> | 
|---|
| 267 |     target.  Tomcat 6 will automatically recognize changes made to JSP pages, | 
|---|
| 268 |     but not to servlet or JavaBean classes - this command will tell Tomcat | 
|---|
| 269 |     to restart the currently installed application so that such changes are | 
|---|
| 270 |     recognized. | 
|---|
| 271 |     <br><br></li> | 
|---|
| 272 | <li><strong>remove</strong> - When you have completed your development and | 
|---|
| 273 |     testing activities, you can optionally tell Tomcat 6 to remove this | 
|---|
| 274 |     application from service. | 
|---|
| 275 |     </li> | 
|---|
| 276 | </ul> | 
|---|
| 277 |  | 
|---|
| 278 | <p>Using the development and testing targets requires some additional | 
|---|
| 279 | one-time setup that is described on the next page.</p> | 
|---|
| 280 |  | 
|---|
| 281 | </blockquote></td></tr></table></td></tr><!--FOOTER SEPARATOR--><tr><td colspan="2"><hr size="1" noshade></td></tr><!--PAGE FOOTER--><tr><td colspan="2"><div align="center"><font size="-1" color="#525D76"><em> | 
|---|
| 282 |         Copyright © 1999-2008, Apache Software Foundation | 
|---|
| 283 |         </em></font></div></td></tr></table></body></html> | 
|---|