3

I'm trying to set the active spring profile when building a WAR file. I'm building the WAR file with gradle bootWar

I managed to find a solution that works for gradle bootRun -Pprofiles=prod

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

But

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

Gives me this error

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

How do I make it work for WAR files?

4

1 回答 1

4

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


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

build.gradle您可以创建一个任务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任务它依赖于我们之前做的这个任务

bootWar {
    dependsOn setProductionConfig
}
于 2018-06-08T14:37:26.703 回答