【问题标题】:Pass Spring profile in maven build在 Maven 构建中传递 Spring 配置文件
【发布时间】:2016-07-11 21:33:15
【问题描述】:

我通过传递如下的 spring 活动配置文件来运行 spring boot 应用程序:

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

但是在使用 maven 创建包时如何传递 spring.profiles.active 。以下是maven命令:

mvn clean install

【问题讨论】:

  • mvn clean install -Dspring.profiles.active=dev?
  • 不,那是行不通的。当我运行战争时,它说配置文件未设置
  • 那么你需要发布一些代码。从 POM 开始。
  • 您想在构建时使用 maven 设置系统属性 (-Dspring.profiles.active=dev),以便 Spring 可以使用该属性,对吗?

标签: java maven spring-boot


【解决方案1】:

如果有人遇到同样的情况,您可以通过 spring boot 和 maven 的 2 个步骤来实现: 首先在 spring 属性或 yaml 文件中,添加 spring.profiles.active 及其值作为占位符:

spring.profiles.active=@active.profile@

二、用maven传值:

mvn clean install -Dactive.profile=dev

jar/war打包后,值会设置为dev。

您还可以利用 Maven 配置文件:

<profiles>
        <profile>
            <id>dev</id>
            <properties>
                <active.profile>dev</active.profile>
            </properties>
        </profile>
        <profile>
            <id>test</id>
            <properties>
                <active.profile>test</active.profile>
            </properties>
        </profile>
    </profiles>

然后运行:

mvn clean install -Pdev

【讨论】:

    【解决方案2】:

    Maven 是一个构建时工具,使其修改构建工件的最终运行时行为的唯一方法是使用它的 (build-time) 配置文件。这不能与 Spring 的 runtime 配置文件混淆,后者是指示 Spring 容器以特定方式引导应用程序的参数。

    换句话说,spring.profiles.active 参数不会被 maven “烘焙”到 war 文件中,您仍然需要在启动应用程序时传递它,无论是通过命令行参数还是配置文件或您的 servlet 容器提供的任何机制。

    【讨论】:

      【解决方案3】:

      对于包,您可以将安装替换为包

      mvn clean install -P dev

      【讨论】:

        【解决方案4】:

        您可以使用环境变量。

        export SPRING_PROFILES_ACTIVE=some,test,profiles
        mvn spring-boot:run
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-02-16
          • 1970-01-01
          • 2018-10-01
          • 2016-10-13
          • 2013-09-20
          • 2015-09-06
          • 1970-01-01
          相关资源
          最近更新 更多