FinalizedBy doesn't work with SpotBugsTask(s)

Hello everyone. I am trying to generate a zipped spotbugs report. I managed to get something similar working for Unit-Test reports and JaCoCo reports.

This works for test reports:

task generateTestReport(dependsOn: test, type: Zip) {
    from file("${buildDir}/reports/tests/test")
    archiveName "${project.name}-TestReport.zip"
    destinationDir rootProject.projectDir

    test.finalizedBy generateTestReport
}

However this doesn’t work for Spotbugs reports. By that I mean, when any of the Spotbugs tasks fails (aka when issues are found and the report is useful) the spotbugs task and the generateSpotbugsReport task aren’t run.

task spotbugs(dependsOn: tasks.withType(SpotBugsTask)) {
    group "verification"
    description "Run SpotBugs analysis for all classes"

    check.dependsOn tasks.spotbugs
}

task generateSpotbugsReport(dependsOn: tasks.spotbugs, type: Zip) {
    from file("${buildDir}/reports/spotbugs")
    archiveName "${project.name}-SpotbugsReport.zip"
    destinationDir rootProject.projectDir

    tasks.spotbugs.finalizedBy generateSpotbugsReport
}

tasks.withType(SpotBugsTask) {
    group "verification"

    reports {
        xml.enabled false
        html.enabled true
    }

    finalizedBy tasks.spotbugs
}

It appears like that the finalizedBy statement inside the tasks.withType(SpotBugsTask)-block gets ignored entirely!

This looks to be working as designed. The dependsOn is a stronger statement than the finalizedBy statement with regards to task dependencies. The finalizer task will be added to the task graph and would run after the finalized task, except that even that task won’t run if its task dependencies have failed.

Your spotbugs task should likely depend on all tasks of the SpotBugsTask type, and all of those tasks should be finalizedBy your generateSpotbugsReport task. You would not directly run your report task. Running the actual task or a lifecycle task that depends on it would cause the report zip to be created with anything available.

As far as your test reports example, as posted, it also fails to create the report when tests have failed. I expect this code is either an approximation or something is misleading you to think it’s working when it is not.

Ok. That did solve it. Thank you!