ChatGPT解决这个技术问题 Extra ChatGPT

在 Spring Boot 中从命令行设置活动配置文件和配置位置

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

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

application-development.yml application-staging.yml application-production.yml

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

spring:
  profiles.active: development

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

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

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

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

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

您能否也添加您的 bootRun 命令行
到目前为止,我是从 eclipse 而不是命令行运行它。但我尝试使用“gradle bootRun -Dspring.config.location=C:\Config\ -Dspring.profiles.active=staging”运行并得到相同的结果。

M
Mark Rotteveel

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

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

重要的是 -D 参数在您的 application.jar 之前,否则它们将无法识别。

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

选项 2:程序参数

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

-D 参数的顺序非常重要:)
在部署 tomcat 容器时如何实现这一点?在那种情况下,我只是将我的战争放在 tomcat 的 webapps 文件夹中,我如何提供配置文件信息?通过设置系统属性?
@maneesh 是的,我正在使用通过 ~/.bash_profile 导出的 env variable SPRING_PROFILES_ACTIVEexport SPRING_PROFILES_ACTIVE=e2e
当您说顺序很重要时:我们是否可以同时传入两个参数:-Dspring.profile.active 和 -Dspring.config.location 以便根据第一个参数设置配置文件,并根据第二个参数?例如:java -Dspring.profiles.active=$ENV -Dspring.config.location=file:///aws-secrets-manager/properties/application-$ENV.properties /code/app.jar
在我添加引号之前,选项 1 对我不起作用: java -jar -D"spring.profiles.active"=prod application.jar
S
Shawrup

我的最佳做法是将其定义为 VM“-D”参数。请注意 spring boot 1.x 和 2.x 之间的区别。

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

Spring-Boot 2.x(仅适用于 maven)

-Dspring-boot.run.profiles=local

弹簧启动 1.x

-Dspring.profiles.active=local

使用 maven 的示例用法:

弹簧启动 2.x

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

Spring-Boot 1.x 和 2.x

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

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

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

你的意思是 spring / spring-boot ? (弹簧 1x 和弹簧 2x)!
@smilyface 弹簧靴。 spring boot 也有不同的版本:mvnrepository.com/artifact/org.springframework.boot/spring-boot
我使用 spring-boot 2.1.3,-Dspring-boot.run.profiles=local 不起作用,-Dspring.profiles.active=local 起作用。
spring-boot 2.2.0:同时工作:-Dspring-boot.run.profiles-Dspring.profiles.active
U
Unheilig
-Dspring.profiles.active=staging -Dspring.config.location=C:\Config

是不正确的。

应该:

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

这会导致错误“无法识别的选项:--spring.config.location”
-D 是设置 Java 系统属性的正确方法。 --something 是一个 bash 参数。
--spring.profiles.active 为我工作,与我从 docs.spring.io/spring-boot/docs/current/reference/html/… 中提到的相同
在 Eclipse 中使用 Run As -> Java Application 时,这也适用于我
实际上两者都是正确的,这取决于它的使用方式:它可以是 java -Dspring.profiles.active=staging -Dspring.config.location=C:\Config your-spring-boot-app.jarjava your-spring-boot.jar --spring.profiles.active=staging --spring.config.location=C:\Config
r
rogerdpack

还有另一种方法是设置 OS 环境变量 SPRING_PROFILES_ACTIVE。

例如:

SPRING_PROFILES_ACTIVE=dev gradle clean bootRun

参考:How to set active Spring profiles


是的,这避免了 Gradle 将系统属性传递给应用程序的方式。
这是整洁的方式。它还应该用于设置数据库用户和密码以及其他敏感配置,以免它们在版本控制中被检查。
V
Vinod Mohanan

我不得不添加这个:

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 获取配置文件和配置位置。

非常感谢@jst 的指针。


这可以更简单,如下所示:bootRun { systemProperties = System.properties }。此命令将使用相同键的 -D 开关传递的所有参数复制到 systemProperty 映射。
这似乎是一个 gradle only 解决方案,没有通用解决方案吗?
你到底在哪里添加这个? build.gradle 文件中的任何位置或文件中的特定位置?
@user1767316 您可以使用 SPRING_PROFILES_ACTIVE=profile 设置环境变量,就像一个魅力
R
Rollen Holt

您可以使用以下命令行:

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

J
JARC

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

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

带调试选项:

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

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


已更改为 mvn spring-boot:run -Dspring-boot.run.profiles=foo,bar,请参阅:docs.spring.io/spring-boot/docs/current/maven-plugin/examples/…
@rwenz3l 谢谢!这对我有用,只是将一个项目从 Spring Boot 1 升级到 2。现在我只需将它们全部添加到我的 bashrc 中...... springmvn="mvn clean spring-boot:run -Dspring.profiles.active=local -Dspring-boot.run.profiles=local"
j
jst

我认为您的问题可能与您的 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


谢谢你指出。但是,当我运行 -Dspring.profiles.active=staging -Dspring.config.location=C:\Config\ 时也会给我同样的问题。即使是活跃的个人资料也没有得到反映。我认为由于某种原因我的命令行没有被忽略。
您应该按照此问题中给出的建议将参数传递给 bootRun stackoverflow.com/questions/25079244/…
谢谢你。这真的很有帮助。
b
ben3000

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

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

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


u
user3650655

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

java -jar myjar.jar --spring.config.additional-location=env/ --spring.profiles.active=prod


F
FredFlinstone

如果你使用 Gradle:

-Pspring.profiles.active=local

J
JoSSte

我们希望根据 spring.profiles.active 中提到的配置文件名称和 -Dspring.config.location 中的路径自动选择属性文件

application-dev.properties

如果我们在 Unix OS 中运行 jar,那么我们必须在 -Dspring.config.location 的末尾使用 /,否则会出现以下错误。

错误 :: java.lang.IllegalStateException:任何 PropertySourceLoader 都不知道配置文件位置“file:/home/xyz/projectName/cfg”的文件扩展名。如果该位置是为了引用一个目录,它必须以“/”结尾

例子

java -Dspring.profiles.active=dev -Dspring.config.location=/home/xyz/projectName/cfg/ -jar /home/xyz/project/abc.jar

或者

java -jar /home/xyz/project/abc.jar --spring.profiles.active=dev --spring.config.location=/home/xyz/projectName/cfg/

v
vinicius gati

我在 intellij 上执行此操作的一种方法是在命令上设置环境变量,如下所示:

https://i.stack.imgur.com/EiL0d.png

在这种情况下,我将配置文件设置为测试


O
O_K

我在springboot的命令行中使用不同的配置文件运行测试时遇到了类似的问题。我通过首先设置配置文件然后运行测试命令来解决这个问题,如下所示:

第 1 步:export SPRING_PROFILES_ACTIVE=test(for mac/linux) 或 SET SPRING_PROFILES_ACTIVE=test(for windows)

第二步:./gradlew test --tests "com.maersk.snd.integrationtest.IntegrationTestPOC"

上面的命令可以组合在一起,如下所示:

export SPRING_PROFILES_ACTIVE=test && ./gradlew test --tests "com.maersk.snd.integrationtest.IntegrationTestPOC"


C
Cozimetzer

就像一个插件一样,如果您在 application.properties 文件中提到了一个属性,并且您需要从另一个配置文件中覆盖该属性,您可以使用下面的属性 spring.config.additional-location(使用 -D,因为您从命令行传递)我们曾经使用它是因为我们在 jar 中有一个 application.properties,在每个服务器的配置文件夹中有一个外部的,用于覆盖任何服务器特定的属性。