Deploying war file to Tomcat with a different path

z12345 picture z12345 · Mar 1, 2012 · Viewed 68.5k times · Source

If I deploy a war file to Tomcat, called for example foo-bar-1.1.2.war, how can I deploy it so that it is extracted to webapps/bar and its URL root is /bar/...?

My intention here is to keep the war file in the webapps server with its version information so that I know which version is installed but have it overwrite a previous version of the app.

I could deploy the war file using PSI Probe. This would allow me to specify a target context for the web app. However, it means that I would lose any version information in the war file name.

Answer

Michael picture Michael · Mar 1, 2012

Tomcat will always extract the contents of a war file, to a folder of the same name (when it's configured to deploy wars - as default etc.).

You can extract it to a folder name of your choice. So if you unzip the contents of foo.war to a folder called bar/ manually, instead of just dropping the war into the web apps folder, it'll still load the web application.

However, this is totally unnecessary as you can specify the URL pattern of the application without messing with the folder / war file name at all by overriding the context root element for your application:

This is often set in the Tomcat server.xml - but that practice is fairly widely discouraged. Instead, I'd suggest you use context.xml in the META-INF folder of your web application / war file:

<Context path="/bar" .../>

When the application is deployed, the context.xml should be copied to /conf/Catalina/localhost but renamed to foo.xml

Note that conext roots must be unique and there are some additional considerations if you're using the autoDeploy or deployOnStartup operations (Source http://tomcat.apache.org/tomcat-7.0-doc/config/context.html).


Other options include:

  • Clean the web apps folder each deployment and drop your new foo-1.1.0 war in.
  • Include the version number in a flat file. foo/version1
  • Or simply include the version in a config / XML file.

You could also use Ant (or an equivalent tool) to automate your deployments (and perform any of the above).