83

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.

Michał Szewczyk
  • 7,540
  • 8
  • 35
  • 47

10 Answers10

124

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:

    spring.profiles.active=@spring.profiles.active@
    

    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.

Daniel Olszewski
  • 13,995
  • 6
  • 58
  • 65
  • Is there a way to enable profile for unit tests via maven command? Maybe you have an idea for [this question.](https://stackoverflow.com/q/47833555/3493036) – Patrick Dec 18 '17 at 07:41
  • 2
    I just want to add that you can set the active profile with the commamd mvn package -P myProfile where my profile is defined in the POM – ihebiheb Feb 15 '19 at 19:05
  • If you are running from jar use `java -Dspring.profiles.active= your-application.jar ` – Shakeel Hussain Jul 16 '23 at 16:47
55

Or rather easily:

mvn spring-boot:run -Dspring-boot.run.profiles={profile_name}
Brent Bradburn
  • 51,587
  • 17
  • 154
  • 173
Thomas Escolan
  • 985
  • 1
  • 8
  • 17
  • 2
    Although this does not answer the question "how to set springboot active profile via maven", but it gives me answer to "how to set springboot:run active profile". – pbeta Oct 13 '19 at 09:11
  • 1
    with Spring2.x, spring.profiles.active setting didnt work, either directly or via jvmArguments. Thanks for the answer – Winster Mar 04 '20 at 12:52
36

There are multiple ways to set profiles for your springboot application.

  1. You can add this in your property file:

    spring.profiles.active=dev
    
  2. Programmatic way:

    SpringApplication.setAdditionalProfiles("dev");
    
  3. Tests make it very easy to specify what profiles are active

    @ActiveProfiles("dev")
    
  4. In a Unix environment

    export spring_profiles_active=dev
    
  5. JVM System Parameter

    -Dspring.profiles.active=dev
    

Example: Running a springboot jar file with profile.

java -jar -Dspring.profiles.active=dev application.jar
Pankaj Jaiswal
  • 689
  • 8
  • 16
18

You can run using the following command. Here I want to run using spring profile local:

spring-boot:run -Drun.jvmArguments="-Dspring.profiles.active=local"

Thomas Fritsch
  • 9,639
  • 33
  • 37
  • 49
Justin Joseph
  • 189
  • 1
  • 2
15

In development, activating a Spring Boot profile when a specific Maven profile is activate is straight. You should use the profiles property of the spring-boot-maven-plugin in the Maven profile such as :

<project>
    <...>
    <profiles>
        <profile>
            <id>development</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-maven-plugin</artifactId>
                        <configuration>
                            <profiles>
                                <profile>development</profile>
                            </profiles>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
    <profiles>
    </...>
</project>

You can run the following command to use both the Spring Boot and the Maven development profile :

mvn spring-boot:run -Pdevelopment

If you want to be able to map any Spring Boot profiles to a Maven profile with the same profile name, you could define a single Maven profile and enabling that as the presence of a Maven property is detected. This property would be the single thing that you need to specify as you run the mvn command.
The profile would look like :

    <profile>
        <id>spring-profile-active</id>
        <activation>
            <property>
                <name>my.active.spring.profiles</name>
            </property>
        </activation>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                        <profiles>
                            <profile>${my.active.spring.profiles}</profile>
                        </profiles>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>

And you can run the following command to use both the Spring Boot and the Maven development profile :

mvn spring-boot:run -Dmy.active.spring.profiles=development

or :

mvn spring-boot:run -Dmy.active.spring.profiles=integration

or :

 mvn spring-boot:run -Dmy.active.spring.profiles=production

And so for...

This kind of configuration makes sense as in the generic Maven profile you rely on the my.active.spring.profiles property that is passed to perform some tasks or value some things.
For example I use this way to configure a generic Maven profile that packages the application and build a docker image specific to the environment selected.

davidxxx
  • 125,838
  • 23
  • 214
  • 215
9

You should use the Spring Boot Maven Plugin:

<project>  
  ...
  <build>
    ...
    <plugins>
      ...
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <version>1.5.1.RELEASE</version>
        <configuration>
          <profiles>
            <profile>foo</profile>
            <profile>bar</profile>
          </profiles>
        </configuration>
        ...
      </plugin>
      ...
    </plugins>
    ...
  </build>
  ...
</project>
SaWo
  • 1,515
  • 2
  • 14
  • 32
1

I would like to run an automation test in different environments.
So I add this to command maven command:

spring-boot:run -Drun.jvmArguments="-Dspring.profiles.active=productionEnv1"

Here is the link where I found the solution: [1]https://github.com/spring-projects/spring-boot/issues/1095

  • `mvn spring-boot:run -Drun:jvmArguments="-Dspring.profiles.active=prod"` --> When I run this, I still get `No active profile set, falling back to default profiles: default` – Pavindu Dec 24 '21 at 12:49
1

I wanted to clarify the excellent answer https://stackoverflow.com/a/42391322/13134499 from Daniel Olszewski if you want to use it just for tests.

How to bind Maven profile with Spring for tests?

  1. As you did, define the variable in pom.xml
...
<properties>
  <spring.profiles.active>development</spring.profiles.active>
</properties>
  1. Define a placeholder in your application-test.properties in src/test/resources:
spring.profiles.active=@spring.profiles.active@
  1. Enable resource filtering in you pom.xml:
<build>
  ...
  <testResources>
    <testResource>
      <directory>src/test/resources</directory>
      <filtering>true</filtering>
    </testResource>
  <testResources>
</build>
Lukasmp3
  • 118
  • 5
1

file structure:

/src/main/resources

=>

application.properties:

spring.profiles.active:@spring.profiles.active@'

application-dev.properties

application-prod.properties

IDE-Eclipse:

Right click on the project=>Run As=>Run Configuration=>Arguments=>VM Arguments:-Dspring.profiles.active=dev

CMD:

mvn spring-boot:run -Dspring.profiles.active=dev
mvn clean install -Dspring.profiles.active=dev
LIU YUE
  • 1,593
  • 11
  • 19
-2

I added this in the first line

spring.profiles.active=@spring.profiles.active@
server.port=9898
jmoerdyk
  • 5,544
  • 7
  • 38
  • 49