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

2

7

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.

Aqaba answered 20/3, 2016 at 18:45 Comment(0)
T
7

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.

Taraxacum answered 30/3, 2019 at 23:47 Comment(1)
Thank you so much! It helped me a lot!Vaginismus
R
3

The spring profiles are targeted to application runtime. They don't operate at the time of packaging the application as the Maven ones do. So you have to use them when launching your application, not when packaging it.

However, if you want to generate different packages with some default profile each one, you could play with Maven resource filtering. After all, the way to build a Spring Boot runnable jar with Maven is to follow the standard procedure, so you get the Spring Boot Maven plugin involved:

mvn clean install -PproductionMvnProfile

See also:

Revivalism answered 20/3, 2016 at 19:2 Comment(4)
I just got one question : Why -Pprofilename not working with command "mvn spring-boot:run". I tried this "mvn spring-boot:run -Pdev" And it is running default activated profile only not the dev.Halfwitted
For spring profiles see this thread: github.com/spring-projects/spring-boot/issues/1095Revivalism
Hi, thanks for getting back quickly. I am already running spring profiles with this "mvn spring-boot:run -Dspring.profiles.active="production" command and it is working. Now, I am handling profiling from pom.xml. So I read the documentation and they said use -Pprofilename command after mvn clean package. That is working well. But -Pprofilename is not working with spring-boot:run. Is it expected behaviour?Halfwitted
What do you mean with not working? You should post a new question with a detailed description of your issue.Revivalism

© 2022 - 2024 — McMap. All rights reserved.