Gradle's mustRunAfter/finalizedBy only for a specific task? - gradle

I'm trying to get Gradle's mustRunAfter and finalizedBy to work only for a specific task. Take this example 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
}
I'm happy with how testEverything works. I want the output I'm getting from that 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 run only flywayMigrate I get unexpected problems. This is the output:
➜ 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 output I want. I would like only flywayMigrate to run. Question 1) How can I make testEverything work as it does and at the same time have gradle flywayMigrate invoke only the flywayMigrate-task?
Question 2)
I'm told this has something to do with the fact that everything inside the brackets of task testEverything {} is configuration, which is always processed by Gradle. So any mustRunAfter/finalizedBy I set within a task will have "global effect". But in that case, why doesn't gradle flywayMigrate invoke startTestDatabaseContainer? (Because of the line flywayMigrate.mustRunAfter startTestDatabaseContainer inside the testEverything task.)
Edit: I was directed to Ordering tasks and Finalizer tasks in the Gradle documentation and they answer question 2: mustRunAfter only takes effect when both tasks are ran. finalizedBy on the other hand takes effect when only the task it is set on is ran. That answers why flywayClean and removeTestDatabasContainer are ran when I execute gradle flywayMigrate.
I'm still struggling to make gradle testEverything work as it does above and at the same time get gradle flywayMigrate to just execute flywayMigrate.

Thanks to the help of eskatos on #gradle on Freenode I found a solution. It was simply to remove the finalizedBy-lines i had. Updated build.gradle that works:
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
}

Related

How to run certain task with gradle

I try to investigate a Gradle and follows some tutorials, but I have confused with the following:
I created are a couple of simple tasks:
task startProcess{
println 'startProcess'
}
task doStep2{
println 'Step2'
}
task doStep3{
println 'Step3'
}
task finishProcess{
println 'finishProcesss'
}
And try to execute one of them:
gradle finishProcess
Or with defaultTasks with command gradle build:
defaultTasks `finishProcess`
task startProcess{
println 'startProcess'
}
task doStep2{
println 'Step2'
}
task doStep3{
println 'Step3'
}
task finishProcess{
println 'finishProcesss'
}
In both options, I got the same result:
> Configure project :
startProcess
Step2
Step3
finishProcesss
BUILD SUCCESSFUL in 1s
How to execute exactly one of them?
You have to use register, I think if you did not use it, You're only asking Gradle to execute these tasks.
for example
tasks.register('startProcess') {
doLast {
println 'startProcess'
}
}
tasks.register('doStep2') {
doLast {
println 'Step2'
}
}
tasks.register('doStep3') {
doLast {
println 'Step3'
}
}
tasks.register('finishProcess') {
doLast {
println 'finishProcesss'
}
}
tasks.named("build") { finalizedBy("finishProcess") }
Registering these tasks, you will be able to call each one indivadually.
If you want to link a specific task, with a build task for example.
Then you can use finalizedBy like the following.
tasks.named("build") { finalizedBy("finishProcess") }
This will call the finishProcess task, whenever build is triggered.
I strongly recommend the official gradle documintation for more information about tasks.

Gradle disable task

Can I disable the task, only for one task?
For example
flywayMigrate {
doFirst {
gradle.startParameter.excludedTaskNames += "test"
// test.enabled = false
}
dependsOn flywayClean
dependsOn build
}
I want you to do flywayMigrate
The tests were turned off.
But when I run clean build tests were run also.
I think you can do it with the task graph of the build as follows:
gradle.taskGraph.whenReady { taskGraph ->
if (taskGraph.hasTask(flywayMigrate)) {
test.enabled = false
}
}
Here is a closure executed then the task graoh is built, just before task are configured or executed. It check's whether flywayMigrate task will be executed and if yes, it disables test task.
Here is how it's described in the official docs.

Ordering depended tasks for a gradle task

I have a gradle task which has dependencies on the subproject task. For example.
task bigTask(type: JacocoReport, dependsOn: [":module1:task",
":module2:task",
":module3:task",
":module4:task",
":module5:task"]) {
....
.....
}
I want to order the depended tasks in a specific way. So that at a time only one task get exeuted. I did like this in the root project. But it is not working.
task (":module1:task") { mustRunAfter (":module2:task") }
task (":module2:task") { mustRunAfter (":module3:task") }
task (":module3:task") { mustRunAfter (":module4:task") }
task (":module4:task") { mustRunAfter (":module5:task") }
Is there some way to order the dependent task in gradle.

How should I structure these gradle task dependencies?

I have these gradle tasks:
- startTestDatabaseContainer: builds and starts a docker container with a database
- removeTestDatabaseContainer: stops and removes the docker container
- flywayValidate: a task from org.flywaydb.flyway that validates my migration files
I wish to run these three tasks in order. Reading this leads me to this solution:
flywayValidate.dependsOn startTestDatabaseContainer
flywayValidate.finalizedBy removeTestDatabaseContainer
This works ok, but then I can't run gradle flywayValidate from the commandline without startTestDatabaseContainer and removeTestDatabaseContainer also being invoked. I want to be able to run flywayValidate without that happening.
What can I do to accomplish this when I cannot have ordered dependencies in gradle?
My first attempt was simply:
task validateMigration {
dependsOn startTestDatabaseContainer
dependsOn flywayValidate
finalizedBy removeTestDatabaseContainer
}
But that fails because flywayValidate can run before startTestDatabaseContainer.
Edit: I've setup a demonstration base on Opal's solution here: github.com/stianlagstad/flyway-migration-error-causes-final-gradle-task-to-not-execute. Clone it and run gradle validateMigration. The migration will fail and the final gradle task won't run (and docker ps will show the container still running). If you fix the migration file then everything works as expected. I'm sure I'm misunderstanding something. Any pointers would be helpful!
The following setup should meet all your requirements:
task startTestDatabaseContainer {
doLast {
println 'startTestDatabaseContainer'
}
}
task flywayValidate {
doLast {
println 'flywayValidate'
}
}
task removeTestDatabaseContainer {
doLast {
println 'removeTestDatabaseContainer'
}
}
task validateMigration {
dependsOn startTestDatabaseContainer
dependsOn flywayValidate
flywayValidate.mustRunAfter startTestDatabaseContainer
finalizedBy removeTestDatabaseContainer
}
EDIT
task removeTestDatabaseContainer {
doLast {
println 'removeTestDatabaseContainer'
}
}
task startTestDatabaseContainer {
doLast {
println 'startTestDatabaseContainer'
}
finalizedBy removeTestDatabaseContainer
}
task flywayValidate { t->
doLast {
println 'flywayValidate'
throw new TaskExecutionException(t, new RuntimeException('whatever'))
}
}
task validateMigration {
dependsOn startTestDatabaseContainer
dependsOn flywayValidate
flywayValidate.mustRunAfter startTestDatabaseContainer
}
Here's a demo.
task validateMigration {
dependsOn startTestDatabaseContainer
dependsOn flywayMigrate
flywayMigrate.finalizedBy removeTestDatabaseContainer
flywayMigrate.mustRunAfter startTestDatabaseContainer
}
This did the trick! Thank you orzeh for the PR on Github, and thank you Opal for the help!

gradle - ignore dependsOn task execution

If a condition is not met, I am trying to stop execution of a task using onlyIf(). Is there a way I can stop the task dependencies too from executing? Seems onlyIf does not stop dependencies from execution.
In the example below, I desire taskA not executed when I pass executeMe parameter as false.
build.gradle
task taskA() {
doFirst {
println 'executing taskA'
}
}
task taskB(dependsOn: 'taskA') {
onlyIf {
executeMe.toBoolean()
}
doFirst {
println 'executing taskB'
}
}
Run output:
>gradle taskB -PexecuteMe=false
10:39:36 AM: Executing external task 'taskB -PexecuteMe=false'...
:taskA
executing taskA
:taskB SKIPPED
One way you can achieve this is by adding "onlyIf" to both tasks at the same time using the following:
task taskA() {
doFirst {
println 'executing taskA'
}
}
task taskB(dependsOn: 'taskA') {
doFirst {
println 'executing taskB'
}
}
[taskA, taskB].each { task ->
task.onlyIf {
executeMe.toBoolean()
}
}
Output:
$ gradle taskB -PexecuteMe=false
:taskA SKIPPED
:taskB SKIPPED
BUILD SUCCESSFUL
Total time: 2.122 secs

Resources