Question
I have an application with maven as a build tool.
I am using maven profiles to set up different properties from different profiles.
What i would like to do is that all active profiles in maven will be ported to
spring active profiles as well so i can reference them in bean signature
(@profile
). but i am not sure how to do it.
for example: consider the following maven setup
<profiles>
<profile>
<id>profile1</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
</properties>
</profile>
<profile>
<id>profile2</id>
<properties>
</properties>
</profile>
<profile>
<id>development</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
</properties>
</profile>
<profile>
<id>production</id>
<properties>
</properties>
</profile>
</profiles>
assuming i run maven with out specifying any other profiles i would like for
spring to have profile1
and development
as active profiles.
Answer
You'll have to filter a resources of your application, for instance a property file, that holds the information of which profile to activate in spring.
For instance
spring.profile = ${mySpringProfile}
And for each profile, define a value for this variable (mySpringProfile
).
During the build, this will be filtered accordingly to the value defined in the currently active profile.
Then during the bootstrap of your application you'll select the appropriated profile according to this file (can't help you more as you didn't gave us more information, but this is quite easy.
Note : I can't find a way to get the currently active profile in maven (something like project.profiles.active that holds your -P values), that's why you'll have to set a new variable for each profile.
Note 2 : if you are running a web application, instead of using this intermediate file, filter this value in your web.xml
<context-param>
<param-name>spring.profiles.active</param-name>
<param-value>${mySpringProfile}</param-value>
</context-param>
Note 3 : This is actually a bad practice, and you should set the profile at runtime with a system property