Search code examples
javatomcatjndicontext.xml

Tomcat updates context.xml but should not


Tomcat documentation (http://tomcat.apache.org/tomcat-6.0-doc/config/context.html)

Only if a context file does not exist for the application in the $CATALINA_BASE/conf/[enginename]/[hostname]/, in an individual file at /META-INF/context.xml inside the application files. If the web application is packaged as a WAR then /META-INF/context.xml will be copied to $CATALINA_BASE/conf/[enginename]/[hostname]/ and renamed to match the application's context path. Once this file exists, it will not be replaced if a new WAR with a newer /META-INF/context.xml is placed in the host's appBase.

Text in bold is clear. But I notify as not true. (I need exactly this behaviour)

Here are steps:

  1. aaa.war with context.xml included into META-INF
  2. copy to webapps. tomcat deploys. File conf\Catalina\localhost\aaa.xml OK
  3. change aaa.war by changing META-INF/context.xml
  4. copy to webapps
  5. File conf\Catalina\localhost\aaa.xml is changed!!!

What is wrong? Is it a bug or a hidden feature?

Background - the issue I want to resolve:

Configuration independent build and installation. I plant to ship war file. Client per-configures his Tomcat using his own settings. when I ship a new release I just delivery the war without config and when deployed it will use client specific configuration.

By documentation the best place is the context.xml But if it overwritten each time it make no sense to use at all. (why would I use JNDI and such a things if new deliverable has to created? Changing something in build-time is not a big configuration advantage.)

Environment: windows, tomcat 6.0.33


Solution

  • I've run into the same problem and the documentation is at least inadequate, if not directly misleading.

    The problem is, that if you copy a new version of an already deployed WAR file to the webapps directory, Tomcat will not redeploy the existing application but undeploy the old application and then deploy the new application. The difference may first seem insignificant, but the problem is that during undeployment of the old application, any context files in conf/Catalina/localhost are deleted as well. Then, the new context file from the current WAR file will be copied to conf/Catalina/localhost during deployment of the new application.

    The only feasible solution I've found is not to deploy the new application as a WAR file, but to replace or overwrite the expanded directory, e.g. not to copy aaa.war to webapps/ but to unpack aaa.war directly into webapps/aaa/.