Debugger
Debugger

Reputation: 792

Maven - While Compile the project I need to set a active the profiles

I need to set a active profiles to the jars while compile the maven spring boot project

Following are my two Approaches tried to activate the profiles.

First Approach - Not Activating the Profiles

 mvn clean package -Dspring-boot.run.profiles=dev help:active-profiles
 -s settings.xml

*

Above command not setting the active profiles while executing jars

 java -jar package.jar

I have using maven plugin dependencies for activating the first approach

<build>
    <finalName>${project.artifactId}</finalName>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>${spring.boot.version}</version>
            <executions>
        <execution>
            <goals>
                <goal>repackage</goal>
            </goals>
        </execution>
   </executions>  </plugin> </plugins></build>

Second Approach - Working as expected.

 mvn clean package -Dspring.profiles.active=dev help:active-profiles -s
 settings.xml

 java -jar -Dspring.profiles.active=dev package.jar

I'm using multi-module parent pom structure for the project. Any one pls advise to work the first approach?

Upvotes: 1

Views: 1236

Answers (1)

Mark Bramnik
Mark Bramnik

Reputation: 42481

Maven is a build system. Its responsible for building your artifact. Maven's profiles are basically a tool that allows to slightly change the build process depending on various factors (operating systems, version of java and so forth). These are defined in maven's files (pom.xml)

Spring (and Spring boot of course) as opposed to maven is a runtime framework. Spring profiles is something totally different - they allow to load different beans, resolve different configurations in runtime (read after you call java -jar app.jar) depending on profile definitions.

So don't be confused with the same name, its only an "accidental coincidence"

Now as for your question.

Maven builds the artifact and packages it for that use spring boot maven plugin. If you want to customize this building process - use maven profiles (and as a consequence help:effective-profiles can be sometimes handy).

When the artifact is built - you can run it. For that you can use spring profiles to define runtime in-variants: - Addresses of databases - Credentials - Some subsystems of your product that won't be run locally and so forth, this list really depends on the application itself.

There is an option to run the spring boot application right from maven and for that you can really use -Dspring.profiles.active=dev but if you don't run it (and you don't in any of suggested approaches, read about mvn spring-boot:run to understand what does it really mean to run a spring boot application during the build), then:

First approach:

 mvn clean package -Dspring-boot.run.profiles=dev help:active-profiles
 -s settings.xml

-Dspring-boot.run.profiles is irrelevant here - you do nothing with it (again you don't run the project during the build) so it does nothing

java -jar package.jar

Here you can really specify spring profiles with --spring.profiles.active=dev,whatever

The Second approach:

mvn clean package -Dspring.profiles.active=dev help:active-profiles -s
 settings.xml

Again, -Dspring.profiles.active=dev is irrelevant, it does nothing.

java -jar -Dspring.profiles.active=dev package.jar

Here you do specify the list of active profiles (just like --spring.profiles.active, from spring boot's standpoint its the same) That's why the application works in runtime as expected

Upvotes: 3

Related Questions