我正在尝试让Gradle的mustRunAfter
和finalizedBy
仅用于特定任务。举个例子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 tasks和Finalizer tasks,他们回答问题2:mustRunAfter
仅在运行两个任务时生效。另一方面,finalizedBy
仅在运行它所设置的任务时生效。这解释了我执行flywayClean
时运行removeTestDatabasContainer
和gradle flywayMigrate
的原因。
我仍然在努力让gradle testEverything
像上面一样工作,同时让gradle flywayMigrate
只执行flywayMigrate
。
答案 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
}