Gradle的mustRunAfter / finalized仅适用于特定任务?

时间:2017-09-25 08:03:47

标签: gradle

我正在尝试让Gradle的mustRunAfterfinalizedBy仅用于特定任务。举个例子build.gradle

task removeTestDatabaseContainer {
  doLast {
    println '\ninside removeTestDatabaseContainer\n'
  }
}

task startTestDatabaseContainer {
  doLast {
    println '\ninside startTestDatabaseContainer\n'
  }
  finalizedBy removeTestDatabaseContainer
}

task flywayMigrate { t->
  doLast {
    println '\n inside flywayMigrate\n'
  }
}

task flywayClean { t->
  doLast {
    println '\n inside flywayClean\n'
  }
}

task testEverything {
    dependsOn startTestDatabaseContainer
    dependsOn flywayMigrate
    dependsOn flywayClean

    flywayMigrate.mustRunAfter startTestDatabaseContainer
    flywayMigrate.finalizedBy flywayClean

    flywayClean.mustRunAfter flywayMigrate
    flywayClean.finalizedBy removeTestDatabaseContainer
}

我对testEverything的工作原理感到满意。我想要从那个任务得到的输出:

➜  gradle testEverything
Parallel execution is an incubating feature.
:startTestDatabaseContainer

inside startTestDatabaseContainer

:flywayMigrate

 inside flywayMigrate

:flywayClean

 inside flywayClean

:removeTestDatabaseContainer

inside removeTestDatabaseContainer

:testEverything

BUILD SUCCESSFUL

Total time: 0.597 secs

然而,当我只运行flywayMigrate时,我遇到了意想不到的问题。这是输出:

➜  gradle flywayMigrate                
Parallel execution is an incubating feature.
:flywayMigrate

 inside flywayMigrate

:flywayClean

 inside flywayClean

:removeTestDatabaseContainer

inside removeTestDatabaseContainer


BUILD SUCCESSFUL

Total time: 0.605 secs

这不是我想要的输出。我希望只有flywayMigrate才能运行。 问题1)如何让testEverything正常工作,同时gradle flywayMigrate仅调用flywayMigrate - 任务?

问题2)
我被告知这与task testEverything {}括号内的所有内容都是配置有关,这一点总是由Gradle处理。因此,在任务中设置的任何mustRunAfter / finalizedBy都将具有“全局效果”。但在这种情况下,为什么gradle flywayMigrate不会调用startTestDatabaseContainer? (因为flywayMigrate.mustRunAfter startTestDatabaseContainer任务中的行testEverything。)

编辑:我被引导到Gradle文档中的Ordering tasksFinalizer tasks,他们回答问题2:mustRunAfter仅在运行两个任务时生效。另一方面,finalizedBy仅在运行它所设置的任务时生效。这解释了我执行flywayClean时运行removeTestDatabasContainergradle flywayMigrate的原因。

我仍然在努力让gradle testEverything像上面一样工作,同时让gradle flywayMigrate只执行flywayMigrate

1 个答案:

答案 0 :(得分:1)

感谢freenode上#gradle上eskatos的帮助,我找到了解决方案。这只是删除我有的最终版本。更新了有效的build.gradle:

task removeTestDatabaseContainer {
  doLast {
    println '\ninside removeTestDatabaseContainer\n'
  }
}

task startTestDatabaseContainer {
  doLast {
    println '\ninside startTestDatabaseContainer\n'
  }
  finalizedBy removeTestDatabaseContainer
}

task flywayMigrate { t->
  doLast {
    println '\n inside flywayMigrate\n'
  }
}

task flywayClean { t->
  doLast {
    println '\n inside flywayClean\n'
  }
}

task testEverything {
    dependsOn startTestDatabaseContainer
    dependsOn flywayMigrate
    dependsOn flywayClean

    flywayMigrate.mustRunAfter startTestDatabaseContainer
    //flywayMigrate.finalizedBy flywayClean

    flywayClean.mustRunAfter flywayMigrate
    //flywayClean.finalizedBy removeTestDatabaseContainer
}