Analyse test code quality in separate Sonar analysis


(thomas.wright) #1

We currently use the Gradle Sonar Runner plugin for analysing our Java multi-project source code. I would like to add a completely independent Sonar analysis covering just our unit test code (which by default is excluded from analysis with the main build), as I would like to use a different Sonar quality profile for the test code analysis.

I thought this should be a straightforward case of changing the source sets that are passed to Sonar, as specified in Section 36.5 of the Gradle documentation, but no matter how I set things up, the analysis seems only to cover the main product code. My current (broken) solution consists of the following additions to my master build.gradle file:

sonarRunner {
   sonarProperties {
      // Standard Sonar configuration...
      ...
        // Remove any existing source sets
      property "sonar.sources", new HashSet()
      property "sonar.tests", new HashSet()
   }
      }
     subprojects {
   sonarRunner {
      sonarProperties {
         // Now add test source sets for each subproject to main sonar.sources
         sourceSets.test.allSource.srcDirs.each { srcDir ->
            def dirsThatExist = new HashSet< File >()
            if ( srcDir.exists() ) {
               dirsThatExist.add( srcDir )
            }
              properties["sonar.sources"] += dirsThatExist
                        }
      }
   }
}

The Gradle build runs, and results are pushed to Sonar. But those results stubbornly refuse to differ from the standard analysis of the production sources. What am I doing wrong?

Tom


(Peter Niederwieser) #2

You only removed the default source directories for the root project (which probably doesn’t have sources anyway), but not for any subproject. Try something like:

subprojects {
    sonarRunner {
        sonarProperties {
            properties["sonar.sources"] = sourceSets.test.java.srcDirs.findAll { it.exists() }
            properties["sonar.test"] = []
        }
    }
       }

It’s not clear to me how you intend to declare two separate analysis runs without using the ‘SonarRunner’ task directly, but that’s a separate topic. Note that you’ll somehow have to distinguish the runs, perhaps by using different values for ‘sonar.projectKey’ or ‘sonar.branch’.


(thomas.wright) #3

Ah yes, of course. Thank you.

My plan for scheduling the two separate runs was pretty ugly…to use a command-line gradle property to conditinally set different propeties in the sonaRunner task. Any ideas for neater solutions are welcome!

Thank you for your reply,

Tom