Search code examples
gradlesonarqubejacoco

How do I get sonarqube to depend on jacocoTestReport?


Why doesn't this work and how can I get the result I'm looking for? Specifically, I want jacoco to run before sonarqube.

subprojects {
    apply plugin: 'java'
    apply plugin: 'idea'
    apply plugin: 'eclipse'
    apply plugin: 'jacoco'

    test {
        jacoco {
            excludes = [...,
                         "javolution.*"]
        }
    }

    jacocoTestReport {
        dependsOn tasks.withType(Test)//This makes integrationTests go. 
    }

//This is the part that I can't get to work:
project.tasks["sonarqube"].dependsOn jacocoTestReport
}

Error is:

* Where:
Build file '/dev/abc/build.gradle' line: 92

* What went wrong:
A problem occurred evaluating root project 'abc'.
> Task with name 'sonarqube' not found in project ':thingamajig'.

Of course thingamajig is an empty parent directory. No build.gradle there but it has many sub-directories that do have a build.gradle. I've tried a number of things like checking the task graph and catching an swallowing the Exception when calling project.tasks.getByName() Time to punt, I guess.


Solution

  • The subprojects do not have an own sonar runner task. Make the root projects sonar runner task depend on the subprojects jacocoTestReport tasks.

    In my build.gradle this looks like

    if (hasProperty('sonarAnalyseOnly') && sonarAnalyseOnly.toBoolean()) {
       tasks.sonarRunner.dependsOn = []
    } else {
       tasks.sonarRunner.dependsOn {
          project.subprojects.findAll { !it.sonarRunner.skipProject }.collect {
             [
                   it.tasks.compileSharedJava,
                   it.tasks.compileServerJava,
                   it.tasks.compileGuiJava,
                   it.tasks.integTest,
                   it.tasks.jacocoMerge
             ]
          }.flatten()
       }
    }