Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does tomcat replace context.xml on redeploy?

Documentation says if you have a context file here:

$CATALINA_HOME/conf/Catalina/localhost/myapp.xml

it will NOT be replaced by a context file here:

mywebapp.war/META-INF/context.xml

It is written here: 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.

But everytime I re-deploy the war it replaces this myapp.xml with the /META-INF/context.xml!

Why does it do it and how can I avoid it?

Thanx

like image 474
artemb Avatar asked Oct 27 '10 11:10

artemb


2 Answers

Undeploy part of redeploy deletes app and the associated context.xml.

If you use maven tomcat plugin you can avoid deleting context.xml if you deploy your app with command like this:

mvn tomcat:deploy-only -Dmaven.tomcat.update=true

More info here: https://tomcat.apache.org/maven-plugin-2.0-beta-1/tomcat7-maven-plugin/deploy-only-mojo.html

You can use deploy-only with parameter mode to deploy the context.xml too.

like image 89
petro Avatar answered Sep 18 '22 14:09

petro


The short answer:

Just make the TOMCATHOME/conf/Catalina/localhost dir read-only, and keep reading for more details:

  • For quick deployment mode (Eclipse dynamic web project, direct Tomcat connection, etc.) on a local/non-shared Tomcat server you can just define your JDBC datasource (or any other 'web resource') using the META-INF/context.xml file inside the WAR file. Easy and fast in your local environment, but not suitable for staging, QA, or production.
  • For build deployment mode (usually for staging, QA, or prod), JDBC datasources and other 'web resources' details are defined by the QA/production team, not the development team anymore. Therefore, they must be specified in the Tomcat server, not inside the WAR file anymore. In this case, specify them in the file TOMCATHOME/conf/Catalina/localhost/CONTEXT.xml (change Catalina by the engine, and localhost by the host, and CONTEXT by your context accordingly). However, Tomcat will delete this file on each deployment. To prevent this deletion, just make this dir read-only; in Linux you can type:

       chmod a-w TOMCATHOME/conf/Catalina/localhost
    

    Voila! Your welcome.

The long answer

  • For historical reasons Tomcat allows you to define web resources (JDBC datasources, and others) in four different places (read four different files) in a very specific order of precedence, if you happen to define the same resource multiple times. The ones named in the short answer above are the more suitable nowadays for each purpose, though you could still use the others (nah... you probably don't want to). I'm not going to discuss the other ones here unless someone asks for it.
like image 35
user4691115 Avatar answered Sep 19 '22 14:09

user4691115