Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

question from:https://stackoverflow.com/questions/66050482/manage-spring-boot-profiles-with-gradle

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
229 views
Welcome To Ask or Share your Answers For Others

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...