If you're interested in functional programming, you might also want to checkout my second blog which i'm actively working on!!

Thursday, May 20, 2010

Multi environment project configuration

Often you need to configure your application for multiple staging environments like 'dev', 'test', 'prod'. I've used the Cocoon Spring Configurator a few times. It let's you configure environment specific properties, create the artifact once and deploy it on multiple staging environments. The only thing you need to set is a system variable to specify runtime behaviour.

But you can actually accomplish the same behaviour with little work as i will explain in this article. Let's take a look at my project structure:

























I created a folder 'properties' in src/main/resources in which i included following property files:
  • dev.properties
  • test.properties
  • prod.properties
  • generic.properties --> shared properties for all environments
To get access to all these properties I specified a PropertyPlaceHolderConfigurer in src/main/resoures/application-context.xml as follows:



The only thing you need to do now is specify a systemvariable called 'environment.id'. In order to run my unit tests automically with maven i added this variable to surefure plugin in the pom.xml.



In my unit test i set the application-context.xml.


The unit test will not run however without the system variable so if you want to run the unit test from within Eclipse for example you need to specify this variable in the run configuration in the second tab 'Arguments' and more specific 'VM arguments'.

Here you can specify it as follows:
-Denvironment.id=dev

No comments:

Post a Comment