Web Archives

There are several ways to build a WAR file.  Also see the page on Eclipse.

Using Ant

Ant is like Make, but for Java.  Instead of Makefiles, Ant uses XML files to describe build targets.  You can use Ant to create a WAR from the command-line.

Project Folder

First, you'll need to create a directory structure and web.xml file for your project:


$ mkdir myapp
$ cd myapp
$ mkdir docs
$ mkdir src
$ mkdir web
$ mkdir web/WEB-INF

Web Application Deployment Description File

Put your web.xml file in web/WEB-INF.  An example web.xml file is as follows:


<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4">


    <display-name>Hello, World Application</display-name>
    <description>
This is a simple web application with a source code organization
based on the recommendations of the Application Developer's Guide.
    </description>


    <servlet>
        <servlet-name>HelloServlet</servlet-name>
        <servlet-class>mypackage.SayHello</servlet-class>
    </servlet>


    <servlet-mapping>
        <servlet-name>HelloServlet</servlet-name>
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>


</web-app>

Build File

Ant should already be installed and configured on your Debian machine if you have Tomcat installed.  All you need is a build file (analogous to a Makefile).  You can download one from Tomcat's website that should do the trick:


$ wget -O build.xml http://tomcat.apache.org/tomcat-6.0-doc/appdev/build.xml

Build Properties File

Next you need a small text file which specifies parameters to your build file.  Create a file called build.properties and fill it with the following text:


catalina.home=/usr/share/tomcat6

Build with Ant

Now you are ready to build your project with Ant.  At the top-level directory of your project, type:


$ ant

If you did everything right, you should see a new 'build' folder in your project.

Build a WAR with Ant

Next you can produce a WAR file from your compiled code:


$ ant dist

You should find your WAR file in the 'dist' folder.

Upload WAR to Tomcat

You can now upload your war file to your Tomcat server using the Manage servlet through your browser.  To make things easier, you can also have Ant deploy your WAR file to Tomcat automatically.  This will save you a lot of time if you need to deploy your web app often.

To allow Ant to deploy your web app for you, you need to tell Ant your Tomcat password.  You should have a username and password set in /etc/tomcat6/conf/tomcat-users.xml (you need to be root to see and edit this file).  Make sure your user has a 'manager' role.  Copy your Tomcat manager's username and password to your build.properties file.  Also, you must specify the URL of your Manager servlet.  Now you have:


catalina.home=/usr/share/tomcat6
manager.username=myusername
manager.password=secret
manager.url=http://virtualNN.cs.missouri.edu:8180/manager

Now Ant can deploy your project for you:


$ ant install

Your new web app will start running on Tomcat automatically.

Comments