为什么 JaCoCo Jenkins 报告中只出现指令覆盖率?

Why does only instruction coverage appear in the JaCoCo Jenkins report?

我有一个 Java 项目,我想在 Jenkins 构建中查看其自动化测试覆盖率。

为此,我修改了文件,如下所示。

Jenkinsfile:

pipeline {
    agent any
    stages {
        stage("Test") {
            steps {
                sh 'cat settings_default.xml'
                sh 'mvn -gs settings_default.xml test'
            }
        }
        stage("Test Reports") {
            steps {
                jacoco(
                        execPattern: 'target/*.exec',
                        classPattern: 'target/classes',
                        sourcePattern: 'src/main/java',
                        exclusionPattern: 'src/test*',
                        changeBuildStatus: true,
                        runAlways: true,
                        minimumBranchCoverage: '60'
                )
            }
        }
        stage("Build") {
            steps {
                sh 'mvn -gs settings_default.xml package'
            }
         }
    }
}

pom.xml:将以下片段添加到 build/plugins

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.8.6</version>
    <configuration>
        <excludes>
            <exclude>**/configuration/*.*</exclude>
            <exclude>**/model/*</exclude>
            <exclude>**/MyApplication.*</exclude>
        </excludes>
    </configuration>
    <executions>
        <execution>
            <id>prepare-agent</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
        </execution>
        <execution>
            <id>check-coverage</id>
            <goals>
                <goal>check</goal>
            </goals>
            <configuration>
                <rules>
                    <rule>
                        <element>CLASS</element>
                        <limits>
                            <limit>
                                <counter>LINE</counter>
                                <value>COVEREDRATIO</value>
                                <minimum>0.80</minimum>
                            </limit>
                            <limit>
                                <counter>BRANCH</counter>
                                <value>COVEREDRATIO</value>
                                <minimum>0.80</minimum>
                            </limit>
                        </limits>
                    </rule>
                </rules>
            </configuration>
        </execution>
        <execution>
            <id>report</id>
            <goals>
                <goal>report</goal>
            </goals>
        </execution>
    </executions>
</plugin>

当我运行 Jenkins 作业时,我只看到报告中的指令覆盖率:

我需要更改什么以及在哪里(pom.xmlJenkinsfile,服务器上的 Jenkins 配置,其他)才能看到分支和 class 也有报道吗?

更新 1: 添加

    <reporting>
        <plugins>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.8.6</version>
                <reportSets>
                    <reportSet>
                        <reports>
                            <report>report</report>
                        </reports>
                    </reportSet>
                </reportSets>
            </plugin>

        </plugins>
    </reporting>

pom.xml都没有解决问题

要查看完整的 jacoco 报告,我会使用 HTML Publisher 插件。

首先,您需要配置pom.xml文件来生成jacoco覆盖率报告,添加以下插件。

           <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.8.6</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>
                    </execution>
                </executions>
            </plugin>

然后,在运行ning mvn install命令后,jacoco报告会生成在目录target/site/jacoco/index.html.

现在您必须配置 Jenkins 以显示此报告。因此,下一步是在 Jenkins 中安装 HTML Publisher plugin。您可以使用插件管理器来完成此任务。

最后,将您的 Jenkins 作业配置为 运行 mvn install 命令,并将 HTML Publisher 设置为构建后任务。基本上,您告诉 Jenkins 阅读 jacoco 报告并显示它。

另一个细节是您需要运行 Jenkins 使用下面的命令来避免 在可视化报告时。

java -Dhudson.model.DirectoryBrowserSupport.CSP="sandbox allow-same-origin allow-scripts; default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; img-src 'self'; style-src 'self' 'unsafe-inline'; font-src *;"  -jar jenkins.war 

最后的报告会是这样的:

错误的原因是这是一个包含两个不同 pom.xml 的多模块 Maven 项目。正在为模块(子目录)生成 Jenkins 报告,其中一个步骤配置错误(配置指向错误的目录)。

        stage("Test Reports") {
            steps {
                dir('my-module') {
                    jacoco( 
[...]

dir('my-module') { [...] } 围绕 jacoco 语句解决了问题。