14

I am running the spring boot app by passing the spring active profile like below:

spring-boot:run -Dspring.profiles.active=dev

But how do I pass the spring.profiles.active when creating the package using maven. Following is the maven command:

mvn clean install
kryger
  • 12,906
  • 8
  • 44
  • 65
Ronald Randon
  • 1,141
  • 3
  • 13
  • 19

4 Answers4

10

In case someone have the same situation, you can achieve this with 2 steps with spring boot and maven: First in spring properties or yaml file, add the spring.profiles.active with it's value as placeholder:

spring.profiles.active=@active.profile@

Second, pass the value with maven:

mvn clean install -Dactive.profile=dev

When the jar/war packaged, the value will be set to dev.

you can also leverage the use of maven profiles:

<profiles>
        <profile>
            <id>dev</id>
            <properties>
                <active.profile>dev</active.profile>
            </properties>
        </profile>
        <profile>
            <id>test</id>
            <properties>
                <active.profile>test</active.profile>
            </properties>
        </profile>
    </profiles>

Then run:

mvn clean install -Pdev
Alza3eem
  • 198
  • 2
  • 6
6

Maven is a build-time tool, the only way to make it modify the eventual runtime behaviour of the built artifact is to use its (build-time) profiles. This must not be confused with Spring's runtime profiles which are parameters instructing the Spring container to bootstrap application in a specific way.

In other words, the spring.profiles.active parameter doesn't get "baked into" the war file by maven, you'll still need to pass it when starting the application up, be it via command-line parameters or configuration file or whatever mechanism your servlet container offers.

kryger
  • 12,906
  • 8
  • 44
  • 65
1

For package, you may replace install with package

mvn clean install -P dev

JMD
  • 69
  • 2
  • 11
0

You can use environment variables.

export SPRING_PROFILES_ACTIVE=some,test,profiles
mvn spring-boot:run
Rob Moffat
  • 465
  • 5
  • 11