Tomcat

Tomcat ROOT.war deployment

If you wish to avoid having to deploy a web application explicitly named as ROOT.war – so that you use the default context URL “/” without having to resort to web server URL rewrites – it’s possible to do this and still maintain your application war files original name.

I thought it was worth writing about our approach to this as the Tomcat documentation isn’t particularly clear on how to achieve this.

As some background information, I work for a large FTSE100 company where a team other than the Development Team are responsible for the deployment of the application, and handing them a file called ROOT.war is a little unclear and could potentially lead to misunderstandings.

It’s possible to have the application context file point to a .war file outside of the Tomcat/webapps directory. Tomcat will then explode your war file, whatever it’s name, into the Tomcat/webapps/ROOT directory. Tomcat infers from the name of the context file that web application should be deployed as the default, accessible as root context “/”, without any need for web server URL rewrites.

<Context docBase="/<external_build_dir>/<application_name>.war" />

where <external_build_dir> can be any directory outside of Tomcat, for example your application build directory, and <application_name> can be the proper full name of your application.

If you are using Apache Ant deployment scripts, you can also make good use of a couple of features to automate this whilst maintaining naming clarity. We have a simple application context file <application_name>.xml:

<Context docBase="@DEPLOY_FILE@" />

and the associated application deployment script copies this file to the Tomcat conf directory, renaming it as ROOT.xml as so:

<copy file="${ant.project.name}.xml" tofile="${tomcat}/conf/Catalina/localhost/ROOT.xml">
  <filterset>
    <filter token="DEPLOY_FILE" value="${project.name}.war"/>
  </filterset>
</copy>

As you can see it’s also using Ant’s filter task with a token to express the location of the application war file, pointing back at the build directory.

Of course, you can still use an Apache HTTP web server RewriteRule to serve your application from “/”, but this would apply to every request served:

RewriteRule /<application_name>/(.*) /$1

BOOTNOTE: Thanks to pid-2 for pointing out my initial mistake in this post.

There’s no need to set the path attribute of the context xml file, in fact Tomcat specifically states that you should not do this. The fact that you have a context file called ROOT.xml is enough for Tomcat to infer that you wish to deploy the web application as the root “/”.

Tags: , ,

Saturday, March 21st, 2009 Tomcat 2 Comments

Agile CTO