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






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

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
        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/${}.exec").exists()) {
    reports.xml.enabled = true
rootProject.getTasksByName('test', true).each {

This line

if (file("${subproject.buildDir}/jacoco/${}.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.

    id 'java'
    id 'jacoco'
  test.finalizedBy jacocoTestReport

Answered by dexter2305 on January 1, 2022

Add your own answers!

Related Questions

Jquery promises in loop

1  Asked on December 9, 2021 by alan-a


Vertical align on left Boostrap

1  Asked on December 9, 2021 by obsesie


How to get flags of opened fd in C?

2  Asked on December 9, 2021 by code_worker


Make list evenly spaced and inline

4  Asked on December 9, 2021 by nobert


ASP Core 3.1 API with EF Core 5.0

1  Asked on December 9, 2021 by bibout182


Using set in c++ standard template library (STL)

1  Asked on December 9, 2021 by tushar-jain


Element goes out of the element in JS

1  Asked on December 9, 2021


How to set scrollbars to only part of the modal?

2  Asked on December 9, 2021 by gowthamss


C#, Linq, Filter a list whether its properties appear on another list

2  Asked on December 9, 2021 by hiedeptrai


Return all filename from database by specific field name

4  Asked on December 9, 2021 by maqsud-inamdar


Fixed header logic

2  Asked on December 9, 2021 by j00m


Ask a Question

Get help from others!

© 2023 All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir