How to ignore dependsOn failure because some subprojects don't have the defined task - gradle

I have a multi-project gradle build where not all of the subprojects have the same plugin, but I would like to define tasks in the root build.gradle file like this:
subprojects {
task continuousBuild(dependsOn: ["clean", "check", "jacocoTestReport", "integrationTests"]
}
Not all subprojects have "jacocoTestReport" or "integrationTests" defined, but this task will fail because of that fact. How do I configure this to work, and frankly, why is the default behavior so strict?

This is what ended up working for me:
task continuousBuild(dependsOn: ['clean', 'check']) {
def uncommonTasks = ['jacocoTestReport', 'integrationTests']
dependsOn += tasks
.findAll { uncommonTasks.contains(name) }
}
And I forgot that I actually needed to run integrationTests in a doLast, which looks like this:
task continuousBuild(dependsOn: ['clean', 'check']) {
dependsOn += tasks
.findAll { 'jacocoTestReport' == name) }
if (tasks.findByName('integrationTests')) {
doLast {
integrationTests
}
}
}

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.

Is there a way to configure multiple gradle tasks at once?

I have the following snippet in a build.gradle file for a project I work on:
tasks.named('test').configure {
dependsOn customTask
}
tasks.named('compileTestJava').configure {
dependsOn customTask
}
tasks.named('jar').configure {
dependsOn customTask
}
tasks.named('build').configure {
dependsOn customTask
}
It seems redundant to have to specify the the same configuration for 4 tasks separately. Is there a way to select and configure multiple tasks at once? Perhaps something like:
tasks.named('test', 'compileTestJava', 'jar', 'build').configure {
dependsOn customTask
}
(I tried this code, but it does not work.)
You can do
['a', 'b', 'c'].each {
tasks.named(it).configure {
dependsOn 'customTask'
}
}

How do I get my custom gradle tasks to run?

I have a gradle project from which I want to generate two artifacts - a java jar, and a tarball of additional files.
So I added the following to my build.gradle
def serviceName = "kafka-schemas"
task packageDistribution(type: Copy) {
from "$buildDir/resources/main"
include "*.avsc"
into "$buildDir/schemas"
}
task archive(type: Tar) {
dependsOn 'packageDistribution'
compression = 'GZIP'
from("$buildDir/schemas") {
include "**/*.avsc"
}
into("${serviceName}")
}
project.tasks.findByName('build') dependsOn archive
project.tasks.findByName('build') dependsOn packageDistribution
However when I run gradle clean build it does not run my tasks.
What am I doing wrong?
Try this:
processResources.dependsOn('packageDistribution')
task packageDistribution(type: Zip) {
archiveFileName = "xxxxx.zip"
destinationDirectory = file("$buildDir/dist")
from "xxxx"
}

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!

Resources