spring-boot - 使用 bootWar 设置事件 Spring 配置文件

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

我正在尝试在构建 WAR 文件时设置事件的 spring 配置文件。我正在使用 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
  }
}

给我这个错误

Could not find method environment() for arguments [{SPRING_PROFILES_ACTIVE=staging}] on task ':bootWar' of type org.springframework.boot.gradle.tasks.bundling.BootWar.

如何使其适用于 WAR 文件?

最佳答案

(链接引用了一个演示项目,在该项目中我做了与您现在尝试做的相同的事情,但有一些额外的复杂性,首先读取属性等并将不同的配置文件设置为事件状态:开发测试生产和其他一些SO帖子)

<小时/>

假设我们要将事件配置文件设置为生产

build.gradle 中,您可以创建一个 task其中写入属性 spring.profiles.active使用ant.propertyfile像这样:

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
}

关于spring-boot - 使用 bootWar 设置事件 Spring 配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50760851/

相关文章:

java - 使用maven构建后.properties文件的路径

c++ - 如何将 OpenCV 集成到 gradle 构建的 C++ 项目中

war - Gradle ear 放的是 JAR 而不是 WAR

java - gradle build<配置名称> : different packaging for ear and war (gradle v2. 3)

java - 查询包含 java.sql.Date 列的表时,Jpa findAll() 会导致错误

java - 使用 Spring Boot Java 的补丁请求

java - 将 UUID 值插入 PostgreSQL 数据库时出现 Liquibase 问题

gradle - 为什么gradle的buildscript类路径不包含buildSrc?

Android Gradle keyAlias 变量存储在单独的文件中

java - 将 WAR 文件上传到 WebSphere 6.1 时出现问题