在春季启动时从命令行设置活动配置文件和配置位置 [英] Setting active profile and config location from command line in spring boot

查看:223
本文介绍了在春季启动时从命令行设置活动配置文件和配置位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个spring引导应用程序。

I have a spring boot application.

我的应用程序中有三个配置文件 - 开发,分段和制作。所以我有3个文件

I have three profiles in my application-> development, staging and production. So I have 3 files


  1. application-development.yml

  2. application-staging.yml
  3. li>
  4. application-production.yml

  1. application-development.yml
  2. application-staging.yml
  3. application-production.yml

我的 application.yml 位于内部的src /主/资源。我已经在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.

我使用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

然而,命令行属性没有得到反映,我的活动配置文件总是被设置为开发(这是我在application.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.

我想我在这里失去了一些东西。我一直在试图弄清楚过去两天的情况。但没有运气。我非常感谢任何帮助。

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.

这篇关于在春季启动时从命令行设置活动配置文件和配置位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆