我想更改我的 gradle springboot 应用程序的 spring.config.additional-location 以在本地运行。我的 C:/demo_class_path
中有一个属性文件,它位于 jar 之外。我试图在代码中访问这些属性。
用于运行 jar wtth 参数的命令 java -jar demo-application.jar spring.config.additional-location=file:C:/demo_class_path 有效,我将能够获取我需要的资源。但我试图在 bootRun 任务中添加参数,但没有成功。
我尝试了下面的代码:
bootRun {
systemProperties = [
'spring.config.additional-location' : "file:C:/demo_class_path",
'server.port' : 8090
]
}
或
bootRun {
jvmArgs = [
"-Dspring.config.additional-location=file:C:/demo_class_path/",
"-Dserver.port=8090"
]
}
使用上面的代码,我将能够将端口更改为 8090,但我的文件无法再从路径中获取。 我还尝试添加 spring.config.additional-location=file:C:/demo_class _path to application.properties 也不起作用。我想知道该位置的语法是否错误。既然如此,为什么 java 命令会起作用?
最佳答案
systemProperties
用于将通常使用 -D
传递的属性传递给运行时。
jvmArgs
用于将参数传递给 JVM。
您要使用的是 args
而不是上述任何一项。
bootRun {
args = [
'--spring.config.additional-location=file:C:/demo_class_path/',
'--server.port=8090'
]
}
在 spring.config.additional-location
末尾包含 /
非常重要。当它不以 /
结尾时,它会被解释为文件的基本名称而不是文件位置。
关于spring - 在 bootRun 任务中传递 spring.config.location,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54221060/