Workaround for GRADLE-1813 - SonarQube plugin

I wanted to add a comment to https://issues.gradle.org/browse/GRADLE-1813
but I could not find a way to do so, so I share some useful info here. Please add this as a comment to GRADLE-1813 as it may be useful for others hitting the same issue.

In a multiproject gradle build, one can either:

  • apply the sonarqube plugin to root project and get a single report for all subprojects
  • apply the sonarqube plugin to subprojects and get a separate report for each subproject

It is preferred to apply the sonarqube plugin to the root project and get a single aggregated report from continuous integration runs. On the other hand, for development, it is useful to run the analysis on a single subproject and get the targeted results.

The solution (workaround) for this is to apply the sonar plugin to the root project and add the following snippet to the root build.gradle:

if (project.hasProperty("sonarProject")) {
  configure(allprojects) {
    if (it != rootProject && it.name != sonarProject) {
      it.sonarqube.skipProject = true
    }
  }
}

To analyze a single project named my-project, one can run:
gradlew -i -PsonarProject=my-project sonarqube

The -i (--info) option instructs the task to print out the sonarqube URL where the results can be found after the task completes.