我有一个弹簧启动应用程序。
我的应用程序中有三个配置文件-> 开发、登台和生产。所以我有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
命令行
您可以通过两种不同的方式在命令行上添加/覆盖弹簧属性。
选项 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
我的最佳做法是将其定义为 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
-Dspring-boot.run.profiles=local
不起作用,-Dspring.profiles.active=local
起作用。
-Dspring-boot.run.profiles
和 -Dspring.profiles.active
-Dspring.profiles.active=staging -Dspring.config.location=C:\Config
是不正确的。
应该:
--spring.profiles.active=staging --spring.config.location=C:\Config
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
还有另一种方法是设置 OS 环境变量 SPRING_PROFILES_ACTIVE。
例如:
SPRING_PROFILES_ACTIVE=dev gradle clean bootRun
参考:How to set active Spring profiles
我不得不添加这个:
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
映射。
您可以使用以下命令行:
java -jar -Dspring.profiles.active=[yourProfileName] target/[yourJar].jar
通过 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/…
springmvn="mvn clean spring-boot:run -Dspring.profiles.active=local -Dspring-boot.run.profiles=local"
我认为您的问题可能与您的 spring.config.location 没有以“/”结束路径有关。
引用文档
如果 spring.config.location 包含目录(而不是文件),它们应该以 / 结尾(并且在加载之前将附加从 spring.config.name 生成的名称)。
Michael Yin 的回答是正确的,但似乎需要更好的解释!
很多人提到 -D
是指定 JVM 参数的正确方法,您是绝对正确的。但正如 Spring Boot Profiles 文档中所述,Michael 也是正确的。
文档中不清楚的是它是什么类型的参数:--spring.profiles.active
不是标准的 JVM 参数,因此如果您想在 IDE 中使用它,请填写正确的字段(即程序参数)
对我来说,帮助在配置文件位置的末尾添加“/”。
java -jar myjar.jar --spring.config.additional-location=env/ --spring.profiles.active=prod
如果你使用 Gradle:
-Pspring.profiles.active=local
我们希望根据 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/
我在 intellij 上执行此操作的一种方法是在命令上设置环境变量,如下所示:
https://i.stack.imgur.com/EiL0d.png
在这种情况下,我将配置文件设置为测试
我在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"
就像一个插件一样,如果您在 application.properties
文件中提到了一个属性,并且您需要从另一个配置文件中覆盖该属性,您可以使用下面的属性 spring.config.additional-location
(使用 -D,因为您从命令行传递)我们曾经使用它是因为我们在 jar 中有一个 application.properties
,在每个服务器的配置文件夹中有一个外部的,用于覆盖任何服务器特定的属性。
不定期副业成功案例分享
~/.bash_profile
导出的 env variableSPRING_PROFILES_ACTIVE
。export SPRING_PROFILES_ACTIVE=e2e
java -Dspring.profiles.active=$ENV -Dspring.config.location=file:///aws-secrets-manager/properties/application-$ENV.properties /code/app.jar