问题
I have a Spring boot application which is managed by Gradle.
What is done so far:
- Created
application.yaml
,application-dev.yaml
, andapplication-qa.yaml
files - Mentioned
spring: profiles: active: dev
inapplication.yaml
(default profile should bedev
if none is mentioned)
What need to be done:
- Need to build
war
with profile mentioned or passed while building. Example, for QA build,spring: profiles: active:
should have valueqa
, so that it can pick-upapplication-qa.yaml
properties. - If no profile is passed, then by default
dev
profile should be selected
What is tried so far:
Added
@activeProfiles@
inspring: profiles: active: @activeProfiles@
inapplication.yaml
and added below snippet inbuild.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 forwar
file. But now issue with this approach is, how to provide default value foractiveProfiles
while running the project from IDE (I usually prefer to runmain
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