我正在尝试在构建 WAR 文件时设置事件的 spring 配置文件。我正在使用 gradle bootWar
我设法找到了适用于 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/