Jacoco Maven multi module project coverage

Deadly 提交于 2020-07-17 09:49:19

问题


Seems like there are couple of questions, which are quite old and things changed from Java 8 support of Jacoco.

My Project contains following structure

pom.xml
|
|
-----sub module A pom.xml
|
|
-----sub module B pom.xml
|
|
-----sub module C pom.xml

I have configured the main pom like this

Main POM.xml

<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>
    <groupId>com.test</groupId>
    <artifactId>jacoco-multi</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>
    <modules>
        <module>projectA</module>
        <module>projectB</module>
    </modules>

    <properties>
        <jacoco.version>0.5.7.201204190339</jacoco.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit-dep</artifactId>
            <version>4.10</version>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.hamcrest</groupId>
                    <artifactId>hamcrest-core</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-core</artifactId>
            <version>1.3.RC2</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-library</artifactId>
            <version>1.3.RC2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.7.5.201505241946</version>
                <configuration>
                    <destFile>${project.basedir}/../target/jacoco.exec</destFile>
                    <append>true</append>
                </configuration>
                <executions>
                    <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>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>2.16</version>
                <executions>
                    <execution>
                        <id>default-integration-test</id>
                        <goals>
                            <goal>integration-test</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.16</version>
            </plugin>
        </plugins>
    </build>

</project>

A Pom.xml

<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>
        <artifactId>jacoco-multi</artifactId>
        <groupId>com.test</groupId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>..</relativePath>
    </parent>
    <artifactId>projectA</artifactId>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.12</version>
                </plugin>

            </plugins>
        </pluginManagement>
    </build>

</project>

B pom.xml

<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>
    <artifactId>jacoco-multi</artifactId>
    <groupId>com.test</groupId>
    <version>0.0.1-SNAPSHOT</version>
    <relativePath>..</relativePath>
</parent>
<artifactId>projectB</artifactId>

<dependencies>
    <dependency>
        <groupId>com.test</groupId>
        <artifactId>projectA</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
</dependencies>

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.12</version>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

I am executing this command mvn clean package. I can see jacoco.exec is getting generated, however I am not able to see any HTML reports are being to verify the data.

Please help me with this. Another point is, my configuration is correct for Multi-Module projects?

Update

Identified issue. <destFile>${project.basedir}/../target/jacoco.exec</destFile> changed to <destFile>${project.basedir}/target/jacoco.exec</destFile>

Now it's generating reports for individual modules. Any idea how to generate consolidated report


回答1:


JaCoCo version 0.7.7 can generate an aggregate coverage report from multiple Maven modules through a new goal jacoco:report-aggregate.




回答2:


Copy the code end of article at https://dzone.com/articles/jacoco-maven-multi-module and put it in .... works for me. Just add as many modules as you would like to report on. Also I needed to change antrun from 1.7 to 1.8 for the ant tasks to run.




回答3:


One problem in multimodule projects is caused, if the aggregator pom is used as parent pom for the modules either, like it is the case in the above example:

- parentAggregator pom
---sub module A pom.xml -> parentAggregator pom
---sub module B pom.xml -> parentAggregator pom
---sub module C pom.xml -> parentAggregator pom

In this case, the build order is:

- parentAggregator
- sub module A
- sub module B
- sub module C

which means, that the parent aggregator can not collect complete information. In my case a transfer of data into sonarQube by mvn sonar:sonar resulted in unexpected and uncomplete results.

Changing the module structure to:

- aggregator pom
-- parent pom
---sub module A pom.xml -> parent pom
---sub module B pom.xml -> parent pom
---sub module C pom.xml -> parent pom

will change the build order to:

- parent
- sub module A
- sub module B
- sub module C
- aggregator

In this case aggregator will be the last one and work with the results of the modules. In my case the results in SonarQube were like expected.




回答4:


After scannning many solutions I created a simple but complete Jacoco demo project showing:

  • Multi module project
  • Unit test (via mvn clean install)
  • Integration test (via mvn clean install -P integration-test)
  • Jacoco - test coverage ( both aggregate datafile and aggregate reporting)
  • FindBugs - code quality

Enjoy the simple demo project.



来源:https://stackoverflow.com/questions/33078745/jacoco-maven-multi-module-project-coverage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!