ChatGPT解决这个技术问题 Extra ChatGPT

Configure active profile in SpringBoot via Maven

I'm trying to set an active profile in Spring Boot application using Maven 3. In my pom.xml I set default active profile and property spring.profiles.active to development:

<profiles>
    <profile>
        <id>development</id>
        <properties>
            <spring.profiles.active>development</spring.profiles.active>
        </properties>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
</profiles>

but every time I run my application, I receive the following message in logs:

No active profile set, falling back to default profiles: default

and the SpringBoot profile is set to default (reads application.properties instead application-development.properties) What else should I do to have my SpringBoot active profile set using Maven profile? Any help highly appreciated.


D
Daniel Olszewski

The Maven profile and the Spring profile are two completely different things. Your pom.xml defines spring.profiles.active variable which is available in the build process, but not at runtime. That is why only the default profile is activated.

How to bind Maven profile with Spring?

You need to pass the build variable to your application so that it is available when it is started.

Define a placeholder in your application.properties: spring.profiles.active=@spring.profiles.active@ The @spring.profiles.active@ variable must match the declared property from the Maven profile. Enable resource filtering in you pom.xml: src/main/resources true When the build is executed, all files in the src/main/resources directory will be processed by Maven and the placeholder in your application.properties will be replaced with the variable you defined in your Maven profile.

For more details you can go to my post where I described this use case.


Is there a way to enable profile for unit tests via maven command? Maybe you have an idea for this question.
I just want to add that you can set the active profile with the commamd mvn package -P myProfile where my profile is defined in the POM
B
Brent Bradburn

Or rather easily:

mvn spring-boot:run -Dspring-boot.run.profiles={profile_name}

Although this does not answer the question "how to set springboot active profile via maven", but it gives me answer to "how to set springboot:run active profile".
with Spring2.x, spring.profiles.active setting didnt work, either directly or via jvmArguments. Thanks for the answer
P
Pankaj Jaiswal

There are multiple ways to set profiles for your springboot application.

You can add this in your property file: spring.profiles.active=dev Programmatic way: SpringApplication.setAdditionalProfiles("dev"); Tests make it very easy to specify what profiles are active @ActiveProfiles("dev") In a Unix environment export spring_profiles_active=dev JVM System Parameter -Dspring.profiles.active=dev

Example: Running a springboot jar file with profile.

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

>You can add this in your property file: In which properties file? as profile is needed to pickup respective application.xxx.properties
T
Thomas Fritsch

You can run using the following command. Here I want to run using spring profile local:

spring-boot:run -Drun.jvmArguments="-Dspring.profiles.active=local"


Best answer! Worked for me using maven 3.5.2 + java 1.8 + spring-boot-1.5.9.RELEASE on mac os 10.13.3
Where do i add it?
d
davidxxx

In development, activating a Spring Boot profile when a specific Maven profile is activate is straight. You should use the profiles property of the spring-boot-maven-plugin in the Maven profile such as :

<project>
    <...>
    <profiles>
        <profile>
            <id>development</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-maven-plugin</artifactId>
                        <configuration>
                            <profiles>
                                <profile>development</profile>
                            </profiles>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
    <profiles>
    </...>
</project>

You can run the following command to use both the Spring Boot and the Maven development profile :

mvn spring-boot:run -Pdevelopment

If you want to be able to map any Spring Boot profiles to a Maven profile with the same profile name, you could define a single Maven profile and enabling that as the presence of a Maven property is detected. This property would be the single thing that you need to specify as you run the mvn command.
The profile would look like :

    <profile>
        <id>spring-profile-active</id>
        <activation>
            <property>
                <name>my.active.spring.profiles</name>
            </property>
        </activation>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                        <profiles>
                            <profile>${my.active.spring.profiles}</profile>
                        </profiles>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>

And you can run the following command to use both the Spring Boot and the Maven development profile :

mvn spring-boot:run -Dmy.active.spring.profiles=development

or :

mvn spring-boot:run -Dmy.active.spring.profiles=integration

or :

 mvn spring-boot:run -Dmy.active.spring.profiles=production

And so for...

This kind of configuration makes sense as in the generic Maven profile you rely on the my.active.spring.profiles property that is passed to perform some tasks or value some things.
For example I use this way to configure a generic Maven profile that packages the application and build a docker image specific to the environment selected.


S
SaWo

You should use the Spring Boot Maven Plugin:

<project>  
  ...
  <build>
    ...
    <plugins>
      ...
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <version>1.5.1.RELEASE</version>
        <configuration>
          <profiles>
            <profile>foo</profile>
            <profile>bar</profile>
          </profiles>
        </configuration>
        ...
      </plugin>
      ...
    </plugins>
    ...
  </build>
  ...
</project>

easier, faster, cleaner, no head bumping whatsoever. +1 for me
Ö
Özlem Pamuk

I would like to run an automation test in different environments. So I add this to command maven command:

spring-boot:run -Drun.jvmArguments="-Dspring.profiles.active=productionEnv1"

Here is the link where I found the solution: [1]https://github.com/spring-projects/spring-boot/issues/1095


mvn spring-boot:run -Drun:jvmArguments="-Dspring.profiles.active=prod" --> When I run this, I still get No active profile set, falling back to default profiles: default
L
Lukasmp3

I wanted to clarify the excellent answer https://stackoverflow.com/a/42391322/13134499 from Daniel Olszewski if you want to use it just for tests.

How to bind Maven profile with Spring for tests?

As you did, define the variable in pom.xml

...
<properties>
  <spring.profiles.active>development</spring.profiles.active>
</properties>

Define a placeholder in your application-test.properties in src/test/resources:

spring.profiles.active=@spring.profiles.active@

Enable resource filtering in you pom.xml:

<build>
  ...
  <testResources>
    <testResource>
      <directory>src/test/resources</directory>
      <filtering>true</filtering>
    </testResource>
  <testResources>
</build>

L
LIU YUE

file structure:

/src/main/resources

=>

application.properties:

spring.profiles.active:@spring.profiles.active@'

application-dev.properties

application-prod.properties

IDE-Eclipse:

Right click on the project=>Run As=>Run Configuration=>Arguments=>VM Arguments:-Dspring.profiles.active=dev

CMD:

mvn spring-boot:run -Dspring.profiles.active=dev
mvn clean install -Dspring.profiles.active=dev