简体   繁体   中英

Jacoco report for maven multimodule project

I have a maven multimodule project and I use the example configuration for jacoco from the website in order to create code coverage reports: ( http://eclemma.org/jacoco/trunk/doc/examples/build/pom.xml )

So my pom looks like this:

<?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>

When I execute mvn:test the surefire reports are generated within the modules and they look just fine. However, when I run mvn:verify, jacoco does not create a report and the console output displays:

[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

Does anybody know how to fix this error?

  1. Change jacoco report execution <phase>prepare-package</phase> to <phase>test</phase> in order for mvn:test to generate the jacoco report (and not only the surefire report).
  2. Add a property name to jacoco prepare-agent execution
<execution>
    <id>default-prepare-agent</id>
    <goals>
        <goal>prepare-agent</goal>
    </goals>
    <configuration>
        <propertyName>jacoco.agent.ut.arg</propertyName>
    </configuration>
</execution>
  1. Declare surefire (as build) plugin and add jacoco.agent.ut.arg as argline :
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <argLine>${jacoco.agent.ut.arg}</argLine>
    </configuration>
</plugin>
  1. Avoid using mvn:verify goal if you don't have any integration tests. In case you do have integration tests, you should run them with failsafe plugin ( http://maven.apache.org/surefire/maven-failsafe-plugin/ ) and not with surefire . Also, it is recommended to have separate jacoco execution goals for unit tests and integration tests ( http://www.petrikainulainen.net/programming/maven/creating-code-coverage-reports-for-unit-and-integration-tests-with-the-jacoco-maven-plugin/ )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM