Categories: Spring

Activating Spring Boot profile with Maven profile

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.

Daniel

Share
Published by
Daniel

Recent Posts

Does Java have default parameters?

Short answer: No.Fortunately, you can simulate them.Many programming languages like C++ or modern JavaScript have…

4 years ago

Understanding JavaScript Promise

The JavaScript Promise is a concept that every modern self-respecting web developer should be familiar…

5 years ago

What is Spring bean?

In short, a Spring bean is an object which Spring framework manages at runtime. A…

5 years ago

Injecting Spring Prototype bean into Singleton bean

Have you ever wonder why singleton is the default scope for Spring beans? Why isn't…

5 years ago

How to bind @RequestParam to object in Spring

Do you have multiple parameters annotated with @RequestParam in a request mapping method and feel…

5 years ago

The JavaScript runtime environment

Have you just started learning JavaScript? Or maybe you already have some language experience but…

5 years ago