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

O-O*_*F-N 134 java spring yaml gradle spring-boot

我有一个春季启动应用程序.

我的应用程序中有三个配置文件 - > 开发,登台和生产.所以我有3个文件

  1. 应用development.yml
  2. 应用staging.yml
  3. 应用production.yml

我的application.yml驻留在里面src/main/resources.我已将application.yml中的活动配置文件设置为:

spring:
  profiles.active: development
Run Code Online (Sandbox Code Playgroud)

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

我正在使用gradle插件进行eclipse.当我尝试执行" bootRun "时,我在eclipse中的gradle配置中设置命令行参数

 -Dspring.profiles.active=staging -Dspring.config.location=C:\Config
Run Code Online (Sandbox Code Playgroud)

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

我想我在这里遗漏了一些东西.过去两天我一直试图解决这个问题.但没有运气.我真的很感激任何帮助.

Ren*_*Ren 272

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

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

重要的是-D参数在application.jar之前,否则它们不会被识别.

java -jar -Dspring.profiles.active=prod application.jar
Run Code Online (Sandbox Code Playgroud)


选项2:计划参数

java -jar application.jar --spring.profiles.active=prod --spring.config.location=c:\config
Run Code Online (Sandbox Code Playgroud)

  • -D参数的顺序非常重要:) (33认同)
  • 如何在部署tomcat容器时实现这一目标?在那种情况下,我只是将我的战争放到tomcat的webapps文件夹中,我该如何提供个人资料信息?通过设置系统属性? (3认同)
  • 选项 1 对我不起作用,直到我添加引号: **java -jar -D"spring.profiles.active"=prod application.jar** (3认同)
  • @maneesh 是的,我正在使用 [env 变量](https://github.com/prayagupd/eccount-rest/tree/REST-API-load-balancing) 通过 `~/.bash_profile` 导出的 `SPRING_PROFILES_ACTIVE`。`导出 SPRING_PROFILES_ACTIVE=e2e` (2认同)

O-O*_*F-N 16

我不得不补充一点:

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"
}
Run Code Online (Sandbox Code Playgroud)

现在bootRun选择了配置文件和配置位置.

非常感谢@jst指针.

  • 这可以更加简单如下:`bootRun {systemProperties = System.properties}`.此命令将使用相同键将`-D`开关传递的所有参数复制到`systemProperty`映射. (7认同)
  • 你到底在哪里添加这个?build.gradle 文件中的任何位置或文件中的特定位置? (2认同)

小智 11

-Dspring.profiles.active=staging -Dspring.config.location=C:\Config
Run Code Online (Sandbox Code Playgroud)

是不正确的.

应该:

--spring.profiles.active=staging --spring.config.location=C:\Config
Run Code Online (Sandbox Code Playgroud)

  • 实际上两者都是正确的,这取决于使用方式:可以是java -Dspring.profiles.active = staging -Dspring.config.location = C:\ Config your-spring-boot-app.jar或Java your-spring-boot.jar --spring.profiles.active = staging --spring.config.location = C:\ Config` (8认同)
  • 这会导致错误"无法识别的选项: - spring.config.location" (3认同)
  • -D 是设置 Java 系统属性的正确方法。--something 是一个 bash 参数。 (2认同)

Rol*_*olt 8

您可以使用以下命令行:

java -jar -Dspring.profiles.active=[yourProfileName] target/[yourJar].jar
Run Code Online (Sandbox Code Playgroud)


JAR*_*ARC 7

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

mvn spring-boot:run -Drun.jvmArguments="-Dspring.profiles.active=production"
Run Code Online (Sandbox Code Playgroud)

使用调试选项:

mvn spring-boot:run -Drun.jvmArguments="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005 -Dspring.profiles.active=jpa"
Run Code Online (Sandbox Code Playgroud)

我看过这次旅行很多人......希望它有所帮助

  • 已更改为`mvn spring-boot:run -Dspring-boot.run.profiles=foo,bar`,请参阅:https://docs.spring.io/spring-boot/docs/current/maven-plugin/examples/运行-profiles.html (2认同)

Rot*_*Sen 7

通过设置OS变量SPRING_PROFILES_ACTIVE还有另一种方法.

例如:

SPRING_PROFILES_ACTIVE=dev gradle clean bootRun
Run Code Online (Sandbox Code Playgroud)

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


小智 7

这将解决您在 Windows 上的问题:

mvn spring-boot:run -D"spring-boot.run.profiles"=env
Run Code Online (Sandbox Code Playgroud)


jst*_*jst 6

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

引用文档

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

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


Eri*_*aix 5

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

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

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


Sma*_* Ma 5

我的最佳实践是将其定义为VM“ -D”参数。请注意Spring Boot 1.x和2.x之间的区别。

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

春季启动2.x

-Dspring-boot.run.profiles=local
Run Code Online (Sandbox Code Playgroud)

春季启动1.x

-Dspring.profiles.active=local
Run Code Online (Sandbox Code Playgroud)

Maven的示例用法:

春季启动2.x

mvn spring-boot:run -Dspring-boot.run.profiles=local
Run Code Online (Sandbox Code Playgroud)

春季启动1.x

mvn spring-boot:run -Dspring.profiles.active=local
Run Code Online (Sandbox Code Playgroud)

确保用逗号分隔多个配置文件:

mvn spring-boot:run -Dspring.profiles.active=local,foo,bar
Run Code Online (Sandbox Code Playgroud)
mvn spring-boot:run -Dspring-boot.run.profiles=local,foo,bar
Run Code Online (Sandbox Code Playgroud)

  • 我使用 spring-boot 2.1.3,并且 `-Dspring-boot.run.profiles=local` 不起作用,`-Dspring.profiles.active=local` 起作用。 (4认同)
  • 此答案的文档:https://docs.spring.io/spring-boot/docs/current/maven-plugin/reference/htmlsingle/#run-example-active-profiles (2认同)

use*_*655 5

对我来说,帮助在配置文件位置的末尾添加“/”。

java -jar myjar.jar --spring.config.additional-location=env/ --spring.profiles.active=prod
Run Code Online (Sandbox Code Playgroud)