Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 598
DanielC Avatar asked Mar 20 '16 18:03

DanielC


Video Answer


2 Answers

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.

like image 121
Alza3eem Avatar answered Oct 14 '22 07:10

Alza3eem


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:

  • maven profiles or spring profiles?
like image 37
Xtreme Biker Avatar answered Oct 14 '22 05:10

Xtreme Biker