【问题标题】:SpringBoot maven build war using specific application.propertiesSpring Boot maven 使用特定的 application.properties 构建战争
【发布时间】:2016-03-02 20:24:32
【问题描述】:

我有一个 Spring Boot 应用程序,其中包含三个 application.properties 文件:

  1. application.properties
  2. application-qa.properties
  3. application-prod.properties

我想通过使用属性之一来使用 maven 构建战争,具体取决于应用程序部署的环境,即 devqaprod

我该怎么做。我对 Spring Boot 很陌生。

【问题讨论】:

    标签: java spring maven spring-boot


    【解决方案1】:

    设置SPRING_PROFILES_ACTIVE 环境变量将激活预期的配置文件。例如,要激活qa 配置文件,请将SPRING_PROFILES_ACTIVE 环境变量设置为qa。在 Spring Boot 文档中阅读有关配置文件特定属性 here 的更多信息。

    【讨论】:

    • 我有一台 Windows 机器,我确实将环境变量 SPRING_PROFILES_ACTIVE 设置为 qa。但是当我在 tomcat 中运行应用程序时,它仍然使用 application.properties 文件中的值?
    • 不,我右键单击我的电脑 -> 属性 -> 高级系统属性 -> 环境变量。我需要使用 set 命令设置它吗?
    • 我也尝试使用命令行设置它:setx SPRING_PROFILES_ACTIVE qa。它仍然从 application.properties 加载值:( ?
    • application.properties 中的spring.profiles.default 设置为您想要的配置文件,然后构建项目
    • 您不想为每个环境构建战争,因为您想提升经过测试的二进制文件。您不想仅仅因为您需要迁移到不同的环境而进行重建。如果你这样做,你基本上必须重新测试,因为它是一个新的二进制文件......
    【解决方案2】:

    1. 接近

    在 Linux 环境中

    export SPRING_PROFILES_ACTIVE=qa 
    

    您可以使用以下命令检查它是否在操作系统上设置:

    $ env
    

    之后就可以运行了:

    ./gradlew bootRun
    

    所以你将能够在启动时看到这一行:

    2018-05-17 17:33:21.722  INFO 8245 --- [main] 
    b.j.t.d.InteropDocumentosApplication     : The following profiles are active: qa
    

    2. 接近

    通过 gradle 构建:

    关于application.properties

    spring.profiles.active=@activeProfile@
    

    关于build.gradle

    import org.apache.tools.ant.filters.ReplaceTokens
    
    processResources {
         filter ReplaceTokens, tokens: [ 
                 activeProfile: project.property('activeProfile')
         ]
    }
    

    您可以使用名为 activeProfile 的属性。因此,您必须在运行构建时提供 qa 值:

    ./gradlew build -PactiveProfile=qa
    

    【讨论】:

      猜你喜欢
      • 2014-12-31
      • 2017-01-31
      • 1970-01-01
      • 2022-11-10
      • 1970-01-01
      • 2018-02-11
      • 2019-03-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多