mfa mfa - 1 year ago 178
Linux Question

Externalizing Tomcat webapp config from .war file

I am having trouble with configuring a webapp in tomcat 7. In my war file, there is a properties file myApp/WEB-INF/classes/myProps.props, and it contains environment-specific properites. I am trying to override that configuration file on the server, so that the same war file will deploy to multiple environments.

I heard there was a way to do this using replacement config files in tomcat/conf/Catalina/myApp. This is the method I am having trouble figuring out.

Also, myApp.war is one of many running on the same tomcat server, and it does not run as localhost. I want to be able to solve this problem for several of the webapps.

Server version: Apache Tomcat/7.0.23
Server built: Nov 20 2011 07:36:25
Server number:
OS Name: Linux

Answer Source

Your tomcat/conf/Catalina/<host> can contain context descriptors that let you configure lots of things including defining "environment entries", which are accessible from Java via JNDI. There are lots of ways to go about using it. Personally, I set an environment entry which is the file system path to my properties file. My app is built to check for this entry, and if it doesn't exist, look for the file on the classpath instead. That way, in dev, we have the dev properties right there on the classpath, but when we build and deploy, we point it to an external file.

There's good documentation for configuring a context on the Tomcat website. See the "Defining a Context" section on details of how to create the file and where to put it.

As an example, if your host is named "myHost" and your app is a war file named "myApp.war" in the webapps directory, then you could create tomcat/conf/Catalina/myHost/myApp.xml with this content:

    <Environment name="configurationPath" value="/home/tomcat/" type="java.lang.String"/>

Then from your code, you'd do a JNDI lookup on java:comp/env/configurationPath (95% certainty here) to get that string value.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download