user447607
user447607

Reputation: 5469

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.

Upvotes: 3

Views: 4680

Answers (2)

anemonetea
anemonetea

Reputation: 61

It has been a few years since the Accepted answer was posted. This question has become relevant once again since SonarQube removed its automatic dependency on the test task.

For SonarQube plugin version 3.4.0.2513 and Gradle versison 6.9.1, this is what worked for me:

def LinkedHashSet jacocoSet = new LinkedHashSet()
subprojects {
    jacocoSet.addAll(
        tasks.findAll({ it.name.contains('jacocoTestReport') || it.name == 'test' })
    )
    // println tasks.findAll {it.name.contains('sonar')}  //returns: []
}
rootProject.tasks.sonarqube.dependsOn = jacocoSet

Differences from the Accepted answer:

  • sonarRunner was not a valid task, neither in the rootProject nor in the subprojects.
  • project.subprojects is a list of subprojects, not tasks. So, you need to access the subprojects' tasks list before you can run the task filter so that you can add a task object (instead of a project object) to dependsOn.

Upvotes: 4

Vampire
Vampire

Reputation: 38639

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()
   }
}

Upvotes: 1

Related Questions