7

I have a Maven multi module project where the JaCoCo is not generating reports based on the source files.

Generally, if MyService is the class under test, it would be reported in two ways, one through a file name MyService.html in appropriate package based location with a list of methods giving an overall picture of the coverage in numbers and graphs - with listing of all the methods in the class and each method has a clickable link to another html MyService.java.html which contains the source code with red/green/yellow background to display coverage status.

In my scenario, only MyService.html is generated and not the MyService.java.html and methods are listed in the former with coverage details, but without hyperlinks to the other report as displayed below.

enter image description here

Issue gets more interesting here that my Maven configuration is not configured in this module, but in a parent module and other child modules are able to generate reports properly. Below is the maven plugin configuration for reference.

Parent POM:

<plugin>
  <groupId>org.jacoco</groupId>
  <artifactId>jacoco-maven-plugin</artifactId>
  <version>${jacoco.version}</version>
  <configuration>
     <destFile>${project.basedir}/target/jacoco-unit.exec</destFile>
     <dataFile>${project.basedir}/target/jacoco-unit.exec</dataFile>
     <append>true</append>
  </configuration>
  <executions>
     <execution>
        <id>jacoco-initialize</id>
        <goals>
           <goal>prepare-agent</goal>
        </goals>
     </execution>
     <execution>
        <id>jacoco-site</id>
        <phase>package</phase>
        <goals>
           <goal>report</goal>
        </goals>
     </execution>
  </executions>
</plugin>

Child POM:

    <plugin>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
    </plugin>

Tried switching JaCoCo versions, but none helped, now sticking to the latest 0.8.2. And Maven is latest - 3.6.0. Apart from this, the other plugin configured in the child pom is PMD - whose presence or absence did not make any difference to report.

Similar issue: Gradle JaCoCo plugin - class and method names not clickable in report

Pavan Kumar
  • 4,182
  • 1
  • 30
  • 45
  • If the problem continues despite of applying the workarounds, you may have a look at my answer on [maven jacoco: not generating code coverage report](https://stackoverflow.com/questions/25395255/maven-jacoco-not-generating-code-coverage-report/71661614#71661614). – Murat Yıldız Mar 29 '22 at 12:01

4 Answers4

9

In absence of Minimal, Complete, and Verifiable example that fully demonstrates steps to reproduce your difficulty, can only quote JaCoCo FAQ :

Why does the coverage report not show highlighted source code?

Make sure the following prerequisites are fulfilled to get source code highlighting in JaCoCo coverage reports:

  • Class files must be compiled with debug information to contain line numbers.
  • Source files must be properly supplied at report generation time.

About first point see -g option of javac, debug and debuglevel options of maven-compiler-plugin.

For the second point make sure that source file Example.java with

package org.example;

is located in src/main/java/org/example/Example.java

Godin
  • 9,801
  • 2
  • 39
  • 76
  • Thanks @Godin, your input helped. I started off with compiler plugin configuration and figured out debug was turned on by default and we did not override it anywhere. Then got into package structure and that was the issue. Package org.example was somehow created in a single directory named "org.example" and not "example" under "org". Not sure how we ended up in this structure, but fixing that created the reports as expected. – Pavan Kumar Dec 19 '18 at 07:27
  • If for some legacy reason I have my source location at `src/org/example/Example.java` can I get away without changing the folder structure or it is not possible within jacoco? – zygimantus May 03 '22 at 07:08
3

Thank you! @Godin

It is also important to make sure you specify the SourceDirectory. In my case - working against Groovy Source Files, for detailed coverage you should specify the child Maven structure as follows:

<build>
        <sourceDirectory>src/main/groovy</sourceDirectory>
        <plugins> ... </plugins>
</build>

This was a combination of the issues in my case. The source Directory and the Compile with Debug option. Thanks again.

Ethan K
  • 131
  • 1
  • 9
1

In my case, <sourceDirectory>src/main/java</sourceDirectory> was already defined inside <build>..here..<plugins>..</plugins>..</build> section and debug option was turned on for Java compilation, but the links in JaCoCo report still were not clickable.

What I had to do is, to define <sourceDirectory> configuration within Jacoco Plugin (defined inside of <plugins> section)

Ex: Now the links are working and also in my case, I'm putting the coverage report data/files inside, a custom folder (coverage/jacoco-report) instead of using the default one. There are bunch of other configuration fields/settings you can use.

    <plugin>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>0.8.2</version>
        <executions>
            <execution>
                <goals>
                    <goal>prepare-agent</goal>
                </goals>
            </execution>
            <!-- attached to Maven test phase -->
            <execution>
                <id>report</id>
                <phase>test</phase>
                <goals>
                    <goal>report</goal>
                </goals>
                <configuration>

                    <sourceDirectory>src/main/java</sourceDirectory>
                    <!-- see above -->
                    <outputDirectory>coverage/jacoco-report</outputDirectory>

                </configuration>
            </execution>
        </executions>
    </plugin>
AKS
  • 16,482
  • 43
  • 166
  • 258
1

I got the same issue working with an Android java/kotlin project. In my case, I have to use custom paths for the code main src folder. So I have something like this:

src/org/example/lib_name

In that folder, I have my java and kotlin code. So I configure my jacoco script with this configuration

task jacocoTestReport(type: JacocoReport, dependsOn: ['testDebugUnitTest']) {

    reports {
        xml.enabled = true
        html.enabled = true
    }

    def fileFilter = ['**/R.class', '**/R$*.class', '**/BuildConfig.*', '**/Manifest*.*', '**/*Test*.*', 'android/**/*.*']
    //def debugTree = fileTree(dir: "${buildDir}/intermediates/classes/debug", excludes: fileFilter)
    def javaClasses = fileTree(dir: "${buildDir}/intermediates/javac/debug/classes", excludes: fileFilter)
    def kotlinClasses = fileTree(dir: "${buildDir}/tmp/kotlin-classes/debug", excludes: fileFilter)

    def mainSrc = "${project.projectDir}/src/org/example/lib_name"

    sourceDirectories.setFrom(files([mainSrc]))
    classDirectories.setFrom(files([javaClasses, kotlinClasses]))
    executionData.setFrom(fileTree(dir: "$buildDir", includes: [
            "jacoco/testDebugUnitTest.exec"
    ]))
}

But that was not working. For some reason, If I define a specified path to a folder It doesn't work. So just changing this line the report starts working.

def mainSrc = "${project.projectDir}/src"