• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Maven + Spring + Multiple Environments + Properties files

 
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic