gpt4 book ai didi

java - 如何将某些类排除在代码覆盖范围之外? ( java )

转载 作者:行者123 更新时间:2023-11-29 08:22:51 27 4
gpt4 key购买 nike

我已经可以通过将其添加到我的 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>

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

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

我已经可以通过

排除我想要的类

Settings > Build, Execution, Deployment > Excludes > [Class(es) to be excluded]

或通过

Edit Configurations > Select Code Coverage tab > then adding the package or class I want to be excluded or include only in the code coverage report.

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

简而言之。我如何以编程方式执行此操作?

我想以编程方式执行此操作的原因是,我执行的上述两个步骤是为了将文件排除在代码覆盖率报告之外,因为我的所有项目中都没有包含我可以推送到存储库中的文件,因此我为排除所做的更改将反射(reflect)在所有将从 git 存储库中提取的人中。在没有添加任何文件或配置的情况下,我可以推送并反射(reflect)给存储库中的所有人。这意味着我的排除项仅适用于本地,不适用于其他人。正确的?。所以在这里我要求一种以编程方式执行的方法,例如将排除项放入 POM 文件中。

最佳答案

当您使用 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>

引用资料:

关于java - 如何将某些类排除在代码覆盖范围之外? ( java ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56016409/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com