【问题标题】:how to pass application.properties in commandLine for a spring boot application?如何在命令行中为 Spring Boot 应用程序传递 application.properties?
【发布时间】:2025-12-12 21:30:03
【问题描述】:

我有一个spring boot 应用程序,当我start-up 时,我想在commandLine 中传递application.properties 文件。

即当我运行mvn spring-boot:run --application.properties

我将在src/main/resources 中有一个默认的 application.properties。但这仅用于testing 目的。在production 运行中,我想在commandLine. 中传递property file

我知道传递单个参数,例如

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

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

【问题讨论】:

    标签: spring-boot spring-4


    【解决方案1】:

    如果有人发现它对我有用。如果您想在使用 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'
    

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

    spring.datasource.url=jdbc:postgresql://localhost:5432/mydb
    spring.datasource.username=admin
    spring.datasource.password=admin
    

    【讨论】:

      【解决方案2】:

      mvn spring-boot:run -Dspring-boot.run.arguments=--spring.config.location=classpath:/application-local.properties

      【讨论】:

        【解决方案3】:

        您可以使用spring.config.location 属性来做到这一点:

        mvn spring-boot:run -Dspring.config.location=your.properties
        

        【讨论】:

          最近更新 更多