【问题标题】:How to create a gradle task that will execute bootRun with a specific profile?如何创建将使用特定配置文件执行 bootRun 的 gradle 任务?
【发布时间】:2025-12-26 07:05:07
【问题描述】:

我本质上想在 gradle 中创建一个执行命令的任务

gradle bootRun -Dspring.profiles.active=test

如果从命令行执行,此命令完全符合我的要求,但我没有运气尝试在任务上使用type:Exec,也没有运气传递System properties

我真的不想把它变成用户需要知道才能运行的外部命令。我希望它显示在任务/其他下。

到目前为止我最接近的尝试:

task bootRunTest() {
    executable "gradle"
    args "-Dspring.profiles.active=test bootRun"
 }

【问题讨论】:

    标签: spring gradle spring-boot


    【解决方案1】:

    我试图创建的任务是这样的:

    task bootRunTest(type: org.springframework.boot.gradle.run.BootRunTask, dependsOn: 'build') {
        group = 'Application'
        doFirst() {
            main = project.mainClassName
            classpath = sourceSets.main.runtimeClasspath
            systemProperty 'spring.profiles.active', 'test'
        }
    }
    

    【讨论】:

    • 如何更改此特定任务的 webapp 文件夹路径?
    • 这个必须dependencies块之后。
    • 对我来说必须是org.springframework.boot.gradle.tasks.run.BootRun
    【解决方案2】:

    这里是您如何设置要运行的任务的属性,在本例中为 bootRun

    Build.gradle

    中添加
    bootRun {
            systemProperty "spring.profiles.active", "test,qa,ect"
    }
    

    然后从命令行

    gradle bootRun
    

    【讨论】:

    • 不应该是systemProperty "spring.profiles.active", "test"而不是"test,qa,ect"吗?
    【解决方案3】:

    您也可以通过将 OS 变量 SPRING_PROFILES_ACTIVE 设置为特定配置文件来实现。

    例如:

    SPRING_PROFILES_ACTIVE=dev gradle clean bootRun
    

    【讨论】:

      最近更新 更多