Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configure active profile in SpringBoot via Maven

I'm trying to set an active profile in Spring Boot application using Maven 3.
In my pom.xml I set default active profile and property spring.profiles.active to development:

<profiles>     <profile>         <id>development</id>         <properties>             <spring.profiles.active>development</spring.profiles.active>         </properties>         <activation>             <activeByDefault>true</activeByDefault>         </activation>     </profile> </profiles> 

but every time I run my application, I receive the following message in logs:

No active profile set, falling back to default profiles: default 

and the SpringBoot profile is set to default (reads application.properties instead application-development.properties)

What else should I do to have my SpringBoot active profile set using Maven profile?
Any help highly appreciated.

like image 773
Michał Szewczyk Avatar asked Feb 22 '17 12:02

Michał Szewczyk


People also ask

How do I run a specific profile in Maven?

Open Maven settings. m2 directory where %USER_HOME% represents the user home directory. If settings. xml file is not there, then create a new one. Add test profile as an active profile using active Profiles node as shown below in example.


2 Answers

The Maven profile and the Spring profile are two completely different things. Your pom.xml defines spring.profiles.active variable which is available in the build process, but not at runtime. That is why only the default profile is activated.

How to bind Maven profile with Spring?

You need to pass the build variable to your application so that it is available when it is started.

  1. Define a placeholder in your application.properties:

    [email protected]@ 

    The @spring.profiles.active@ variable must match the declared property from the Maven profile.

  2. Enable resource filtering in you pom.xml:

    <build>     <resources>         <resource>             <directory>src/main/resources</directory>             <filtering>true</filtering>         </resource>     </resources>     … </build> 

    When the build is executed, all files in the src/main/resources directory will be processed by Maven and the placeholder in your application.properties will be replaced with the variable you defined in your Maven profile.

For more details you can go to my post where I described this use case.

like image 119
Daniel Olszewski Avatar answered Oct 21 '22 02:10

Daniel Olszewski


Or rather easily:

mvn spring-boot:run -Dspring-boot.run.profiles={profile_name} 
like image 42
Thomas Escolan Avatar answered Oct 21 '22 01:10

Thomas Escolan