【问题标题】:Set active spring profile with bootWar使用 bootWar 设置活动弹簧配置文件
【发布时间】:2018-11-18 12:39:08
【问题描述】:

我正在尝试在构建 WAR 文件时设置活动弹簧配置文件。我正在使用 gradle bootWar 构建 WAR 文件

我设法找到了适用于gradle bootRun -Pprofiles=prod的解决方案

bootRun {
  if (project.hasProperty('profiles')) {
    environment SPRING_PROFILES_ACTIVE: profiles
  }
}

但是

bootWar {
  if (project.hasProperty('profiles')) {
    environment SPRING_PROFILES_ACTIVE: profiles
  }
}

给我这个错误

在 org.springframework.boot.gradle.tasks.bundling.BootWar 类型的任务 ':bootWar' 上找不到参数 [{SPRING_PROFILES_ACTIVE=staging}] 的方法 environment()。

如何使它适用于 WAR 文件?

【问题讨论】:

    标签: spring-boot gradle war spring-profiles spring-boot-gradle-plugin


    【解决方案1】:

    (链接指的是一个演示项目,我在其中做与您现在尝试做的相同的操作,但有一些额外的复杂性,请先阅读属性等并将不同的配置文件设置为活动状态:developmenttestingproduction 和其他一些 SO 帖子)


    假设我们要将活动配置文件设置为production

    build.gradle 中,您可以创建一个task,它使用ant.propertyfile 写入属性spring.profiles.active,如下所示:

    task setProductionConfig() {
        group = "other"
        description = "Sets the environment for production mode"
        doFirst {
            /*
               notice the file location ("./build/resources/main/application.properties"),
               it refers to the file processed and already in the build folder, ready
               to be packed, if you use a different folder from `build`, put yours
               here
            */
            ant.propertyfile(file: "./build/resources/main/application.properties") {
                entry(key: "spring.profiles.active", value: "production")
            }
        }
        doLast {
            // maybe put some notifications in console here
        }
    }
    

    然后你告诉bootWar 任务它依赖于我们之前创建的这个任务,比如this

    bootWar {
        dependsOn setProductionConfig
    }
    

    【讨论】:

    • 在使用 .yml 文件时您有什么解决办法吗?
    • 从未尝试过使用 yml 文件。不过,那会很有趣。
    猜你喜欢
    • 2014-10-14
    • 2016-12-25
    • 2020-01-24
    • 1970-01-01
    • 1970-01-01
    • 2018-11-27
    • 2020-03-24
    • 2016-12-09
    • 1970-01-01
    相关资源
    最近更新 更多