gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 00:53:19 31 4
gpt4 key购买 nike

我正在尝试在构建 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/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com