我有一个maven多模块项目,我正在使用jacoco-maven进行代码覆盖率报告。不应该报告某些类,因为它们是Spring配置,我对它们不感兴趣。
我已将maven-jacoco插件声明如下:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>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>
问题在于,当我执行mvn clean verify
时,jacoco仍会报告应该已经排除的类,因为我的xml配置指出了。如何正确配置?
答案 0 :(得分:127)
您的XML略有错误,您需要在排除父字段中添加任何类排除项,因此您的上述配置应如下所示Jacoco docs
<configuration>
<excludes>
<exclude>**/*Config.*</exclude>
<exclude>**/*Dev.*</exclude>
</excludes>
</configuration>
使用标准通配符语法,排除字段的值应该是编译类相对于目标target / classes /的类路径(而不是包名)
* Match zero or more characters
** Match zero or more directories
? Match a single character
您也可以这样排除包及其所有子包/子包:
<exclude>some/package/**/*</exclude>
这将排除some.package
中的所有课程以及任何儿童。例如,some.package.child
也不会包含在报告中。
我已经过测试,我的报告目标使用上述方法报告了减少的课程数量。
如果您正在将此报告推送到Sonar,那么您需要告诉Sonar在显示中排除这些可以在声纳设置中完成的类
设置&gt;一般设置&gt;排除&gt;代码覆盖率
Sonar Docs解释得更多
在上面运行命令
mvn clean verify
将显示已排除的课程
没有例外
[INFO] --- jacoco-maven-plugin:0.7.4.201502262128:report (post-test) @ ** ---
[INFO] Analyzed bundle '**' with 37 classes
有排除
[INFO] --- jacoco-maven-plugin:0.7.4.201502262128:report (post-test) @ ** ---
[INFO] Analyzed bundle '**' with 34 classes
希望这有帮助
答案 1 :(得分:7)
虽然安德鲁已经回答了有关细节的问题,但我正在给代码如何在pom中排除它
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>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应用程序
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>sonar-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.sonarsource.scanner.maven</groupId>
<artifactId>sonar-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>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>
答案 2 :(得分:3)
https://github.com/jacoco/jacoco/issues/34
这些是我们所拥有的课程的不同符号:
代理参数,Ant任务和Maven准备代理目标
这些规范允许使用通配符*和?,其中*通配符可以是任意数量的字符,甚至是多个嵌套文件夹。
Maven报告目标
这些规范允许Ant Filespec像通配符*,**和?,其中*通配符只是单个路径元素的一部分。
答案 3 :(得分:3)
另一种解决方案:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>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>
请注意,我们在配置中使用"<element>PACKAGE</element>"
,然后帮助我们在包级别排除。
答案 4 :(得分:3)
使用sonar.coverage.exclusions属性。
mvn clean install -Dsonar.coverage.exclusions=**/*ToBeExcluded.java
这应该从覆盖率计算中排除类别。
答案 5 :(得分:2)
您可以在jacoco插件的配置之外,在声纳属性中配置覆盖率排除范围:
import packageName.R
并记得从插件中删除排除设置
答案 6 :(得分:1)
这是pom.xml
文件中的工作示例。
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>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>
答案 7 :(得分:1)