Maven Jacoco Configuration - Exclude classes/packages from report not working
我有一个Maven多模块项目,并且正在使用jacoco-maven进行代码覆盖率报告。 有些类不应该报告,因为它们是Spring配置的,我对它们不感兴趣。
我已经声明了maven-jacoco插件如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | <plugin> <groupId>org.jacoco</groupId> jacoco-maven-plugin</artifactId> <version>0.7.2.201409121644</version> <configuration> <outputDirectory>${project.reporting.outputDirectory}/jacoco-ut</outputDirectory> <exclude>some.package.*</exclude> <exclude>**/*Config.*</exclude> <exclude>**/*Dev.*</exclude> <exclude>some/package/SomeClass.java</exclude> </configuration> <executions> <execution> <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> </execution> </executions> </plugin> |
问题是,当我执行
您的XML略有错误,您需要在excludes父字段中添加任何类排除,因此根据Jacoco docs,您的上述配置应如下所示
1 2 3 4 5 6 | <configuration> <excludes> <exclude>**/*Config.*</exclude> <exclude>**/*Dev.*</exclude> </excludes> </configuration> |
排除字段的值应该是使用标准通配符语法相对于目录target / classes /的已编译类的类路径(而不是包名称)
1 2 3 | * Match zero or more characters ** Match zero or more directories ? Match a single character |
您还可以通过以下方式排除软件包及其所有子/子软件包:
1 | <exclude>some/package/**/*</exclude> |
这将排除
我已经使用上述方法进行了测试,并且我的报告目标报告的课程减少了。
如果您随后将此报告推送到Sonar中,则需要告诉Sonar在显示屏中排除这些类,这可以在Sonar设置中完成
设置>常规设置>排除>代码覆盖率
Sonar Docs对此进行了解释
在上面运行命令
1 | mvn clean verify |
将显示已被排除的班级
没有排除
1 2 | [INFO] --- jacoco-maven-plugin:0.7.4.201502262128:report (post-test) @ ** --- [INFO] Analyzed bundle '**' with 37 classes |
有排除
1 2 | [INFO] --- jacoco-maven-plugin:0.7.4.201502262128:report (post-test) @ ** --- [INFO] Analyzed bundle '**' with 34 classes |
希望这可以帮助
尽管安德鲁已经详细回答了问题,但我给出了如何在pom中排除它的代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <plugin> <groupId>org.jacoco</groupId> jacoco-maven-plugin</artifactId> <version>0.7.9</version> <configuration> <excludes> <exclude>**/*com/test/vaquar/khan/HealthChecker.class</exclude> </excludes> </configuration> <executions> <!-- prepare agent for measuring integration tests --> <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> |
对于Springboot应用程序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> maven-surefire-plugin</artifactId> </plugin> <plugin> <groupId>org.codehaus.mojo</groupId> sonar-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.sonarsource.scanner.maven</groupId> sonar-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.jacoco</groupId> jacoco-maven-plugin</artifactId> <configuration> <excludes> <!-- Exclude class from test coverage --> <exclude>**/*com/khan/vaquar/Application.class</exclude> <!-- Exclude full package from test coverage --> <exclude>**/*com/khan/vaquar/config/**</exclude> </excludes> </configuration> </plugin> </plugins> </build> |
另一个解决方案:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | <plugin> <groupId>org.jacoco</groupId> jacoco-maven-plugin</artifactId> <version>0.7.5.201505241946</version> <executions> <execution> <id>default-prepare-agent</id> <goals> <goal>prepare-agent</goal> </goals> </execution> <execution> <id>default-report</id> <phase>prepare-package</phase> <goals> <goal>report</goal> </goals> </execution> <execution> <id>default-check</id> <goals> <goal>check</goal> </goals> <configuration> <rules> <rule implementation="org.jacoco.maven.RuleConfiguration"> <excludes> <exclude>com.mypackage1</exclude <exclude>com.mypackage2</exclude> </excludes> <element>PACKAGE</element> <limits> <limit implementation="org.jacoco.report.check.Limit"> <counter>COMPLEXITY</counter> <value>COVEREDRATIO</value> <minimum>0.85</minimum> </limit> </limits> </rule> </rules> </configuration> </execution> </executions> </plugin> |
请注意,我们在配置中使用
使用sonar.coverage.exclusions属性。
1 | mvn clean install -Dsonar.coverage.exclusions=**/*ToBeExcluded.java |
这应将类别从覆盖率计算中排除。
https://github.com/jacoco/jacoco/issues/34
这些是我们拥有的类的不同符号:
- VM名称:java / util / Map $ Entry
- Java名称:java.util.Map $ Entry文件
- 名称:java / util / Map $ Entry.class
代理参数,Ant任务和Maven准备代理目标
- 包括:Java名称(VM名称也可以)
- 排除:Java名称(VM名称也可以)
- exclclassloader:Java名称
这些规范允许使用通配符*和?,其中*通配符可以使用任意数量的字符,甚至可以使用多个嵌套文件夹。
Maven报告目标
- 包括:文件名
- 排除:文件名
这些规范允许Ant Filespec像通配符*,**和?,其中*仅通配符是单个路径元素的一部分。
您可以在jacoco插件的配置之外,在声纳属性中配置覆盖范围排除:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | ... <properties> .... <sonar.exclusions> **/generated/**/*, **/model/**/* </sonar.exclusions> <sonar.test.exclusions> src/test/**/* </sonar.test.exclusions> .... <sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin> <sonar.jacoco.reportPath>${project.basedir}/../target/jacoco.exec</sonar.jacoco.reportPath> <sonar.coverage.exclusions> **/generated/**/*, **/model/**/* </sonar.coverage.exclusions> <jacoco.version>0.7.5.201505241946</jacoco.version> .... </properties> .... |
并记得从插件中删除排除设置
这是
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | <plugin> <groupId>org.jacoco</groupId> jacoco-maven-plugin</artifactId> <version>${jacoco.version}</version> <executions> <execution> <id>prepare-agent</id> <goals> <goal>prepare-agent</goal> </goals> </execution> <execution> <id>post-unit-test</id> <phase>test</phase> <goals> <goal>report</goal> </goals> </execution> <execution> <id>default-check</id> <goals> <goal>check</goal> </goals> </execution> </executions> <configuration> <dataFile>target/jacoco.exec</dataFile> <!-- Sets the output directory for the code coverage report. --> <outputDirectory>target/jacoco-ut</outputDirectory> <rules> <rule implementation="org.jacoco.maven.RuleConfiguration"> <element>PACKAGE</element> <limits> <limit implementation="org.jacoco.report.check.Limit"> <counter>COMPLEXITY</counter> <value>COVEREDRATIO</value> <minimum>0.00</minimum> </limit> </limits> </rule> </rules> <excludes> <exclude>com/pfj/fleet/dao/model/**/*</exclude> </excludes> <systemPropertyVariables> <jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile> </systemPropertyVariables> </configuration> </plugin> |