• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Maven + Spring + Multiple Environments + Properties files

 
Niall Loughnane
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic