I am trying to get Gradle mustRunAfter and finalizedBy to work for a specific task only. Take this build.gradle example:
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 }
I am pleased with how testEverything works. I need the result that I get from this task:
➜ 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
However, when I only run flywayMigrate , I get unexpected problems. This is the result:
➜ gradle flywayMigrate Parallel execution is an incubating feature. :flywayMigrate inside flywayMigrate :flywayClean inside flywayClean :removeTestDatabaseContainer inside removeTestDatabaseContainer BUILD SUCCESSFUL Total time: 0.605 secs
This is not the result that I want. I would like to launch only flywayMigrate . Question 1) How to make testEverything work as it is, and at the same time have a gradle flywayMigrate only call flywayMigrate -task?
Question 2)
I was told that this is because everything inside the brackets of the task testEverything {} is a configuration that is always handled by Gradle. Thus, any mustRunAfter / finalizedBy set inside the task will have a "global effect." But in this case, why does gradle flywayMigrate call startTestDatabaseContainer ? (Due to the line flywayMigrate.mustRunAfter startTestDatabaseContainer inside the testEverything task.)
Edit: The Task Order and Finalizer Tasks were sent to me in the Gradle documentation, and they answer question 2: mustRunAfter takes effect only when both tasks are launched. finalizedBy , on the other hand, takes effect when only a given task is running. This answers why flywayClean and removeTestDatabasContainer are triggered when gradle flywayMigrate .
I'm still trying to make gradle testEverything work as it is above, and at the same time get gradle flywayMigrate just execute flywayMigrate .