how to use Spring Boot profiles

前端 未结 12 1099
忘了有多久
忘了有多久 2020-12-04 21:14

i have application.yml,application-dev.ymlandapplication-dev.yml

  1. I\'m using the maven command mvn spring-boot:run
相关标签:
12条回答
  • 2020-12-04 22:03

    I'm not sure I fully understand the question but I'll attempt to answer by providing a few details about profiles in Spring Boot.

    For your #1 example, according to the docs you can select the profile using the Spring Boot Maven plugin using -Drun.profiles.

    Edit: For Spring Boot 2.0+ run has been renamed to spring-boot.run and run.profiles has been renamed to spring-boot.run.profiles

    mvn spring-boot:run -Dspring-boot.run.profiles=dev
    

    http://docs.spring.io/spring-boot/docs/current/maven-plugin/examples/run-profiles.html

    From your #2 example, you are defining the active profile after the name of the jar. You need to provide the JVM argument before the name of the jar you are running.

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

    General info:

    You mention that you have both an application.yml and a application-dev.yml. Running with the dev profile will actually load both config files. Values from application-dev.yml will override the same values provided by application.yml but values from both yml files will be loaded.

    There are also multiple ways to define the active profile.

    You can define them as you did, using -Dspring.profiles.active when running your jar. You can also set the profile using a SPRING_PROFILES_ACTIVE environment variable or a spring.profiles.active system property.

    More info can be found here: http://docs.spring.io/spring-boot/docs/current/reference/html/howto-properties-and-configuration.html#howto-set-active-spring-profiles

    0 讨论(0)
  • 2020-12-04 22:05

    You don't need three .yml files for this. You can have a single application.yml file and write profile specific properties in the same where each profile section is separated by 3 hyphen (---)

    Next, for selecting the current active profile, you can specify that as well in your application.yml file, like this :

    spring:
      profiles:
        active:
        - local
    

    However, this configuration will be overriden if you set an Environment variable, eg : SPRING_PROFILES_ACTIVE = dev


    Here is a sample file for you requirement:

    # include common properties for every profile in this section
    
    server.port: 5000 
    
    spring:
      profiles:
        active:
        - local
    
    ---
    # profile specific properties
    
    spring:
      profiles: local
    
      datasource:
        url: jdbc:mysql://localhost:3306/
        username: root
        password: root
    
    ---
    # profile specific properties
    
    spring:
      profiles: dev
    
      datasource:
        url: jdbc:mysql://<dev db url>
        username: <username>
        password: <password>
    
    0 讨论(0)
  • 2020-12-04 22:05

    Create specific .yml files in the resources directory for each and every environment(Eg: dev,qa,stg etc.) that you need to run the application. image of .yml files in resources directory

    If you are using spring-boot-maven-plugin 2.0.5.RELEASE in your pom.xml file you can add the profiles within the dependency tag as follows. image of pom.xml spring-boot-maven-plugin (you can configure multiple profiles using multiple profile tags)

    Then you can use the following commands to build and run the project.

    1) mvn clean install
    2) mvn spring-boot:run -Dspring-boot.run.default==qa
    

    Then you will see that the default profile is set as qa while running the project. displaying the default profile when running the application

    0 讨论(0)
  • 2020-12-04 22:05

    Use "-Dspring-boot.run.profiles=foo,local" in Intellij IDEA. It's working. Its sets 2 profiles "foo and local".

    Verified with boot version "2.3.2.RELEASE" & Intellij IDEA CE 2019.3.

    <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    

    Setting profile with "mvn spring-boot:run"

    Setting environment variable

    0 讨论(0)
  • 2020-12-04 22:11

    If your using maven,

    <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                        <profiles>
                            <profile>dev</profile>
                        </profiles>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    

    this set dev as active profile

    ./mvnw spring-boot:run
    

    will have dev as active profile.

    0 讨论(0)
  • 2020-12-04 22:12

    mvn spring-boot:run -Dspring-boot.run.profiles=foo,bar

    **Source- **https://docs.spring.io/spring-boot/docs/current/maven-plugin/examples/run-profiles.html

    Basically it is need when you multiple application-{environment}.properties is present inside in your project. by default, if you passed -Drun.profiles on command line or activeByDefault true in

     <profile>
        <id>dev</id>
        <properties>
            <activatedProperties>dev</activatedProperties>
        </properties>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
    

    Nothing defined like above it will choose by default application.properties otherwise you need to select by appending -Drun.profiles={dev/stage/prod}.

    TL;DR

    mvn spring-boot:run -Drun.profiles=dev

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