Manage Spring Boot Profiles with Gradle

江枫思渺然 提交于 2021-02-11 12:05:36

问题


I have a Spring boot application which is managed by Gradle.

What is done so far:

  1. Created application.yaml, application-dev.yaml, and application-qa.yaml files
  2. Mentioned spring: profiles: active: dev in application.yaml (default profile should be dev if none is mentioned)

What need to be done:

  1. Need to build war with profile mentioned or passed while building. Example, for QA build, spring: profiles: active: should have value qa, so that it can pick-up application-qa.yaml properties.
  2. If no profile is passed, then by default dev profile should be selected

What is tried so far:

  1. Added @activeProfiles@ in spring: profiles: active: @activeProfiles@ in application.yaml and added below snippet in build.gradle:

    processResources {
        filter org.apache.tools.ant.filters.ReplaceTokens, tokens: [
            activeProfiles: activeProfiles
        ]
    }
    

    And fired following command to build the war - gradlew clean build -PactiveProfiles=qa, and it just did right job for war file. But now issue with this approach is, how to provide default value for activeProfiles while running the project from IDE (I usually prefer to run main class from IntelliJ)?

I am also not sure if this is the correct approach or is there any other approach to achieve this task. I have already done all these stuff with Maven, and with ease, but I am new to Gradle, and it's project requirement to use Gradle.


回答1:


One common approach to switch between profiles in different environments is to set up SPRING_PROFILES_ACTIVE environment variable to your desired value and let Spring boot use that. For example, in QA environment you would set the value to qa and Spring will automatically use qa application properties. Read more : https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html#howto-set-active-spring-profiles



来源:https://stackoverflow.com/questions/66050482/manage-spring-boot-profiles-with-gradle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!