【发布时间】:2021-07-17 02:53:47
【问题描述】:
我正在使用 Gradle 将我的 spring boot 项目构建到一个 war 文件中。我想使用不同的配置文件构建不同风格的战争文件。我的 Gradle 文件中有以下内容
tasks.register("bootRunDev") {
group = "application"
description = "Runs the Spring Boot application with the dev profile"
doFirst {
tasks.bootRun.configure {
systemProperty("spring.profiles.active", "dev")
}
}
finalizedBy("bootRun")
}
tasks.register("bootWarDev") {
group = "application"
description = "Runs the Spring Boot application with the dev profile"
doFirst {
tasks.withType<JavaExec> {
systemProperty("spring.profiles.active", "dev")
}
}
finalizedBy("bootWar")
}
./gradlew bootRunDev 命令有效,但我正在努力将其注入战争文件。有什么想法吗?
编辑
我现在正在尝试将战争文件 application.properites 替换为
tasks.register("bootWarDev") {
copySpec {
from("src/main/resources") {
filter<ReplaceTokens>(Pair("activeProfiles", "dev"))
println("run")
}
}
}
但这也不起作用
【问题讨论】:
-
我不确定我是否遵循。 “将其注入战争文件”是什么意思?
bootRun运行应用程序。您将 env 属性spring.profiles.active设置为dev,这意味着dev配置文件将处于活动状态。但是,在生成 WAR 时设置 env 属性有什么意义呢? -
我想让
spring.profiles.active = dev在 war 文件运行时运行。但我认为你的问题有助于我理解这个问题。systemProperty在运行期间设置了一个环境,而不是将其打包到 jar 中
标签: spring-boot gradle build.gradle