Java 在 spring boot 中从命令行设置活动配置文件和配置位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31038250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 10:33:17  来源:igfitidea点击:

Setting active profile and config location from command line in spring boot

javaspringgradlespring-bootyaml

提问by O-OF-N

I have a spring boot application.

我有一个弹簧启动应用程序。

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

我的 application-> development, staging 和 production 中有三个配置文件。所以我有3个文件

  1. application-development.yml
  2. application-staging.yml
  3. application-production.yml
  1. 应用程序开发.yml
  2. application-staging.yml
  3. 应用程序生产.yml

My application.ymlresides inside src/main/resources. I have set the active profile in application.yml as :

我的application.yml位于src/main/resources. 我已将 application.yml 中的活动配置文件设置为:

spring:
  profiles.active: development

The other 3 profile specific config files are present in C:\configfolder.

其他 3 个特定于配置文件的配置文件存在于C:\config文件夹中。

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

我正在为 Eclipse 使用 gradle 插件。当我尝试执行“ bootRun”时,我将 eclipse 中的 gradle 配置中的命令行参数设置为

 -Dspring.profiles.active=staging -Dspring.config.location=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.

但是,命令行属性没有得到反映,我的活动配置文件总是被设置为开发(这是我在 applications.yml 文件中提到的那个)。此外,不会在 C:\Config 文件夹中搜索特定于配置文件的配置文件。

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.

我想我在这里遗漏了一些东西。在过去的 2 天里,我一直在试图弄清楚。但没有运气。我真的很感激任何帮助。

采纳答案by O-OF-N

I had to add this:

我不得不添加这个:

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"
}

And now bootRun picks up the profile and config locations.

现在 bootRun 获取配置文件和配置位置。

Thanks a lot @jst for the pointer.

非常感谢@jst 的指点。

回答by jst

I think your problem is likely related to your spring.config.location not ending the path with "/".

我认为您的问题可能与您的 spring.config.location 没有以“/”结尾的路径有关。

Quote the docs

引用文档

If spring.config.location contains directories (as opposed to files) they should end in / (and will be appended with the names generated from spring.config.name before being loaded).

如果 spring.config.location 包含目录(而不是文件),它们应该以 / 结尾(并且在加载之前会附加从 spring.config.name 生成的名称)。

http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config-application-property-files

http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config-application-property-files

回答by Michael Yin

-Dspring.profiles.active=staging -Dspring.config.location=C:\Config

is not correct.

是不正确的。

should be:

应该:

--spring.profiles.active=staging --spring.config.location=C:\Config

回答by Eric Taix

Michael Yin's answer is correct but a better explanation seems to be required!

Michael Yin的回答是正确的,但似乎需要更好的解释!

A lot of you mentioned that -Dis the correct way to specify JVM parameters and you are absolutely right. But Michael is also right as mentioned in Spring Boot Profilesdocumentation.

你们中的很多人都提到这-D是指定 JVM 参数的正确方法,您是绝对正确的。但正如Spring Boot Profiles文档中提到的,Michael 也是对的。

What is not clear in the documentation, is what kind of parameter it is: --spring.profiles.activeis a not a standard JVM parameter so if you want to use it in your IDE fill the correct fields (i.e. program arguments)

文档中不清楚的是它是什么类型的参数:--spring.profiles.active不是标准的 JVM 参数,因此如果您想在 IDE 中使用它,请填写正确的字段(即程序参数)

回答by RenRen

There are two different ways you can add/override spring properties on the command line.

您可以通过两种不同的方式在命令行上添加/覆盖弹簧属性。

Option 1: Java System Properties (VM Arguments)

选项 1:Java 系统属性(VM 参数)

It's important that the -D parameters are before your application.jar otherwise they are not recognized.

-D 参数位于 application.jar 之前很重要,否则它们将无法识别。

java -jar -Dspring.profiles.active=prod application.jar



Option 2: Program arguments

选项 2:程序参数

java -jar application.jar --spring.profiles.active=prod --spring.config.location=c:\config

回答by Rollen Holt

you can use the following command line:

您可以使用以下命令行:

java -jar -Dspring.profiles.active=[yourProfileName] target/[yourJar].jar

回答by JARC

When setting the profile via the Maven pluginyou must do it via run.jvmArguments

通过Maven 插件设置配置文件时,您必须通过run.jvmArguments

mvn spring-boot:run -Drun.jvmArguments="-Dspring.profiles.active=production"

With debug option:

使用调试选项:

mvn spring-boot:run -Drun.jvmArguments="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005 -Dspring.profiles.active=jpa"

I've seen this trip a lot of people up..hope it helps

我见过很多人参加这次旅行..希望它有帮助

回答by Rothin Sen

There's another way by setting the OSvariable, SPRING_PROFILES_ACTIVE.

还有另一种设置操作系统变量SPRING_PROFILES_ACTIVE 的方法

for eg :

例如:

SPRING_PROFILES_ACTIVE=dev gradle clean bootRun

Reference : How to set active Spring profiles

参考:如何设置活动的 Spring 配置文件

回答by Sma Ma

My best practice is to define this as a VM "-D" argument. Please note the differences between spring boot 1.x and 2.x.

我的最佳实践是将其定义为 VM“-D”参数。请注意 spring boot 1.x 和 2.x 之间的差异。

The profiles to enable can be specified on the command line:

可以在命令行上指定要启用的配置文件:

Spring-Boot 2.x

弹簧启动 2.x

-Dspring-boot.run.profiles=local

Spring-Boot 1.x

弹簧启动 1.x

-Dspring.profiles.active=local

example usage with maven:

Maven 的示例用法:

Spring-Boot 2.x

弹簧启动 2.x

mvn spring-boot:run -Dspring-boot.run.profiles=local

Spring-Boot 1.x

弹簧启动 1.x

mvn spring-boot:run -Dspring.profiles.active=local

Make sure to separate them with a comma for multiple profiles:

对于多个配置文件,请确保用逗号分隔它们:

mvn spring-boot:run -Dspring.profiles.active=local,foo,bar
mvn spring-boot:run -Dspring-boot.run.profiles=local,foo,bar

回答by FredFlinstone

If you use Gradle:

如果您使用 Gradle:

-Pspring.profiles.active=local