如何在commandLine中传递application.properties以获得Spring启动应用程序?

bra*_*orm 9 spring-boot spring-4

我有一个spring boot应用程序,我想application.propertiescommandLine我的时候传入文件start-up.

即我跑的时候 mvn spring-boot:run --application.properties

我将有一个默认的application.properties src/main/resources.但这只是出于testing目的.在production运行,我想通过property filecommandLine.

我知道传递单个参数,如

mvn spring-boot:run --server.port=9001.

但是我有很多这样的属性,如果可能的话,我更愿意传递一个属性文件.

mzc*_*mzc 15

你可以用spring.config.location财产这样做:

mvn spring-boot:run -Dspring.config.location=your.properties
Run Code Online (Sandbox Code Playgroud)


Deh*_*oos 7

万一有人发现它对我有用。如果您想在使用 maven spring boot run 命令时将各个应用程序属性作为参数传递,您可以使用参数 spring-boot.run.jvmArguments。

例如:

mvn spring-boot:run -Dspring-boot.run.jvmArguments='
-Dspring.datasource.url=jdbc:postgresql://localhost:5432/mydb 
-Dspring.datasource.username=admin 
-Dspring.datasource.password=admin'
Run Code Online (Sandbox Code Playgroud)

使用上面的命令,我设置(覆盖)application.properties 文件中的以下属性。

spring.datasource.url=jdbc:postgresql://localhost:5432/mydb
spring.datasource.username=admin
spring.datasource.password=admin
Run Code Online (Sandbox Code Playgroud)


小智 5

mvn spring-boot:run -Dspring-boot.run.arguments=--spring.config.location=classpath:/application-local.properties
Run Code Online (Sandbox Code Playgroud)