【问题标题】:Using Spring Boot profiles with command line arguments使用带有命令行参数的 Spring Boot 配置文件
【发布时间】:2020-02-26 17:17:15
【问题描述】:

我对如何在 Spring Boot 应用程序中使用配置文件感到困惑。

假设我在pom.xml 文件中定义了配置文件, 这是我发现的不同方法。

mvnw -Pdev

mvnw spring-boot:run -Dspring-boot.run.profiles=dev

mvnw spring-boot:run -Dspring.profiles.active=dev

mvnw spring-boot:run --spring.profiles.active=dev

1/ 它们之间有什么区别吗?

2/ 最好的使用方法是什么?

【问题讨论】:

    标签: maven spring-boot spring-boot-maven-plugin maven-profiles


    【解决方案1】:

    您不应该混淆 Maven 配置文件和 Spring 配置文件 - 这是完全不同的东西。

    mvnw spring-boot:run -Dspring.profiles.active=dev - 它告诉 Spring 在运行时使用哪个配置文件。

    Spring 配置文件 - 是您的 Spring 应用程序在运行时根据活动配置文件进行配置的方式。 你可以阅读更多关于它的信息herehere

    mvnw -P dev - 它告诉 Maven 使用构建 Maven 配置文件

    Maven 配置文件 - 编译/构建/打包 Java 应用程序的方式。它与您的 Spring 应用程序的编译/打包有关,而不是运行时执行,并且与 Spring 完全无关。 你可以阅读更多关于它的信息herehere

    这是您第一个问题的答案。

    关于你的第二个问题, 当您需要根据配置文件设置以不同方式构建应用程序时,将使用 Maven 配置文件。例如,在执行mvn -P fat-jar 时,打包一个包含所有依赖项的“fat jar”。

    当您希望将 Spring 应用程序配置为在运行时以不同方式工作时,使用 Spring 配置文件,例如 - 使用 developmentproduction 数据库等。

    【讨论】:

      【解决方案2】:

      mvnw -Pdev

      这将考虑 ma​​ven 配置文件

      mvnw spring-boot:run -Dspring-boot.run.profiles=dev

      这将考虑 ma​​ven 配置文件

      mvnw spring-boot:run -Dspring.profiles.active=dev

      这将考虑弹簧启动配置文件

      mvnw spring-boot:run --spring.profiles.active=dev

      这不应该工作,因为 maven 不理解 --spring

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-05-08
        • 2016-03-08
        • 2017-07-03
        • 1970-01-01
        • 2014-10-11
        • 2015-12-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多