15

I'm using Tomcat 7 and would like to set the context root of a war file in the war file itself and have Tomcat autodeploy and pick up this path. I thought I found the way to do it by putting a context.xml in the META-INF directory of the war which contains.

<?xml version="1.0" encoding="UTF-8"?>
<Context path="/somepath/myapp"/>

But this doesn't seem to work, I think it's loaded by http://tomcat.apache.org/tomcat-7.0-doc/api/org/apache/catalina/startup/SetContextPropertiesRule.html which states it loads everything but the path!

I know I can name the war somepath#myapp.war and it will pick it up but I also deploy to weblogic which isn't happy with a name like this.

Is there some setting I can use to have the path work from the context.xml above?

Thanks David

David Kerwick
  • 478
  • 1
  • 4
  • 11

4 Answers4

15

The Context path attribute is ignored unless the path is specified in a hard-coded Context in server.xml, which is strongly discouraged, and doesn't take multilevel paths.

The name of the war file, or the name of the Context xml file in tomcat/conf/Catalina/hostname becomes the path of the deployed application.

In your case the latter of the two above is the solution, just make sure you put the .war file outside of the designated appBase for the Host, or you'll deploy the app twice.

In: conf/Catalina/localhost/myapp#path.xml

<?xml version="1.0"?>
<Context docBase="/some/path/to/myapp.war">
</Context>
Pidster
  • 628
  • 4
  • 9
  • 2
    That solved the same problem for me too thank you. However, I believe it's a Tomcat's flaw to ignore the path attribute. The documentation even says the opposite. Worse, docBase seems to be ignored by Tomcat 7.0.22, so the only way is to use these stupid hashed names for both context.xml and the .war. #Sigh... – zakmck Dec 05 '12 at 14:13
  • 1
    It's not stupid. You can't use the / (or perhaps \) character in a filename for obvious reasons. The path attribute should be removed IMO. It confuses people too much. – Pidster Dec 12 '12 at 18:27
  • 2
    I have nothing against hashes, I think it's bad that I'm forced to determine the context path via the file name, instead of the parameters path and docBase. For instance, this way in practice I cannot deploy any file named like myapp-v1.2.3beta.war. OK, I could set up an alias, but the other way would be better. – zakmck Dec 12 '12 at 20:32
  • 1
    Sure you can: myapp##v1.2.3beta.war /myapp – Pidster Dec 16 '12 at 09:19
  • Sure you can. But if you mean you want to include the version, but not use it: myapp##v1.2.3beta.war publishes on: /myapp The docBase attribute has nothing to do with the published path, it's just a reference to the location of the application. If you forget about the path attribute, you should see that it is *far* more clear to use the application filename as the path. – Pidster Dec 16 '12 at 09:26
  • Good to know, thanks. Though IMHO that's still clumsy, I'm used to name my files myapp-v1.2.3beta.war, not the awkward myapp##v1.2.3beta.war. – zakmck Dec 16 '12 at 21:36
  • Hi, Is that possible to dynamically populate this docBase attribute at build time? My requirement is to populate it with the latest build number from maven. please reply – dharam May 27 '13 at 12:03
  • Hi dharam. It should be possible via resource filtering (http://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html). However, I prefer to do it via Tomcat-deployment script: https://github.com/EBIBioSamples/myequivalents/blob/master/myequivalents-web/deploy2tomcat.sh. This allows you to build once and deploy on multiple Tomcat installations, and also to deploy without Maven. – zakmck Jul 14 '13 at 10:31
  • 5
    I agree with you @zakmck it's a bad idea the context path it's determined by the war file name, instead of attribute "path" in META-INF/context.xml file. It's confusing and took me a lot of time to find in [Tomcat documentation](https://tomcat.apache.org/tomcat-8.0-doc/config/context.html#Common_Attributes) this statement: "This attribute (path) must only be used when statically defining a Context in server.xml ..." – j2gl May 15 '15 at 06:21
  • 1
    Please could Pidster or anybody else expand on the statement: Sure you can: myapp##v1.2.3beta.war /myapp – user835745 Nov 28 '16 at 15:24
2

In /tomcat7/conf/server.xml add below lines inside the element and restart Tomcat to make changes take place.

*change "mycom" to your application name.

<Context path="" docBase="mycom">
  <!-- Default set of monitored resources -->
  <WatchedResource>WEB-INF/web.xml</WatchedResource>
</Context>
<Context path="ROOT" docBase="ROOT">
  <!-- Default set of monitored resources -->
  <WatchedResource>WEB-INF/web.xml</WatchedResource>
</Context>

This will make the default root application appear under context root "/ROOT".

Now your app is accessible at "/" and "/mycom" as well!

1

add copyXML="true" to Host config inside $TOMCAT_HOME/conf/server.xml like

<Host name="localhost"  appBase="webapps" unpackWARs="true" autoDeploy="true" copyXML="true">
  • I was hoping that this would work, but it ended up still ignoring the value. this used to work in older versions of Tomcat, but not in Tomcat 7. – Ben L. May 31 '16 at 21:42
0

If you load your application.war to a directory Tomcat isn't aware of, how can it read anything in your war file? The correct place to add this information is in $TOMCAT_HOME/conf/context.xml - this is part of Tomcat and Tomcat can read this file and find out where your application is and deploy it. There's more on how to set up JNDI in Tomcat

  • Welcome to Stack Overflow! Thanks for posting your answer! Please be sure to read the [FAQ on Self-Promotion](http://stackoverflow.com/faq#promotion) carefully. Also note that it is *required* that you post a disclaimer every time you link to your own site/product. – Andrew Barber Feb 27 '13 at 02:57