简体   繁体   中英

SonarQube not showing test Jacoco coverage for JUnit tests in a Gradle multi-project

I would like to show the test coverage of a multiple project Spring boot application build with Gradle 6.0. We currently use JUnit5.

The test coverage shows 0% in SonarQube even though a few first tests exists.

The build.gradle files in the top level project ( https://github.com/OpenReqEU/eclipse-plugin-vogella/blob/master/server/build.gradle ) has the following input:

plugins {
    id "org.sonarqube" version "2.7"
    id 'jacoco'
}

repositories {
    jcenter()
}

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

    repositories {
        jcenter()
        maven { url 'https://repo.spring.io/snapshot' }
        maven { url 'https://repo.spring.io/milestone' }
        maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' }
            maven { url 'http://oss.jfrog.org/artifactory/oss-snapshot-local/' }
    }

    jacocoTestReport {
        reports {
            xml.enabled true
            xml.destination file("${buildDir}/coverage-reports/coverage.xml")
            //html.destination file("${buildDir}/coverage-reports")
        }
    }

    ext {
        springBootVersion = '2.1.1.RELEASE'
    }

    sourceCompatibility = 1.8

}


wrapper {
    gradleVersion = '6.0'
}

In the Jenkins build we set the following parameters:

sonar.projectKey=eclipse-plugin-vogella
sonar.sources=server/com.vogella.prioritizer.server/src/main,server/com.vogella.prioritizer.server.bugzilla/src/main,server/com.vogella.prioritizer.server.issue.api/src/main
sonar.java.binaries=com.vogella.prioritizer.server/build/classes/java/main,com.vogella.prioritizer.server.bugzilla/build/classes/java/main,com.vogella.prioritizer.server.issue.api/build/classes/java/main
sonar.tests=server/com.vogella.prioritizer.server/src/test,server/com.vogella.prioritizer.server.bugzilla/src/test
sonar.coverage.jacoco.xmlReportsPath=server/com.vogella.prioritizer.server.bugzilla/build/jacoco/test.exec,server/com.vogella.prioritizer.server/build/jacoco/test.exec,server/com.vogella.prioritizer.server.issue.api/build/jacoco/test.exec

The result of the build shows an error: INFO: parsing [/home/jenkins/workspace/issue-prioritizer/coverage-reports/coverage.xml] ERROR: Reports path not found or is not a directory: /home/jenkins/workspace/issue-prioritizer/coverage-reports/coverage.xml

I see that each project has a generated ${buildDir}/coverage-reports/coverage.xml file but the root file is empty, which is expected as I did not configure anything related to this.

At some point I added a copy task which copied one of the generated xml files from one project into the root folder but the build job complained that the classes were not matching.

Does anybody know how this issue can be solved? I assume I must add a configuration to add a root coverage.xml file which is the aggregate of the individual ones but I have not found a solution for that.

I also tried to apply the jacoco to the root project but that also failed as the root project is not a Java project.

Need to generate report in xml format. Add the sonar property to the xml path as below.

jacocoTestReport {
    reports {
        xml.enabled true
    }
}

sonarqube {
        properties {
            property "sonar.java.source", "1.8"
            property "sonar.java.coveragePlugin", "jacoco"
            property "sonar.jacoco.reportPaths", "build/reports/jacoco/test/jacocoTestReport.xml"
        }
}

Run the gradle command with the jacocoTestReport

gradlew sonarqube jacocoTestReport

I managed to create the aggregated coverage.xml file by changing the top level build.gradle to:

plugins {
//    id "org.sonarqube" version "2.7"
    id 'jacoco'
}

repositories {
    jcenter()
}


subprojects {

    println name;
    apply plugin: 'jacoco'
    apply plugin: 'java'
    apply plugin: 'eclipse'

    repositories {
        jcenter()
        maven { url 'https://repo.spring.io/snapshot' }
        maven { url 'https://repo.spring.io/milestone' }
        maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' }
        maven { url 'http://oss.jfrog.org/artifactory/oss-snapshot-local/' }
    }

    jacocoTestReport {   
        reports {
            xml.enabled true
        }
    }

    ext {
        springBootVersion = '2.1.1.RELEASE'
    }

    sourceCompatibility = 1.8
}

// run the build with ./gradlew clean build generateMergedReport
task generateMergedReport(type: JacocoReport) {
    dependsOn = subprojects.test 

    additionalSourceDirs.setFrom files(subprojects.sourceSets.main.allSource.srcDirs)
    sourceDirectories.setFrom files(subprojects.sourceSets.main.allSource.srcDirs)
    classDirectories.setFrom files(subprojects.sourceSets.main.output)

    executionData.setFrom project.fileTree(dir: '.', include: '**/build/jacoco/test.exec')
    println executionData;

    reports {
        xml.enabled true
        xml.destination file("../coverage-reports/coverage.xml")
    }
}

wrapper {
    gradleVersion = '6.0'
}

And changing the Jenkins to build generateMergedReport:

cd server && ./gradlew build generateMergedReport

The SonarQube properties where changed to:

sonar.projectKey=eclipse-plugin-vogella
sonar.sources=server/com.vogella.prioritizer.server/src/main,server/com.vogella.prioritizer.server.bugzilla/src/main,server/com.vogella.prioritizer.server.issue.api/src/main
sonar.java.binaries=com.vogella.prioritizer.server/build/classes/java/main,com.vogella.prioritizer.server.bugzilla/build/classes/java/main,com.vogella.prioritizer.server.issue.api/build/classes/java/main

Unfortunately SonarQube still doesnt find the coverage.xml file.

INFO: parsing [/home/jenkins/workspace/issue-prioritizer/coverage-reports/coverage.xml]
ERROR: Reports path not found or is not a directory: /home/jenkins/workspace/issue-prioritizer/coverage-reports/coverage.xml

Does someone have an idea of what is missing?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM