## Development Processes

### Table of Contents

- Development Processes
  1. One-Time Setup of Ant and Tomcat for Development
  2. Create Project Source Code Directory
  3. Edit Source Code and Pages
  4. Build the Web Application
  5. Test Your Web Application
  6. Creating a Release

### Development Processes

Although application development can take many forms, this manual proposes a fairly generic process for creating web applications using Tomcat. The following sections highlight the commands and tasks that you, as the developer of the code, will perform. The same basic approach works when you have multiple programmers involved, as long as you have an appropriate source code control system and internal team rules about who is working on what parts of the application at any given time.

The task descriptions below do not assume any particular source code control system but simply identify when and what source code control tasks are typically performed. You will need to identify the appropriate source code control commands for your system.

#### One-Time Setup of Ant and Tomcat for Development

In order to take advantage of the special Ant tasks that interact with the Manager web application, you need to perform the following tasks once (no matter how many web applications you plan to develop).

- Configure the Ant custom tasks. The implementation code for the Ant custom tasks is in a JAR file named `$CATALINA_HOME/lib/catalina-ant.jar`, which must be copied in to the `lib` directory of your Ant installation.

- Define one or more Tomcat users. The Manager web application runs under a security constraint that requires a user to be logged in, and have the security role `manager-script` assigned to then. How such users are defined depends on which Realm you have configured in Tomcat's `conf/server.xml` file -- see the Realm Configuration How-To for more information. You may define any number of users (with any username and password that you like) with the `manager-script` role.

#### Create Project Source Code Directory

The first step is to create a new project source directory, and customize the `build.xml` and `build.properties` files you will be using. The directory structure is described in the previous section, or you can use the sample application as a starting point.

Create your project source directory, and define it within your source code control system. This might be done by a series of commands like this:

    cd {my home directory}
    mkdir myapp <-- Assumed "project source directory"
    cd myapp
    mkdir docs
    mkdir src
    mkdir web
    mkdir web/WEB-INF
    cvs or svn or git ... <-- Add this structure to the appropriate repository

To verify that the project was created correctly in the source code control repository, you may wish to check out the project to a separate directory and confirm that all the expected contents are present.

Next, you will need to create and check in an initial version of the `build.xml` script to be used for development. For getting started quickly and easily, base your `build.xml` on the basic build.xml file, included with this manual, or code it from scratch.

    cd {my home directory}
    cd myapp
    emacs build.xml     <-- if you want a real editor :-)
    cvs or svn or git ... <-- Add this file to the repository

The next step is to customize the Ant properties that are named in the `build.xml` script. This is done by creating a file named `build.properties` in your project's top-level directory. The supported properties are listed in the comments inside the sample `build.xml` script. At a minimum, you will generally need to define the `catalina.home` property defining where Tomcat is installed, and the manager application username and password. You might end up with something like this:

    # Context path to install this application on
    app.path=/hello

    # Tomcat installation directory
    catalina.home=/usr/local/apache-tomcat-11.0

    # Manager webapp username and password
    manager.username=myusername
    manager.password=mypassword

In general, you will not want to check the `build.properties` file in to the source code control repository, because it is unique to each developer's environment.

Now, create the initial version of the web application deployment descriptor. You can base `web.xml` on the basic web.xml file, or code it from scratch.

    cd {my home directory}
    cd myapp/web/WEB-INF
    emacs web.xml
    cvs or svn or git ... <-- Add this file to the repository

Note that this is only an example web.xml file. The full definition of the deployment descriptor file is in the Servlet Specification.

#### Edit Source Code and Pages

The edit/build/test tasks will generally be your most common activities during development and maintenance. The following general principles apply. As described in Source Organization, newly created source files should be located in the appropriate subdirectory, under your project source directory.

You should regularly refresh your development directory to reflect the work performed by other developers.

To create a new file, go to the appropriate directory and create the file. When you are satisfied with its contents (after building and testing is successful), add the new file to the repository. For example, to create a new JSP page:

    cd {my home directory}
    cd myapp/web        <-- Ultimate destination is document root
    emacs mypage.jsp
    ... build and test the application ...
    cvs or svn or git ... <-- Add this file to the repository

Java source code that is defined in packages must be organized in a directory hierarchy (under the src/ subdirectory) that matches the package names. For example, a Java class named `com.mycompany.mypackage.MyClass.java` should be stored in file `src/com/mycompany/mypackage/MyClass.java`. Whenever you create a new file, don't forget to add it to the source code control system.

To edit an existing source file, you will generally just start editing and testing, then commit the changed file when everything works.

#### Build the Web Application

When you are ready to compile the application, issue the following commands (generally, you will want a shell window open that is set to the project source directory, so that only the last command is needed):

    cd {my home directory}
    cd myapp        <-- Normally leave a window open here
    ant

The Ant tool will be execute the default "compile" target in your `build.xml` file, which will compile any new or updated Java code. If this is the first time you compile after a "build clean", it will cause everything to be recompiled.

To force the recompilation of your entire application, do this instead:

    cd {my home directory}
    cd myapp
    ant all

This is a very good habit immediately before checking in changes, to make sure that you have not introduced any subtle problems that Javac's conditional checking did not catch.

#### Test Your Web Application

To test your application, you will want to install it under Tomcat. The quickest way to do that is to use the custom Ant tasks that are included in the sample `build.xml` script. Using these commands might follow a pattern like this:

- Start Tomcat if needed. If Tomcat is not already running, you will need to start it in the usual way.

- Compile your application. Use the `ant compile` command (or just `ant`, since this is the default). Make sure that there are no compilation errors.

- Install the application. Use the `ant install` command. This tells Tomcat to immediately start running your app on the context path defined in the `app.path` build property. Tomcat does NOT have to be restarted for this to take effect.

- Test the application. Using your browser or other testing tools, test the functionality of your application.

- Modify and rebuild as needed. As you discover that changes are required, make those changes in the original source files, not in the output build directory, and re-issue the `ant compile` command. This ensures that your changes will be available to be saved (via your chosen source code control system) later on -- the output build directory is deleted and recreated as necessary.

- Reload the application. Tomcat will recognize changes in JSP pages automatically, but it will continue to use the old versions of any servlet or JavaBean classes until the application is reloaded. You can trigger this by executing the `ant reload` command.

- Remove the application when you are done. When you are through working on this application, you can remove it from live execution by running the `ant remove` command.

Do not forget to commit your changes to the source code repository when you have completed your testing!

#### Creating a Release

When you are through adding new functionality, and you've tested everything (you DO test, don't you :-), it is time to create the distributable version of your web application that can be deployed on the production server. The following general steps are required:

- Issue the command `ant all` from the project source directory, to rebuild everything from scratch one last time.

- Use the source code control system to tag the current state of the code to create an identifier for all of the source files utilized to create this release. This allows you to reliably reconstruct a release (from sources) at a later time.

- Issue the command `ant dist` to create a distributable web application archive (WAR) file, as well as a JAR file containing the corresponding source code.

- Package the contents of the `dist` directory using the tar or zip utility, according to the standard release procedures used by your organization.