Jacoco Maven multi module project coverage

后端 未结 5 1835
余生分开走
余生分开走 2020-12-30 01:49

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



        
相关标签:
5条回答
  • 2020-12-30 02:18

    Follow below-mentioned instructions

    1. Create a new sub-project. This will be used as , report aggregator. parent pom will be like:

    <modules>
            <module>A</module>
            <module>B</module>
            <module>C</module>
            <module>ReportAggregator</module>
        </modules>

    1. In aggregator branch pom- add other subprojects dependencies.

    <dependency>
                <groupId>xyz</groupId>
                <artifactId>A</artifactId>
                <version>${project.version}</version>
            </dependency>

    1. In aggregator branch pom- configure jacoco plugin

    <plugin>
                    <groupId>org.jacoco</groupId>
                    <artifactId>jacoco-maven-plugin</artifactId>
                    <version>0.8.6</version>
                    <executions>
                        <execution>
                            <goals>
                                <goal>prepare-agent</goal>
                            </goals>
                        </execution>
                        <execution>
                            <id>report-aggregate</id>
                            <phase>verify</phase>
                            <goals>
                                <goal>report-aggregate</goal>
                            </goals>
                            <configuration>
                                <dataFileIncludes>
                                    <dataFileInclude>**/jacoco.exec</dataFileInclude>
                                </dataFileIncludes>
                                <outputDirectory>${project.reporting.outputDirectory}/jacoco-aggregate</outputDirectory>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>

    1. In aggregator branch pom- configure surefire plugin as

    <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>3.0.0-M5</version>
                    <configuration>
                        <systemPropertyVariables>
                            <jacoco-agent.destfile>**/jacoco.exec</jacoco-agent.destfile>
                        </systemPropertyVariables>
                    </configuration>
                </plugin>

    1. (optional step) If anybody face warning/error like: Classes in bundle '*' do no match with execution data. For report generation, the same class files must be used as at runtime.**

    Then add below mentioned lines in aggregator branch pom

     <plugin>
                    <groupId>org.jacoco</groupId>
                    <artifactId>jacoco-maven-plugin</artifactId>
                    <version>0.8.6</version>
                    <executions>
                        <execution>
                            <id>instrument-ut</id>
                            <goals>
                                <goal>instrument</goal>
                            </goals>
                        </execution>
                        <execution>
                            <id>restore-ut</id>
                            <goals>
                                <goal>restore-instrumented-classes</goal>
                            </goals>
                        </execution>
                        <execution>
                            <goals>
                                <goal>prepare-agent</goal>
                            </goals>
                        </execution>
                        <execution>
                            <id>report-aggregate</id>
                            <phase>verify</phase>
                            <goals>
                                <goal>report-aggregate</goal>
                            </goals>
                            <configuration>
                                <dataFileIncludes>
                                    <dataFileInclude>**/jacoco.exec</dataFileInclude>
                                </dataFileIncludes>
                                <outputDirectory>${project.reporting.outputDirectory}/jacoco-aggregate</outputDirectory>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>

    1. run mvn clean install
    0 讨论(0)
  • 2020-12-30 02:20

    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.

    0 讨论(0)
  • 2020-12-30 02:23

    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.

    0 讨论(0)
  • 2020-12-30 02:28

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

    0 讨论(0)
  • 2020-12-30 02:33

    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.

    0 讨论(0)
提交回复
热议问题