making environment specific property files in spring mvc

Rohit Bajaj
Rohit Bajaj used Ask the Experts™
on
HI,
I am having following project structure :
resources->properties ->[database.properties, log4j.properties]
I want to create a new project structure in which inside the properties folder there will be following folders
DEV
PROD
QA
each having there own database.properties and log4j.properties file.

I am using maven with spring mvc. How do i make this possible so that when i build the project the right property file gets picked up based on the system environment variables.
eg. if the system environment variable env = DEV then properties files under the dev folder should be used.

Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
IT Business Systems Analyst / Software Developer
Top Expert 2015
Commented:
You will need to fiddle with your directory layout a little bit. Are you able to lay it out like this...

src
     |_  main
                 |_  DEV
                 |              |_  resources
                 |                                    |_  properties
                 |                                                          |_  database.properties, etc
                 |_  PROD
                 |              |_  resources
                 |                                    |_  properties
                 |                                                          |_  database.properties, etc
                 |_  QA
                                |_  resources
                                                      |_  properties
                                                                            |_  database.properties, etc



If so then it is quite easy to configure, you just need to add/merge the following xml into your pom...

    <build>
        <resources>
            <resource>
                <directory>${basedir}/src/main/${env.env}/resources/</directory>
            </resource>
        </resources>
    </build>

Open in new window


I say add or merge because you may already have a <build> section in your pom, and possibly already a resources section. If so you just need to modify what you have got so that it includes the ${env.env} part in the directory path.

If you don't already have these sections, then you can just add them in.

Explanation...
Basically you are just telling the maven-resources-plugin where to find the files that it will operate on. ${ } is used to substitute properties into the configuration. ${basedir} is just a Maven built-in property for the base directory of the project. ${env.env} is how Maven refers to system environment variables, the first "env" tells Maven to look at the system environment, and the second "env" is the name of the environment variable to use.

Refer to the below for information on Maven properties...
https://maven.apache.org/pom.html#Properties

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial