Spring Boot:application.properties文件路径

3
假设我想从 /tmp/application.properties 加载 application.properties,当我通常使用以下命令运行应用程序时,该怎么做?
```shell $ java -jar myapp.jar ```
mvn spring-boot:run

我阅读了所有的文档和StackOverflow帖子,但是我无法让它工作。 通常我会发现:
java -jar -Dspring.config.location=<path-to-file> myBootProject.jar

但是,通过mvn-spring-boot:run执行时似乎不起作用。 我尝试过,但没有成功:
mvn spring-boot:run -Drun.jvmArguments="-Dspring.config.location=/tmp/application.properties"
1个回答

6

spring-boot:run目标有一个可选参数,正是用于此目的:spring-boot.run.arguments

在您的情况下,您想配置的属性是--spring.config.location=<path-to-file>。因此,您可以使用以下命令:

mvn spring-boot:run -Dspring-boot.run.arguments=--spring.config.location=<path-to-file>

来源:https://docs.spring.io/spring-boot/docs/current/maven-plugin/run-mojo.html#arguments

这是Spring Boot运行插件的官方文档,其中介绍了命令行参数的使用方法。

1
有一篇Baeldung文章错误地说mvn spring-boot:run -Dspring.config.location="file:///Users/home/application.properties"是可行的,这让我和许多人走上了一个令人沮丧的道路。感谢您展示正确的做法! - devdanke

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接