3
votes

I've started with the 'java-gradle-simple' example from the Sonar GitHub repo:

https://github.com/SonarSource/sonar-examples/tree/master/projects/languages/java/gradle/java-gradle-simple

I've added the jacoco plugin to my build.gradle file in an empty to see unit test coverage results, and I'm seeing the warning:

Coverage information was not collected. Perhaps you forget to include debug information into compiled classes?

I'm currently running version 4.3.2 of Sonar, and this is what I'm seeing after running 'gradle test jacocoTestReport sonarRunner':

Sonar report

My build.gradle file looks like this:

apply plugin: 'java'
apply plugin: 'sonar-runner'
apply plugin: 'jacoco'

allprojects {  
  ext.baseVersion = "0.1"
  ext.snapshotVersion = true

  group = "org.sonar.tests"
  version = "$baseVersion" + (snapshotVersion ? "-SNAPSHOT" : "")
}

sonarRunner {
    sonarProperties {
        property "sonar.projectName", "Simple Java Gradle Project"
        property "sonar.projectKey", "org.codehaus.sonar:example-java-gradle"
        property "sonar.jacoco.reportPath", "${project.buildDir}/jacoco/test.exec"
        property "sonar.binaries", "${project.buildDir}/classes"
        property "sonar.sources", "src/main"
        property "sonar.tests", "src/test"
        property "sonar.java.coveragePlugin", "jacoco"
    }
}

buildscript {
    repositories { mavenCentral() }
    dependencies { classpath 'org.ajoberstar:gradle-jacoco:0.1.0' }
}

test {
  ignoreFailures = true
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.10'
}

repositories {
    mavenCentral()
}

Is there some other configuration that I'm missing? Surprisingly, there doesn't seem to be much information on generating unit test coverage with Sonar using Gradle.

1
What jdk version are using? Jdk8 requires a newer version of jacoco than the one provided in gradle. - Stav Saad
@StavSaad I was indeed using JDK 8. After switching to JDK7, I'm able to see unit test coverage in my local Sonar instance. Is it possible to set the jacoco version that gradle is using? - chazzlabs

1 Answers

1
votes

Since you are using jdk 8, you should add the following block to your gradle script:

jacoco {
    toolVersion = "0.7.0.201403182114"
}

Also, you should make sure your sonar version supports jdk8