Configure active profile in SpringBoot via Maven

前端 未结 7 1557
执笔经年
执笔经年 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 21:13

    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.

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

    2. Enable resource filtering in you pom.xml:

      <build>
          <resources>
              <resource>
                  <directory>src/main/resources</directory>
                  <filtering>true</filtering>
              </resource>
          </resources>
          …
      </build>
      

      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.

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