Configure active profile in SpringBoot via Maven

前端 未结 7 1556
执笔经年
执笔经年 2020-11-29 20:13

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.pr

相关标签:
7条回答
  • 2020-11-29 20:54

    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"

    0 讨论(0)
  • 2020-11-29 20:58

    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.

    0 讨论(0)
  • 2020-11-29 20:58

    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>
    
    0 讨论(0)
  • 2020-11-29 21:03

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

    1. You can add this in your property file:

      spring.profiles.active=dev
      
    2. Programmatic way:

      SpringApplication.setAdditionalProfiles("dev");
      
    3. Tests make it very easy to specify what profiles are active

      @ActiveProfiles("dev")
      
    4. In a Unix environment

      export spring_profiles_active=dev
      
    5. JVM System Parameter

      -Dspring.profiles.active=dev
      

    Example: Running a springboot jar file with profile.

    java -jar -Dspring.profiles.active=dev application.jar
    
    0 讨论(0)
  • 2020-11-29 21:07

    Or rather easily:

    mvn spring-boot:run -Dspring-boot.run.profiles={profile_name}
    
    0 讨论(0)
  • 2020-11-29 21:08

    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

    0 讨论(0)
提交回复
热议问题