我正在使用Jacoco通过生成报告的最终聚合器模块来计算多模块项目的覆盖范围。该项目的父pom文件具有surefire和Jacoco配置如下:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
<argLine>${argLine} -Xmx2048m -Duser.timezone=UTC</argLine>
<forkedProcessTimeoutInSeconds>1200</forkedProcessTimeoutInSeconds>
<forkCount>1</forkCount>
<reuseForks>true</reuseForks>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.0</version>
<executions>
<execution>
<id>default-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
</executions>
</plugin>
项目的最终报告模块(继承所有其他模块作为依赖项)的Jacoco配置如下:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.0</version>
<executions>
<execution>
<id>report-aggregate</id>
<phase>test</phase>
<goals>
<goal>report-aggregate</goal>
</goals>
</execution>
</executions>
</plugin>
但是我在一些模块中收到与测试有关的jacoco警告:
[INFO] Analyzed bundle 'project-submod1' with 46 classes
[WARNING] Classes in bundle 'project-submod1' do no match with execution data. For report generation the same class files must be used as at runtime.
[WARNING] Execution data for class org/project/submod1/ExampleClass does not match.
所以我读过我可以使用离线检测或设置classDumpDir来解决这个问题:http://www.eclemma.org/jacoco/trunk/doc/classids.html
但是我不确定如何完成这两个选项。我已经尝试将离线检测任务添加到父pom文件中的Jacoco,但是我接到了关于类已经过检测的投诉。或者,如果我为每个模块设置ClassDumpDir,我似乎无法使用最终报告使用每个模块的转储类,而不是修改后的类...
可能有更好的方法,但这对我有用。
在父pom.xml中
<jacoco.version>0.8.3</jacoco.version>
<surefire.version>3.0.0-M3</surefire.version>
<!-- Stuff Skipped -->
<dependency>
<groupId>org.jacoco</groupId>
<artifactId>org.jacoco.agent</artifactId>
<classifier>runtime</classifier>
<version>${jacoco.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.version}</version>
<type>maven-plugin</type>
<scope>test</scope>
</dependency>
<!-- Stuff Skipped -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.version}</version>
<configuration>
<reportsDirectory>../surefire-reports</reportsDirectory>
<systemPropertyVariables>
<jacoco-agent.destfile>jacoco.exec</jacoco-agent.destfile>
</systemPropertyVariables>
</configuration>
</plugin>
在每个孩子的pom.xml中(我尝试了一些变种以避免编辑孩子,但它们似乎都没有工作)。这基于离线jacoco工具的配置。
<dependency>
<groupId>org.jacoco</groupId>
<artifactId>org.jacoco.agent</artifactId>
<classifier>runtime</classifier>
<version>${jacoco.version}</version>
<scope>test</scope>
</dependency>
<!-- Stuff Skipped -->
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
<executions>
<execution>
<id>default-instrument</id>
<goals>
<goal>instrument</goal>
</goals>
</execution>
<execution>
<id>default-restore-instrumented-classes</id>
<goals>
<goal>restore-instrumented-classes</goal>
</goals>
</execution>
</executions>
</plugin>
最后一部分是在报告模块中,类似于通常用于多模块jacoco的建议。但是,它包括在运行报告器之前调用jacoco.exec结果的调用。
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
<configuration>
<fileSets>
<fileSet>
<directory>..</directory>
<include>**/*.exec</include>
</fileSet>
</fileSets>
</configuration>
<executions>
<execution>
<phase>verify</phase>
<goals>
<goal>merge</goal>
<goal>report-aggregate</goal>
</goals>
</execution>
</executions>
</plugin>