TransWikia.com

How do I get a Jacoco coverage report using gradle plugin when all my tests are in a separate submodule

Stack Overflow Asked by MysticForce on January 1, 2022

I’m having trouble setting up jacoco coverage for my java project since I’m new to gradle. My final goal is to connect this to sonarqube. All my tests are in a separate module

structure:

./build.gradle
settings.gradle

./submodule1/build.gradle
./submodule1/src/main/java/prismoskills/Foo.java

./submodule2/build.gradle
./submodule2/src/main/java/com/project/prismoskills/Bar.java

./test/build.gradle
./test/src/test/java/prismoskills/TestFooBar.java

One way I can think of is to set additionalSourceDirs in test module and enable jacoco only in root and test module.

The problem with this approach is that my project has a lot of sub modules(which I haven’t shown here) and I am having trouble passing additionalsourcedirs to test module’s JacocoReport task in an automated way.

Also it looks like this use case can be handled in maven easily by referring to this
https://prismoskills.appspot.com/lessons/Maven/Chapter_06_-_Jacoco_report_aggregation.jsp

Any leads on how to proceed further with gradle will be appreciated. Thanks in advance

gradle version: 6.4
jacoco gradle plugin version: 0.8.5

2 Answers

I think the following solution should solve your problem. The idea is that:

  1. JaCoCo exec file is generated for every project
  2. at the end one XML report with all data is generated

It does the same for JUnit reports because it is easier to see all tests reports together in the root project instead of navigating between directories.

plugins {
    id 'base'
    id 'org.sonarqube' version '3.0'
}

allprojects {
    apply plugin: 'jacoco'
    apply plugin: 'project-report'

    // ...
    
    jacoco {
        toolVersion = 0.8.5
    }
}

subprojects {
    // ...

    test {
        reports.html.enabled = false
        useJunitPlatform()
        finalizedBy jacocoTestReport
    }

    jacocoTestReport {
        dependsOn test
        reports.html.enabled = false
    }
}

// ...

task testReport(type: TestReport) {
    destinationDir = file("${buildDir}/reports/test")
    reportOn subprojects*.test
}
task jacocoTestReport(type: JacocoReport) {
    subprojects { subproject ->
        subproject.tasks.findAll { it.extensions.findByType(JacocoTaskExtension) }.each { extendedTask ->
            configure {
                sourceSets subproject.sourceSets.main
                if (file("${subproject.buildDir}/jacoco/${extendedTask.name}.exec").exists()) {
                    executionData(extendedTask)
                }
            }
        }
    }
    reports.xml.enabled = true
}
rootProject.getTasksByName('test', true).each {
    it.finalizedBy(testReport)
    it.finalizedBy(jacocoTestReport)
}

This line

if (file("${subproject.buildDir}/jacoco/${extendedTask.name}.exec").exists()) {

is added to prevent build failures when some subprojects don't have tests at all.

Answered by agabrys on January 1, 2022

Following can be defined in the build.gradle of root project. Jacoco plugin will be applied to all the submodules.

subprojects{
  plugins{
    id 'java'
    id 'jacoco'
  }
  test.finalizedBy jacocoTestReport
}

Answered by dexter2305 on January 1, 2022

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP