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.

luboskrnac
  • 23,973
  • 10
  • 81
  • 92
DanielC
  • 357
  • 1
  • 4
  • 10

2 Answers2

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:

spring.profiles.active=@active.profile@

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.

Alza3eem
  • 198
  • 2
  • 6
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:

Aritz
  • 30,971
  • 16
  • 136
  • 217
  • 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. – Jimmy Jul 11 '18 at 05:57
  • For spring profiles see this thread: https://github.com/spring-projects/spring-boot/issues/1095 – Aritz Jul 11 '18 at 06:11
  • 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? – Jimmy Jul 11 '18 at 06:16
  • What do you mean with `not working`? You should post a new question with a detailed description of your issue. – Aritz Jul 11 '18 at 07:06