【问题标题】:Spring boot war with active profile using gradle使用 gradle 的带有活动配置文件的 Spring Boot War
【发布时间】: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


【解决方案1】:

找出我使用 maven 替换需要使用 Gradle 替换 spring.profiles.active=${activeProfile} 并在 project.properties 中定义此值的问题

【讨论】:

    猜你喜欢
    • 2018-11-12
    • 2021-05-09
    • 2017-01-25
    • 1970-01-01
    • 2018-02-19
    • 2018-11-03
    • 2014-09-07
    • 2017-11-24
    相关资源
    最近更新 更多