Pass Spring profile in maven build

Viewed 13768

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
4 Answers

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

For package, you may replace install with package

mvn clean install -P dev

You can use environment variables.

export SPRING_PROFILES_ACTIVE=some,test,profiles
mvn spring-boot:run
Related