aspose file tools*
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
Author

Maven + Spring + Multiple Environments + Properties files

Niall Loughnane
Ranch Hand

Joined: Dec 07, 2006
Posts: 208
Hi,

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

There is 1 default properties file "spring_app.properties",

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

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 "spring_app.properties"?

Is the best approach is to use propertyoverrideconfigurer?

Thanks in Advance,

Niall
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1648
    
    7

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 spring.profiles.active 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
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

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)

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Maven + Spring + Multiple Environments + Properties files