how to use Spring Boot profiles

前端 未结 12 1098
忘了有多久
忘了有多久 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 21:54

    Since Spring Boot v2+

    I have verified with Spring Boot v2.3.5.RELEASE

    With Spring Boot Maven Plugin

    You can provide commandline argument like this:

    mvn spring-boot:run -Dspring-boot.run.arguments="--spring.profiles.active=dev"

    You can provide JVM argument like this:

    mvn spring-boot:run -Dspring-boot.run.jvmArguments="-Dspring.profiles.active=dev"

    java -jar

    java -Dspring.profiles.active=dev -jar app.jar (Note order)

    or

    java -jar app.jar --spring.profiles.active=dev (Note order)

    0 讨论(0)
  • 2020-12-04 21:54

    The @Profile annotation allows you to indicate that a component is eligible for registration when one or more specified profiles are active. Using our example above, we can rewrite the dataSource configuration as follows:

    @Configuration
    @Profile("dev")
    public class StandaloneDataConfig {
    
        @Bean
        public DataSource dataSource() {
            return new EmbeddedDatabaseBuilder()
                .setType(EmbeddedDatabaseType.HSQL)
                .addScript("classpath:com/bank/config/sql/schema.sql")
                .addScript("classpath:com/bank/config/sql/test-data.sql")
                .build();
        }
    }
    

    And other one:

    @Configuration
    @Profile("production")
    public class JndiDataConfig {
    
        @Bean(destroyMethod="")
        public DataSource dataSource() throws Exception {
            Context ctx = new InitialContext();
            return (DataSource) ctx.lookup("java:comp/env/jdbc/datasource");
        }
    }
    
    0 讨论(0)
  • 2020-12-04 21:57

    If you are using the Spring Boot Maven Plugin, run:

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

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

    0 讨论(0)
  • 2020-12-04 21:58

    If you are using maven, define your profiles as shown below within your pom.xml

    <profiles>
    <profile>
        <id>local</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <jdbc.url>dbUrl</jdbc.url>
            <jdbc.username>dbuser</jdbc.username>
            <jdbc.password>dbPassword</jdbc.password>
            <jdbc.driver>dbDriver</jdbc.driver>
        </properties>
    </profile>
    <profile>
        <id>dev</id>
        <properties>
            <jdbc.url>dbUrl</jdbc.url>
            <jdbc.username>dbuser</jdbc.username>
            <jdbc.password>dbPassword</jdbc.password>
            <jdbc.driver>dbDriver</jdbc.driver>
        </properties>
        <dependencies>
            <dependency>
                <groupId>org.postgresql</groupId>
                <artifactId>postgresql</artifactId>
            </dependency>
        </dependencies>
    </profile>
    <profile>
        <id>prod</id>
        <properties>
            <jdbc.url>dbUrl</jdbc.url>
            <jdbc.username>dbuser</jdbc.username>
            <jdbc.password>dbPassword</jdbc.password>
            <jdbc.driver>dbDriver</jdbc.driver>
        </properties>
    </profile>
    

    By default, i.e if No profile is selected, the local profile will always be use.

    To select a specific profile in Spring Boot 2.x.x, use the below command.

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

    If you want want to build/compile using properties of a specific profile, use the below command.

    mvn clean install -Pdev -DprofileIdEnabled=true
    
    0 讨论(0)
  • 2020-12-04 21:59

    You can specify properties according profiles in one application.properties(yml) like here. Then mvn clean spring-boot:run -Dspring.profiles.active=dev should run it correct. It works for me

    0 讨论(0)
  • 2020-12-04 21:59

    Working with Intellij, because I don't know how to set keyboard shortcut to mvn spring-boot:run -Dspring.profiles.active=dev, I have to do this:

    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
            <jvmArguments>
                -Dspring.profiles.active=dev
            </jvmArguments>
        </configuration>
    </plugin>
    
    0 讨论(0)
提交回复
热议问题