如何从代码覆盖率中排除某些类?(爪哇)

iam*_*hua 5 java spring maven spring-boot

我已经可以通过在我的 POM 文件的标签中添加这个来排除带有声纳立方体报告的类。

 <properties>
        <java.version>1.8</java.version>
        <sonar.exclusions> 
            /src/main/java/com/example/org/test/mainpackage/message/**/*
            ,
            /src/main/java/com/example/org/test/mainpackage/security/jwt/**/*
            ,
            /src/main/java/com/example/org/test/mainpackage/security/services/**/*
            ,
            /src/main/java/com/example/org/test/mainpackage/controller/AuthRestAPIs.java
            ,
            /src/main/java/com/example/org/test/mainpackage/controller/TestRestAPIs.java
        </sonar.exclusions>
    </properties>
Run Code Online (Sandbox Code Playgroud)

然而,在本地运行以测试代码覆盖率的相同代码根本不起作用。

我在哪里可以使用 Maven 和 Spring Boot 以编程方式进行设置?

我已经可以通过

设置 > 构建、执行、部署 > 排除 > [要排除的类]

或通过

编辑配置 > 选择代码覆盖率选项卡 > 然后添加我想要排除或仅包含在代码覆盖率报告中的包或类。

我想要的是编码要排除的类,即在 pom 文件中,就像我排除声纳 qube 的代码一样。

使其简短。我如何以编程方式执行此操作?

我想要以编程方式执行此操作的原因是,我为排除代码覆盖率中报告的文件所做的上述两个步骤是,我的所有项目中都没有包含可以推送到存储库中的文件,以便我进行更改因为排除将反映在所有从 git 存储库中提取的人中。没有添加任何文件或配置,我可以推送并反映到 repo 中的所有内容。这意味着我的排除项仅在本地有效,对其他人无效。对?。所以在这里我要求一种以编程方式执行的方法,例如将排除项放入 POM 文件中。

snm*_*ula 9

当您使用 Maven 时,您需要在 jacoco 插件中配置排除项,该插件用于捕获覆盖率统计信息并将其传递给声纳。

<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.1</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>
            <configuration>
                <!-- Sets the path to the file which contains the execution data. -->
                <dataFile>target/jacoco.exec</dataFile>
                <!-- Sets the output directory for the code coverage report. -->
                <outputDirectory>target/jacoco-ut</outputDirectory>
            </configuration>
        </execution>
</executions>
<configuration>
    <systemPropertyVariables>
        <jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile>
    </systemPropertyVariables>
    <excludes>
        <exclude>snmaddula/app/domain/*.class</exclude>
        <exclude>snmaddula/app/exception/*.class</exclude>
        <exclude>snmaddula/app/filter/*.class</exclude>
        <exclude>snmaddula/app/App.class</exclude>
    </excludes>
</configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)

参考: