十一、SonarQube自定义质量门禁和使用Jacoco

发布时间 2023-05-27 19:44:03作者: shigp1

一、自定义质量门阀

接着上个例子,从流水线的运行结果来看,质量门禁通过了,使用的是默认的规则。但是代码的规则和单元测试的覆盖率等没有限制。因此需要自定义质量门禁规则。
 

 
这里添加了两个代码条件并设置为默认规则。如果覆盖率不到80%或单元测试有错误都不通过质量门禁。

 
重新运行流水线后,看到:
 

 
查看日志后发现:
 

 

新增单元测试,保证覆盖率达到80%以上后,重新运行流水线,发现还是报错:
 

 
登录SonarQube发现覆盖率还是0。可知SonarQube未获取到覆盖率。

二、使用Jacoco

JaCoCo是一个免费的Java代码覆盖库。在idea中使用Jacoco,在pom.xml中加入依赖:

<dependency>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>0.7.8</version>
    </dependency>

在pom.xml的properties中配置:

    <sonar.jacoco.itReportPath>${project.basedir}/target/jacoco.exec</sonar.jacoco.itReportPath>
    <sonar.language>java</sonar.language>

并在pom.xml中配置插件:

<plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.8.7</version>
            <executions>
                <execution>
                    <id>prepare-agent</id>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                </execution>
                <execution>
                    <id>report</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>
                </execution>
                <execution>
                    <id>post-unit-test</id>
                    <phase>test</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>
                    <configuration>
                        <dataFile>target/jacoco.exec</dataFile>
                    </configuration>
                </execution>
            </executions>
        </plugin>

运行测试后:
 

 
用浏览器打开后:
 

 

提交到gitee后重新运行流水线,看到:
 

 

登录SonarQube,查看:
 

 

如果要排除类,不需要查看覆盖率,可以配置:

<plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.8.7</version>
            <configuration>
                <excludes>
                    <exclude>com/example/mytest/MyTestApplication.class</exclude>
                </excludes>
            </configuration>
            <executions>
                <execution>
                    <id>prepare-agent</id>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                </execution>
                <execution>
                    <id>report</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>
                </execution>
                <execution>
                    <id>post-unit-test</id>
                    <phase>test</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>
                    <configuration>
                        <dataFile>target/jacoco.exec</dataFile>
                    </configuration>
                </execution>
            </executions>
        </plugin>

在exclude加入要排除的class。运行测试后查看Jacoco报告发现MyTestApplication已不在Jacoco报告中。