我有一个Spring引导应用程序,由Gradle管理。
到目前为止所做的事情:
application.yaml中创建了application.yaml、application-dev.yaml和application-qa.yaml spring: profiles: active: dev (如果没有提到,默认配置文件应该是dev )G 213
需要做什么:
war。例如,对于QA构建,spring: profiles: active:应该有值qa,这样它就可以获取application-qa.yaml qa- no配置文件,然后默认情况下应该选择。
到目前为止尝试的内容:
application.yaml中的spring: profiles: active: @activeProfiles@中添加了@activeProfiles@,在build.gradle中添加了下面的代码片段:processResources { filter org.apache.tools.ant.filters.ReplaceTokens,令牌: activeProfiles: activeProfiles }
并启动以下命令来构建war - gradlew clean build -PactiveProfiles=qa,并且它为war文件做了正确的工作。但是现在这种方法的问题是,如何在从IDE运行项目时为activeProfiles提供默认值(我通常更喜欢从IntelliJ运行main类)?
我也不知道这是否正确的做法,或是否有任何其他办法来完成这项任务。我已经用Maven轻松地完成了所有这些工作,但是我对Gradle还是新手,使用Gradle是项目的需求。
发布于 2021-02-24 18:17:11
如果在运行/构建代码时没有传递配置文件,我可以在build.gradle文件中使用下面的配置设置默认配置文件。在任何IDE中运行项目都不需要额外的配置。
def activeProfiles=project.properties['activeProfiles'] ?: "dev" // set default (dev) profile if no profile is passed as property
processResources {
filter org.apache.tools.ant.filters.ReplaceTokens, tokens: [
activeProfiles: activeProfiles
]
}我有3个yaml文件,用于按环境配置:
application.yamlapplication-dev.yamlapplication-qa.yamlapplication.yaml包含以下用于弹簧配置文件管理的配置和其他常见配置:
spring:
profiles:
active: @activeProfiles@
...application-dev.yaml和application-qa.yaml文件包含与各自环境相关的配置。
发布于 2021-02-04 23:46:04
在不同环境中切换配置文件的一种常见方法是将SPRING_PROFILES_ACTIVE环境变量设置为您想要的值,并让Spring使用它。例如,在QA环境中,将值设置为qa,Spring将自动使用qa应用程序属性。阅读更多:https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html#howto-set-active-spring-profiles
https://stackoverflow.com/questions/66050482
复制相似问题