在 spring boot 中从命令行设置活动配置文件和配置位置 [英] Setting active profile and config location from command line in spring boot
问题描述
我有一个 Spring Boot 应用程序.
I have a spring boot application.
我的应用程序中有三个配置文件-> 开发、暂存和生产.所以我有3个文件
I have three profiles in my application-> development, staging and production. So I have 3 files
- application-development.yml
- application-staging.yml
- application-production.yml
我的 application.yml 位于 src/main/resources
中.我已将 application.yml 中的活动配置文件设置为:
My application.yml resides inside src/main/resources
. I have set the active profile in application.yml as :
spring:
profiles.active: development
其他 3 个特定于配置文件的配置文件位于 C:config
文件夹中.
The other 3 profile specific config files are present in C:config
folder.
我正在为 Eclipse 使用 gradle 插件.当我尝试执行bootRun"时,我将 Eclipse 中的 gradle 配置中的命令行参数设置为
I am using gradle plugin for eclipse. When I try to do a "bootRun", I am setting the command line arguments in my gradle configuration in eclipse as
-Dspring.profiles.active=staging -Dspring.config.location=C:Config
然而,命令行属性没有得到反映,我的活动配置文件总是被设置为开发(这是我在 Applications.yml 文件中提到的那个).此外,不会在 C:Config 文件夹中搜索特定于配置文件的配置文件.
However, the command line property is not getting reflected and my active profile is always getting set as development(which is the one that I have mentioned in the applications.yml file). Also C:Config folder is not searched for profile specific config files.
我想我在这里遗漏了一些东西.在过去的 2 天里,我一直在试图弄清楚.但没有运气.我真的很感激任何帮助.
I think I am missing something here. I have been trying to figure it out for the past 2 days. But no luck. I would really appreciate any help.
推荐答案
我必须添加:
bootRun {
String activeProfile = System.properties['spring.profiles.active']
String confLoc = System.properties['spring.config.location']
systemProperty "spring.profiles.active", activeProfile
systemProperty "spring.config.location", "file:$confLoc"
}
现在 bootRun 获取配置文件和配置位置.
And now bootRun picks up the profile and config locations.
非常感谢@jst 的指点.
Thanks a lot @jst for the pointer.
这篇关于在 spring boot 中从命令行设置活动配置文件和配置位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!