简体   繁体   English

JaCoCo 未根据源文件生成覆盖率报告 - 方法名称不可点击

[英]JaCoCo Not Generating Coverage Reports based on Source File - Method names not clickable

I have a Maven multi module project where the JaCoCo is not generating reports based on the source files.我有一个 Maven 多模块项目,其中 JaCoCo 没有根据源文件生成报告。

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.一般来说,如果MyService是被测试的类,它将以两种方式报告,一种是通过基于适当包的位置中的文件名MyService.html和方法列表,以数字和图表的形式给出覆盖范围的总体图 - 带有列表在类中的所有方法中,每个方法都有一个指向另一个 html MyService.java.html的可点击链接,其中包含带有红色/绿色/黄色背景的源代码,以显示覆盖状态。

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.在我的场景中,只生成了MyService.html而不是MyService.java.html和方法在前者中列出了覆盖详细信息,但没有指向其他报告的超链接,如下所示。

在此处输入图像描述

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.问题在这里变得更有趣,我的 Maven 配置未在此模块中配置,但在父模块和其他子模块中能够正确生成报告。 Below is the maven plugin configuration for reference.下面是maven插件配置供参考。

Parent POM:父 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:子 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.尝试切换 JaCoCo 版本,但无济于事,现在坚持使用最新的 0.8.2。 And Maven is latest - 3.6.0. Maven 是最新的 - 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.除此之外,在子 pom 中配置的另一个插件是 PMD——它的存在与否对报告没有任何影响。

Similar issue: Gradle JaCoCo plugin - class and method names not clickable in report类似问题: Gradle JaCoCo 插件 - 报告中不可点击的类和方法名称

In absence of Minimal, Complete, and Verifiable example that fully demonstrates steps to reproduce your difficulty, can only quote JaCoCo FAQ :在没有完全演示重现您的困难的步骤的最小、完整和可验证示例的情况下,只能引用JaCoCo 常见问题解答

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:确保满足以下先决条件以在 JaCoCo 覆盖率报告中突出显示源代码:

  • 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.关于第一点,请参见javac-g选项、maven-compiler-plugin 的debugdebuglevel选项。

For the second point make sure that source file Example.java with对于第二点,请确保源文件Example.java

package org.example;

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

Thank you!谢谢! @Godin @戈丁

It is also important to make sure you specify the SourceDirectory .确保指定SourceDirectory也很重要 In my case - working against Groovy Source Files, for detailed coverage you should specify the child Maven structure as follows:在我的情况下 - 使用Groovy源文件,为了详细覆盖,您应该指定Maven结构,如下所示:

<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.再次感谢。

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.在我的情况下, <sourceDirectory>src/main/java</sourceDirectory>已经在<build>..here..<plugins>..</plugins>..</build>部分中定义,并且调试选项已打开用于Java编译,但JaCoCo报告中的链接仍然不可点击。

What I had to do is, to define <sourceDirectory> configuration within Jacoco Plugin (defined inside of <plugins> section)我必须做的是,在Jacoco 插件中定义<sourceDirectory>配置(在<plugins>部分中定义)

Ex: Now the links are working and also in my case, I'm putting the coverage report data/files inside, a custom folder (c overage/jacoco-report ) instead of using the default one.例如:现在链接正在工作,而且在我的情况下,我将覆盖率报告数据/文件放入自定义文件夹(c overage/jacoco-report )而不是使用默认文件夹。 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>

I got the same issue working with an Android java/kotlin project.我在使用 Android java/kotlin 项目时遇到了同样的问题。 In my case, I have to use custom paths for the code main src folder.就我而言,我必须为代码主 src 文件夹使用自定义路径。 So I have something like this:所以我有这样的事情:

src/org/example/lib_name

In that folder, I have my java and kotlin code.在那个文件夹中,我有我的 java 和 kotlin 代码。 So I configure my jacoco script with this configuration所以我用这个配置配置我的 jacoco 脚本

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"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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