To spare anyone else the time and effort of trying to deploy to a different path using META-INF/context.xml, and with respect to Pavel's answer and others, it would seem that it is simply not possible (in tomcat 9.x).
I was hoping to deploy to the root context through configuration of the war's META-INF/context.xml, but it would seem that it's not currently possible (tomcat 9.x).
With Host copyXML="true", the context.xml is deployed (to tomcat's conf/Catalina/<host>
) using the war's base name. Due to path configuration rules, this then takes precendence in determining the path. That is, the path is named according to the .xml's base name - hence the war's basename. As the docs and the answer mentioned address, the Context path
field is not respected unless it is used in the server.xml.
This is actually in the docs but is not immdiately obvious and you have to piece it together a bit, especially as the path
attribute is readily available to the META-INF/context.xml
.
See:
Tomcat 9.0/ Config Guide/ Naming
When autoDeploy or deployOnStartup operations are performed by a Host, the name and context path of the web application are derived from the name(s) of the file(s) that define(s) the web application. Consequently, the context path may not be defined in a META-INF/context.xml embedded in the application and there is a close relationship between the context name, context path, context version and the base file name (the name minus any .war or .xml extension) of the file.
If you want to deploy a WAR file or a directory using a context path that is not related to the base file name then one of the following options must be used to prevent double-deployment:
Disable autoDeploy and deployOnStartup and define all Contexts in server.xml
Locate the WAR and/or directory outside of the Host's appBase and use a context.xml file with a docBase attribute to define it.
Tomcat 9.0/ Config Guide/ Defining a Context
- Individual Context elements may be explicitly defined:
- In an individual file at /META-INF/context.xml inside the application files. Optionally (based on the Host's copyXML attribute) this may be copied to $CATALINA_BASE/conf/[enginename]/[hostname]/ and renamed to application's base file name plus a ".xml" extension.
Tomcat 9.0/ Config Guide/ Defining a Context/ Common Attributes/ path 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.
It seems to me, after a lot of play, that the only use of the META-INF/context.xml is in defining other configuration items of an application on deployment - e.g. resource configuration - though I'd be interested to know other's thoughts.
See Pavel's answer and others (below) for how to deploy to a different context path and to the root context (without using META-INF/context.xml - no autodeploy here(!)):