Jacoco关于maven多模块项目的报告

时间:2016-05-19 09:10:44

标签: java maven surefire

我有一个maven多模块项目,我使用网站上的jacoco示例配置来创建代码覆盖率报告:(http://eclemma.org/jacoco/trunk/doc/examples/build/pom.xml

所以我的pom看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>com.dti</groupId>
    <artifactId>dti-parent</artifactId>
    <version>1.0.1</version>
</parent>
<groupId>com.dti.myproject</groupId>
<artifactId>myproject-build-all</artifactId>
<version>6.5.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>myproject</name>
<modules>
    <module>../myproject-config</module>
    <module>../myproject-messages</module>
    <module>../myproject-persistence</module>
    <module>../myproject-resources</module>
    <module>../myproject-service</module>
</modules>
<build>
    <plugins>
        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.7.7-SNAPSHOT</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>
                            <!--  implementation is needed only for Maven 2  -->
                            <rule implementation="org.jacoco.maven.RuleConfiguration">
                                <element>BUNDLE</element>
                                <limits>
                                    <!--  implementation is needed only for Maven 2  -->
                                    <limit implementation="org.jacoco.report.check.Limit">
                                        <counter>COMPLEXITY</counter>
                                        <value>COVEREDRATIO</value>
                                        <minimum>0.0</minimum>
                                    </limit>
                                </limits>
                            </rule>
                        </rules>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
</project>

当我执行mvn:test时,在模块中生成了surefire报告,它们看起来很好。但是,当我运行mvn:verify时,jacoco不会创建报告,并且控制台输出显示:

[INFO] --- jacoco-maven-plugin:0.7.7-SNAPSHOT:report (default-report) @ myproject-build-all ---
[INFO] Skipping JaCoCo execution due to missing execution data file.
[INFO]
[INFO] --- jacoco-maven-plugin:0.7.7-SNAPSHOT:check (default-check) @ myproject-build-all ---
[INFO] Skipping JaCoCo execution due to missing execution data file:C:\workspaces\release-trunk\myproject-build-all\target\jacoco.exec

有人知道如何修复此错误吗?

1 个答案:

答案 0 :(得分:2)

  1. 将jacoco报告执行<phase>prepare-package</phase>更改为<phase>test</phase>,以便 mvn:test 生成jacoco报告(而不仅仅是万无一失的报告)。
  2. 向jacoco prepare-agent execution
  3. 添加属性名称
    <execution>
        <id>default-prepare-agent</id>
        <goals>
            <goal>prepare-agent</goal>
        </goals>
        <configuration>
            <propertyName>jacoco.agent.ut.arg</propertyName>
        </configuration>
    </execution>
    
    1. 声明surefire(as build)插件并将jacoco.agent.ut.arg添加为argline:
    2. <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-surefire-plugin</artifactId>
          <configuration>
              <argLine>${jacoco.agent.ut.arg}</argLine>
          </configuration>
      </plugin>
      
      1. 如果您没有任何集成测试,请避免使用 mvn:verify 目标。如果您有集成测试,则应使用 failsafe 插件(http://maven.apache.org/surefire/maven-failsafe-plugin/)而不是 surefire 运行它们。此外,建议单元测试和集成测试具有单独的jacoco执行目标(http://www.petrikainulainen.net/programming/maven/creating-code-coverage-reports-for-unit-and-integration-tests-with-the-jacoco-maven-plugin/