Maven and spring-boot: how to specify profile(s) on spring-boot:repackage

DanielC picture DanielC · Mar 20, 2016 · Viewed 13.4k times · Source

With spring-boot, I know that I can have profiles and use different configuration files depending on the active profiles. For instance the command:

"mvn spring-boot:run -Drun.profiles=default,production"

will run my spring-boot application using the settings defined in both the "application-default.properties" and "application-production.properties" with the settings on the second file overriding the same settings defined in the first file (for instance db connection settings). All this is currently running well.

However, I want to build my spring-boot application and generate a runnable jar using the following command:

"mvn package spring-boot:repackage".

This command does generate self-contained runnable jar perfectly well. The question is, ¿how do I specifiy active profiles with the former command? I have used

"mvn package spring-boot:repackage -Drun.profiles=default,production"

but it is not working.

Answer

Alza3eem picture Alza3eem · Mar 31, 2019

I answered the same question in this thread: Pass Spring profile in maven build , but I will repeat the answer here again.

In case someone have the same situation, to run a spring boot runnable jar or war using specific profile you need to have the property spring.profiles.active present in your default application.properties file, to change its value dynamically while generating the artifact, you can do this:

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 package spring-boot:repackage -Dactive.profile=dev

or if the spring-boot plugin is already presented in your pom like the following:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

You can run instead the following command:

mvn clean package -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>prod</active.profile>
            </properties>
        </profile>
    </profiles>

Then run:

mvn clean install -Pdev

You don't need to pass the 2 properties files (the default and dev/prod), by default the variables in application.properties will be executed first.