It's not a secret anymore!
The moose likes Spring and the fly likes Maven + Spring + Multiple Environments + Properties files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Maven + Spring + Multiple Environments + Properties files" Watch "Maven + Spring + Multiple Environments + Properties files" New topic

Maven + Spring + Multiple Environments + Properties files

Niall Loughnane
Ranch Hand

Joined: Dec 07, 2006
Posts: 208

I have a Spring application that is deployed on multiple environments (dev, test, lab etc.),

There is 1 default properties file "",

Each of the environments has their own properties files, i.e.:
* dev -
* test -
* lab -

Is there any best practice for how to configure maven to build the Spring app for a specific maven profile (i.e. dev) and to override the default ""?

Is the best approach is to use propertyoverrideconfigurer?

Thanks in Advance,

Bill Gorder

Joined: Mar 07, 2010
Posts: 1682

Are you using Spring 3.1?

What I typically do here is use Spring Profiles. To make this work you just make sure to set variable in your environment somewhere (JNDI, System variable etc), when Spring picks it up it uses it initialize the correct profile.

For example (in java config)

Now since these are PropertySources I can access them like this

Or alternatively I could use @Value etc as well

[How To Ask Questions][Read before you PM me]
Mark Spritzler

Joined: Feb 05, 2001
Posts: 17276

Or if you aren't using Spring 3.1, Maven has a concept of profiles too. And anything you put into the resources directory will get copied into your classpath. So a Maven profile approach is to create a directory for each profile in your resources directory. In each directory has your properties file, and in this case it would be best to give it the same name in each directory. Then in your Spring config you just use that one name. When you run your maven build you set your profile to one of the profiles and it will copy out the file from the correct directory. Each directory might ahve to have the exact same name as the profile, or some setting in your pom or better settings.xml for Maven pointing to the corresponding directory in resources for each profile.

Or you can also use Spring Expression language to dynamically point to the correct file. (A little more complex)


Perfect World Programming, LLC - iOS Apps
How to Ask Questions the Smart Way FAQ
I agree. Here's the link:
subject: Maven + Spring + Multiple Environments + Properties files
It's not a secret anymore!