6
votes

How can I deploy mywebapp-1.0.0.war to $TOMCAT_HOME/webapps directory with context path /mywebapp using context.xml inside the war file on Tomcat 8?

I'm getting back to work with Tomcat after long time since version 5. I'm used to create META-INF/context.xml inside my war file:

<?xml version="1.0" encoding="UTF-8"?>
<Context path="/mywebapp">
  ...
</Context>

Maven creates a war file with this name: mywebapp-1.0.0.war

But when I deploy the war file to $TOMCAT_HOME/webapps directory the context path will be http://localhost:8080/mywebapp-1.0.0 instead of http://localhost:8080/mywebapp/.

Also I see that $TOMCAT_HOME/conf/Catalina/localhost is empty, instead of having the xml file copied from the war file deployed.

I also added to $TOMCAT_HOME/conf/server.xml the deployXML="true"

 <Host name="localhost"  appBase="webapps"
        unpackWARs="true" autoDeploy="true" 
        deployXML="true">
3
Are you sure maven packs your war as expected? Do you put context.xml under src/main/webapp/META-INF? Also note the you can add <finalName>${artifactId}</finalName> to pom build section. This will solve your problem - Ori Dar
thanks @orid, and yes I'm sure maven packs the war fine. I tried in tomcat 5 now, and I had the same problem. Your solution works, but I'm wondering what is wrong with the context path on my context.xml inside the war. - j2gl
I don't think anything is wrong with it. It may be some other attribute or sub-element which is not valid. Try set a DEBUG logging level for org.apache.catalina to see what is going on - Ori Dar
After trying a lot, I found the answer reading Tomcat 8 documentation. - j2gl

3 Answers

8
votes

It is not possible to put a war inside the webapp directory and set the path attribute in META-INF/context.xml file at the same time. Tomcat 8 documentation, clearly says about this attribute:

This attribute must only be used when statically defining a Context in server.xml. In all other circumstances, the path will be inferred from the filenames used for either the .xml context file or the docBase.

1
votes

Can't you just rename your war file to mywebapp (via Maven or else) so that Tomcat will deploy it under /mywebapp ?

-1
votes

Thanks for your research j2gl! I've found out that good way how to achieve both .war file with full name with version and deployed short path is to use Tomcat manager API. For example through Tomcat7-maven-plugin.