O-O*_*F-N 134 java spring yaml gradle spring-boot
我有一个春季启动应用程序.
我的应用程序中有三个配置文件 - > 开发,登台和生产.所以我有3个文件
我的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属性.
重要的是-D参数在application.jar之前,否则它们不会被识别.
java -jar -Dspring.profiles.active=prod application.jar
Run Code Online (Sandbox Code Playgroud)
java -jar application.jar --spring.profiles.active=prod --spring.config.location=c:\config
Run Code Online (Sandbox Code Playgroud)
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指针.
小智 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 -jar -Dspring.profiles.active=[yourProfileName] target/[yourJar].jar
Run Code Online (Sandbox Code Playgroud)
通过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)
我看过这次旅行很多人......希望它有所帮助
通过设置OS变量SPRING_PROFILES_ACTIVE还有另一种方法.
例如:
SPRING_PROFILES_ACTIVE=dev gradle clean bootRun
Run Code Online (Sandbox Code Playgroud)
小智 7
这将解决您在 Windows 上的问题:
mvn spring-boot:run -D"spring-boot.run.profiles"=env
Run Code Online (Sandbox Code Playgroud)
我认为您的问题可能与您的 spring.config.location 没有以“/”结尾的路径有关。
引用文档
如果 spring.config.location 包含目录(而不是文件),它们应该以 / 结尾(并且在加载之前会附加从 spring.config.name 生成的名称)。
Michael Yin的回答是正确的,但似乎需要更好的解释!
你们中的很多人都提到这-D是指定 JVM 参数的正确方法,您是绝对正确的。但正如Spring Boot Profiles文档中提到的,Michael 也是对的。
文档中不清楚的是它是什么类型的参数:--spring.profiles.active不是标准的 JVM 参数,因此如果您想在 IDE 中使用它,请填写正确的字段(即程序参数)
我的最佳实践是将其定义为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)
对我来说,帮助在配置文件位置的末尾添加“/”。
java -jar myjar.jar --spring.config.additional-location=env/ --spring.profiles.active=prodRun Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
199457 次 |
| 最近记录: |