views:

259

answers:

5

I have an application running in tomcat that has a bunch of configuration files that are different for each environment it runs in (dev, testing, and production). But not every line in a config file will be different between environments so there's invariably duplicated information that doesn't get updated if something changes.

Is there a good framework/library that collapses the separate files into one with environment specific blocks? Or some other way of dealing with this?

+1  A: 

A Properties file is what I've always used. It's editable by hand as well as in in your software and the Properties object can read itself in and write itself out to the filesystem. Here's the javadoc page:

http://java.sun.com/j2se/1.4.2/docs/api/java/util/Properties.html

Grant Limberg
A: 

The duplication is not really a problem, having a central config file the the other files 'extend' is likely to casue more of a headache in the long term.

My advice is to use ant to load (copy and move) the appropriate file(s) into place and then launch the app (bundle into war?). Just have a different task for each environment. So you will have three config files (dev.config, test.config and production.config) which will be moved and overwrite the config in the /WEB-INF folder depending on the task that you are running.

Hyposaurus
along the same lines you could have a sinlge build task that takes a required parameter of the config file name. if this is missing the build will fail.
shsteimer
+1  A: 
  1. Assign reasonable default values for all properties in the properties files distributed within your .war file.
  2. Assign environment-specific values for the appropriate properties in webapp context (e.g. conf/server.xml or conf/Catalina/localhost/yourapp.xml)
  3. Have your application check the context first (for the environment-specific values), and fall back on the default values in the app's properties values if no override is found.
Eric Rath
I like to put the environment specific values in the server.xml and use resource links in the yourapp.xml file for the individual webapps.
bmatthews68
+1  A: 

If you use maven, you can use it's resource filtering abilities, along with profiles to generate a properties file for each environment you're deploying into.

As an added bonus, maven can also deploy your web app for you.

tunaranch
A: 

I would suggest to have a separate config file for environment parameters alone if you want to avoid cluttering. Then you will have one more config file to manage. This is a trade off between number of config files vs complexity of each config file.

Rejeev Divakaran