How to Handle Project Configuration

August 26, 2011

Every web project needs some environment-specific configurations. Database credentials, root url, smtp settings, to name a few. It’s a recurring question on stackoverflow, and I’ve seen a lot of variations on the topic, and here I’ll describe the one that I think is best.

  • put all such properties in a .properties file (for example application.properties)
  • have that application.properties sitting outside the web application. It is not bundled with the build
  • provide a command-line option that indicates where that file is located. For example -Dconfig.location=/home/you/config
  • on application startup (in a ServletContextListener usually) load the file and put it in a map. Can be java.util.Properties or a HashMap
  • for spring users – use <context:property-placeholder-configurer location="file://${config.location}/application.properties" . Other frameworks will likely have some mechanism for loading such global properties
  • hold a skeleton properties file in the SCM repository. It should contain all properties, but their values are irrelevant – they will change on each environment
  • the ops team is likely to benefit from versioning different environment configurations (production, qa, stage), so a separate /config folder/subproject can be created and all environment-specific properties can be stored there. When adding a property developers should go and update all files accordingly
  • properties that are not dependent on the environment, but are still global for the project, can be stored within the project (src/main/resources for maven), and committed to SCM. They can be merged with the external properties on startup (merged in memory, that is)
  • most of the externalizable properties can have reasonable defaults – the smtp server of the company, the database driver, etc. They can be placed in a application-defeault.properties within the project, and the external file can override them. This is just an option – if you are going to have a file committed in the repository with those reasonable defaults, and each environment to use that file as a basis, it’s virtually the same

Developers can easily run their projects that way. Ops can easily deploy builds on different environments. The build remains environment-agnostic.

14 Responses to “How to Handle Project Configuration”

  1. ‘s/property-placeholder-configurer/property-placeholder/’ – do not confuse with PropertyPlaceholderConfigurer class name. Nice wrap-up BTW.

  2. […] It should be external, in the same way (and the same location, preferably) as the other externalized project configurations. You should then load it based on a system “config.location” […]

  3. What do you think are the pros/cons of storing the configuration properties in a database instead of using a properties file?

  4. You have to configure database access somehow, and for that you will need a properties file. Another thing is initialization – you will need database scripts to initialize your database. And what about in-memory database for tests – initialize them as well. Then versioning – operations can use VCS to store properties files, while it will be a bit harder to track that in the DB (it’s not hard, but it’s harder). In short – storing them in DB adds unneeded complexity.

  5. Thanks for article. Nice to see that’s an approach we have set for our project independently on your article :-)

    With only one difference: we deploy many copies of the same war (content is the same but name and configuration differ) into one Tomcat server. Hence we can’t rely on system properties, instead we have to derive properties file name from war file name. For example: for app1.war there is app1.properties, for app2.war there is app2.properties.

  6. Well, and how about picking up changes in properties at runtime? This is an important aspect of maintaining live servers, since one cannot afford of having downtime just for the sake of configuration changes. I believe in such cases having config stored in DB is the cleanest solution.

    What do you think?

  7. There are ways to make .properties reloadable. Take log4j/logback for example – if you change logging configuration at runtime, it is reflected without start/stop.

  8. […] کار باید به صورت خارجی (external) انجام شود؛ همانند بقیۀ پیکربندیهای خارجی پروژه. شما باید فایل این پیکربندی را بعدا بر اساس یک متغیر […]

  9. Where should we store a dynamic configuration, changed by Ops through a WebUI ?
    Properties are key-value pairs. Is it simpler to manage in a file, or in a DB ? With a file you can easily save/restore, but you loose transaction.

    As of webapps, i recommend to use the web.xml entry “resource-env-ref”, and obtains the value through JNDI. With tomcat, you simply provide a context.xml with the right config file URL.

  10. Now that cloud based applications are getting more common (Red Hat OpenShift, Java EE 7) it is not a good idea to depend on runtime parameters. The developer has no control over these runtime and environment variables anymore.

    The best way and cleanest solution is to store application specific properties in the database. A database connection has to be configured by a datasource within the application server.

  11. Hi there, While analysing backlinks for some mysterious Google rankings, I found some interesting links originated from your WP blog. It looks like your site has been hacked as it seems pretty unlikely you d normally link out to websites that sell Viagra. Don t worry this happens a lot of the time and there are some simple steps you can take to fix the problem. Read this Secure WordPress for a background on typical vulnerabilities and best practices against hacking

  12. […] How do you switch the implementations, depending on whether you are in development or in production mode? It depends on the framework you use. With spring you can simply have @Resource("${service.implementation}") ExtractionService service, where service.implementation is externally configured. […]

  13. I do agree with all of tthe ideas you have presented for your post.
    They’re very convincing and can defnitely work. Still, the posts are very brief for novices.
    Could you please extend them a biit from next time?

    Thanks for the post.

  14. Swapping with a rainbow sprinkle candy will change the total row
    or column of candies to stripped candies.

Leave a Reply