Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass Spring profile in maven build

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
like image 908
Ronald Randon Avatar asked Mar 24 '16 10:03

Ronald Randon


People also ask

What is Maven build profile?

A Build profile is a set of configuration values, which can be used to set or override default values of Maven build. Using a build profile, you can customize build for different environments such as Production v/s Development environments. Profiles are specified in pom.

How do I profile in spring boot?

Spring Boot profilesSpring Boot allows to define profile specific property files in the form of application-{profile}. properties . It automatically loads the properties in an application. properties file for all profiles, and the ones in profile-specific property files only for the specified profile.

How do I add multiple accounts to spring boot?

The solution would be to create more property files and add the "profile" name as the suffix and configure Spring Boot to pick the appropriate properties based on the profile. Then, we need to create three application. properties : application-dev.


2 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:

[email protected]@

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
like image 119
Alza3eem Avatar answered Sep 17 '22 09:09

Alza3eem


For package, you may replace install with package

mvn clean install -P dev

like image 43
JMD Avatar answered Sep 17 '22 09:09

JMD