使用Jacoco的离线工具,使用surefire coverage

时间:2018-02-20 12:11:23

标签: java jacoco maven-surefire-plugin multi-module jacoco-maven-plugin

我正在使用Jacoco通过生成报告的最终聚合器模块来计算多模块项目的覆盖范围。该项目的父母pom文件有确定性和安全性。 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,我似乎无法使用最终报告使用每个模块&#39;转储类,而不是修改过的类......

1 个答案:

答案 0 :(得分:0)

也许有更好的方法,但这对我有用。

在父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>