Jacoco with Gradle custom test sourceSets not generating coverage report

I am new to gradle and following is the existing gradle Project structure:

build.gradle.kts:

plugins {
    java
    jacoco
}
sourceSets {
    main {
        java {
           //set to some non existing folders to avoid compiling root project
            setSrcDirs(listOf("src/java"))
        }
        resources {
            setSrcDirs(listOf("src/resources"))
        }
    }
    test {
        java {
            srcDirs("test")
        }
        resources{
            srcDir("test/resources")
        }
    }
}

tasks.test {
    minHeapSize = "1G"
    maxHeapSize = "3G"
    finalizedBy (tasks.jacocoTestReport )
}

jacoco{
    toolVersion = "0.8.12"
}

tasks.jacocoTestReport{
    dependsOn(tasks.test)
    reports{
        xml.required.set(true)
        html.required.set(true)
    }

    classDirectories.setFrom(
        fileTree("${project.projectDir}/src"){
            include("**/com/healthcare/sv/**")
        }
    )
    sourceDirectories.setFrom(files("${project.projectDir}/src"))
    executionData.setFrom(files("${buildDir}/jacoco/test.exec"))

}

Problem Statement:

Jacoco coverage report is not getting generated when I run the command:
./grdalew test jacocoTestReport

No error logs related to jacoco, test.exec is generated at β€œ\build\jacoco\test.exec”

P.S. It’s not possible to restructure the packages.

Please never anywhere share screenshots of text if you only want to share the text and not something additional like colors or IDE annotations. Text in images is hard to read, especially on mobile, very hard to copy, and nearly impossible to search for.

Besides that, your image and your text seem to be inconsistent, or that is the source of your problems.
In the image you say something about build.xml?!?
Also in the image you say the source is at src/... but then you say you configure your build to look in src/java for the sources!?!

Your main problem probabyl is, that you configure the classDirectories to be files in src, which seems to be wrong on multiple layers. I think you should be able to remove all the jacoco configuration below the reports block. As you configure the main source set with your path structure and that JaCoCo task should be configured to work with the main source set, it should just work.

Ok, I’ll avoid posting screenshots again:

root-project/
β”‚
β”œβ”€β”€ modules/
β”‚   └── moduleA/
β”‚       └──src/
β”‚          └──main/
β”‚      └── java/   
β”‚                 └──com/example/ClassA.java
β”‚       └──build.gradle.kts
β”‚   └── moduleB/
β”‚       └──src/
β”‚          └──main/
β”‚             └── java/   
β”‚                 └──com/example/ClassB.java
β”‚       └──build.gradle.kts
β”‚   └── moduleC/
β”‚       └──src/
β”‚          └──main/
β”‚             └── java/   
β”‚                 └──com/example/ClassC.java 
β”‚       └──build.gradle.kts
β”‚
β”‚
β”œβ”€β”€ src/
β”‚   └── com/
β”‚       └── healthcare/
β”‚           └── sv/
β”‚               └── api/
β”‚                   └── Provider.java
β”‚               └── crypto/
β”‚                   └── Relation.java
β”‚               └── domain/
β”‚                   └── Person.java
β”‚               └── json/
β”‚                   └── JsonServlet.java
β”œβ”€β”€ test/
β”‚   └── com/
β”‚       └── healthcare/
β”‚           └── sv/
β”‚               └── api/
β”‚                   └── ProviderTest.java
β”‚               └── crypto/
β”‚                   └── RelationTest.java
β”‚               └── domain/
β”‚                   └── PersonTest.java
β”‚               └── json/
β”‚                   └── JsonServletTest.java
β”‚
└── build.gradle.kts
β”‚
└── build.xml

src/ is a folder in the root directory, which has .java files, for these java files the .class files are being generated in one of the modules: moduleA/build/classes folder (this is done using build.xml)

but then you say you configure your build to look in src/java for the sources!?!
β†’ as you can see the comment : //set to some non existing folders to avoid compiling root project (I know this not right, but can’t be changed at this point of time)

Here the java files in src/com/healthcare package are being compiled and .class files are generated in moduleA’s modules/moduleA/build/classes/java/main location, this is achieved using build.xml…

Tests for classes in src/com/healthcare are written in root directory test folder.

I am able to add the test folder as custom source set in root build.gradle.kts, but not able to get jacoco coverage, when I am adding jacoco config in root build.gradle files. its generating rootDirectory/build/jacoco/test.exe, but no report.

modules/moduleA/build.gradle.kts also has jacoco plugin, which is generating report for .class files of root src/com/healthcare package but its coming as zero coverage.

Here’s is my jacoco config in root build.gradle.kts:

tasks.test {
    minHeapSize = "1G"
    maxHeapSize = "3G"
    finalizedBy (tasks.jacocoTestReport)
}


tasks.jacocoTestReport{
    dependsOn(tasks.test)
    reports{
        xml.required.set(true)
        html.required.set(true)
    }
    classDirectories.setFrom(
        fileTree("modules/moduleA/build/classes/java/main").include("**/*.class")
    )
    sourceDirectories.setFrom(
        files("src/com/healthcare/sv")
    )

    executionData.setFrom(
        fileTree("modules/moduleA/build").include("**/jacoco/*.exec")
    )
}

as you can see the comment : //set to some non existing folders to avoid compiling root project (I know this not right, but can’t be changed at this point of time)

Ah, sorry, I missed that comment.
But then at least set it to an empty list, instead of something non-existing, that is just confusing and still wastes time. :slight_smile:

As this is a quite uncommon setup, I suggest you create an MCVE so that it is easier to talk concretely.

In absence of that, you set something that is called classDirectories to class files. Are you sure JaCoCo can handle that? I suspect this might be the cause of your issue. You might also get more insight when you run with --info or --debug to see what is given to what where.

sure, will give it a try :slight_smile:

1 Like