Some teams prefer having a separate Maven build profile for each application runtime environment, like dev, test, prod, etc. In this article, I going to show you how to connect Maven profiles with Spring Boot profiles. You will learn how to set an active Spring profile using Maven’s pom.xml file.
Are you ready?
Advertisement
Separate application.properties for Spring profiles
You’re going to use Spring Boot application.properties file to activate one of the profiles. If you don’t have the file in your application resources, it’s the right moment to create one.
Usually, we want to select the active Spring profile with Maven to build a JAR/WAR file with a chosen configuration file. This demo will show you how to do it.
What will you need?
First of all, create two additional configuration files. The names of the files should match with the pattern application-{profile}.properties. Add the files to the src/main/resources directory of your Maven project, next to the main application.properties file.
Maven profiles
Then it’s time to modify your pom.xml. You created two configurations files so now you need to define two Maven profiles.
Inside both Maven profiles, define a custom property and call it, for instance, activatedProperties. The name doesn’t really matter. What is more important is its value.
The value of your custom Maven property should match with Spring profiles that you want to activate with the corresponding Maven profile. In our sample, we called them dev and release.
In addition, you can mark one Maven profile to run by default. The following sample activates the first profile if you don’t select one on your own.
<profile> <id>dev</id> <properties> <activatedProperties>dev</activatedProperties> </properties> <activation> <activeByDefault>true</activeByDefault> </activation> </profile> <profile> <id>release</id> <properties> <activatedProperties>release</activatedProperties> </properties> </profile>
Maven resource filtering
Let’s stay in pom.xml for a moment. In the previous step, you defined a mapping between Maven and Spring profiles. But you still need to transfer profile mapping into your Spring Boot application.
How?
You’re going to use Maven to set Spring property called spring.profiles.active in the application.properties file with the values from defined Maven profiles. As the name suggests, this property is responsible for activating Spring Profiles.
In order to do so, you need to allow Maven to manipulate resource files as a part of the build process. You do this by enabling filtering for the Resources Plugin in pom.xml. However, if you use spring-boot-starter-parent as the parent of your pom.xml, you can skip this step.
<build> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> … </build>
Now it’s time for the last step. Add the following line to the application.properties file.
spring.profiles.active=@activatedProperties@
The value between @ characters must match with the custom property in your Maven profiles.
How does it work?
When you run the build, the Resources Plugin will replace the activatedProperties placeholder in application.properties with the property from the currently active Maven profile.
After starting your application, the Spring framework activates Spring profile as defined in the filtered application.properties file. Next, the framework reads additional application-{profile}.properties file based on the active profile.
A word of caution: Spring Boot 1.3 replaced the default Resources Plugin syntax for filtered values. Not, the framework uses @activatedProperties@ instead of ${activatedProperties} notation.
Just build it
At this point, you should know how to activate Spring profiles with Maven. As you see, creating a mapping between Maven profiles and Spring profiles is pretty easy. Now you know how to select Spring configuration using Maven profile.
In case of any issues, improvements, or questions, don’t hesitate to leave a comment. Please consider subscribing to my site if want to know about future articles.
this is awesome thanks!
You’re welcome! I’m glad you found it helpful.
nice work!
Thank you. I appreciate your feedback.
Great, but this doesn’t work for me.
I have a pom.xml with 2 profiles, but the @..@ placeholders are not resolved without using tag.
Hi Alessandro. The tag is actually optional. You just need to make sure that just one of your profiles is activated when you run the build.
I have the same Problem.
If I use the code without active by default as true, my build failed.
for deploying i use
mvn clean package -Dtest.productionMode
and the part in my pom is
production
test
test.productionMode
...
org.springframework.boot
spring-boot-maven-plugin
true
-Dtest.productionMode
What is wrong? :-/
Thanks a lot ! This is awesome.
Always glad to help!
My maven profiling is working fine but how can i read a key from a property file
Thank you for post. Tell please how influences to this approach? When I use dependencyManagement in multimodules project @activatedProperties@ doesn’t change.