简体   繁体   English

使用 Gradle 管理 Spring 引导配置文件

[英]Manage Spring Boot Profiles with Gradle

I have a Spring boot application which is managed by Gradle.我有一个由 Gradle 管理的 Spring 引导应用程序。

What is done so far:到目前为止做了什么:

  1. Created application.yaml , application-dev.yaml , and application-qa.yaml files创建application.yamlapplication-dev.yamlapplication-qa.yaml文件
  2. Mentioned spring: profiles: active: dev in application.yaml (default profile should be dev if none is mentioned)提到spring: profiles: active: dev application.yaml如果未提及,默认配置文件应为dev人员)

What need to be done:需要做什么:

  1. Need to build war with profile mentioned or passed while building.需要在构建时使用提到或传递的配置文件来构建war Example, for QA build, spring: profiles: active: should have value qa , so that it can pick-up application-qa.yaml properties.例如,对于 QA 构建, spring: profiles: active:应该具有值qa ,以便它可以获取application-qa.yaml属性。
  2. If no profile is passed, then by default dev profile should be selected如果没有通过配置文件,则默认情况下应选择dev配置文件

What is tried so far:到目前为止尝试了什么:

  1. Added @activeProfiles@ in spring: profiles: active: @activeProfiles@ in application.yaml and added below snippet in build.gradle :在 spring 中添加了@activeProfiles@ spring: profiles: active: @activeProfiles@application.yaml中,并在build.gradle中添加了以下代码段:

     processResources { filter org.apache.tools.ant.filters.ReplaceTokens, tokens: [ activeProfiles: activeProfiles ] }

    And fired following command to build the war - gradlew clean build -PactiveProfiles=qa , and it just did right job for war file.并触发以下命令来构建war - gradlew clean build -PactiveProfiles=qa ,它为war文件做了正确的工作。 But now issue with this approach is, how to provide default value for activeProfiles while running the project from IDE (I usually prefer to run main class from IntelliJ)?但是现在这种方法的问题是,如何在从 IDE 运行项目时为activeProfiles提供默认值(我通常更喜欢从 IntelliJ 运行main class)?

I am also not sure if this is the correct approach or is there any other approach to achieve this task.我也不确定这是否是正确的方法,或者是否有任何其他方法可以完成此任务。 I have already done all these stuff with Maven, and with ease, but I am new to Gradle, and it's project requirement to use Gradle.我已经用 Maven 轻松完成了所有这些工作,但我是 Gradle 的新手,使用 Gradle 是项目要求。

One common approach to switch between profiles in different environments is to set up SPRING_PROFILES_ACTIVE environment variable to your desired value and let Spring boot use that.在不同环境中切换配置文件的一种常见方法是将SPRING_PROFILES_ACTIVE环境变量设置为您想要的值,然后让 Spring 启动使用它。 For example, in QA environment you would set the value to qa and Spring will automatically use qa application properties.例如,在 QA 环境中,您可以将值设置为 qa,Spring 将自动使用 qa 应用程序属性。 Read more: https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html#howto-set-active-spring-profiles阅读更多: https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html#howto-set-active-spring-profiles

I was able to set default profile, if no profile is passed while running/building the code, using below configurations in build.gradle file.如果在运行/构建代码时没有传递配置文件,我可以设置默认配置文件,使用build.gradle文件中的以下配置。 No extra configuration is needed in any IDE to run the project.在任何 IDE 中都不需要额外的配置来运行项目。

def activeProfiles=project.properties['activeProfiles'] ?: "dev" // set default (dev) profile if no profile is passed as property

processResources {
    filter org.apache.tools.ant.filters.ReplaceTokens, tokens: [
        activeProfiles: activeProfiles
    ]
}

I have 3 yaml files for configurations as per environments:我有 3 个 yaml 文件,用于根据环境进行配置:

  1. application.yaml
  2. application-dev.yaml
  3. application-qa.yaml

application.yaml contains below configuration for spring profile management, and other common configurations: application.yaml包含 spring 配置文件管理的以下配置,以及其他常见配置:

spring:
    profiles:
        active: @activeProfiles@
...

application-dev.yaml and application-qa.yaml files contain configurations related to respective environments. application-dev.yamlapplication-qa.yaml文件包含与各自环境相关的配置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM