Wednesday, September 5, 2018

Jacoco multiple module (aggregate-report )

lot many links were discussing about the consolidated report for jacoco tool...

after reading all the links , i'll be posting working example..


parent
  - submodule1
     - unit test for submodule1
  - submodule2
     - unit test for submodule2
  - submodule3
     - unit test for submodule3
  - parentpom file


goal is, need a report where I can see code coverage  report for all the modules (submodule1,submodule2,submodule3 etc)

in order to achieve that, we need to create one more submodule(say report)


parent

- report

 -reportpom file

- submodule1
   - unit test for submodule1
- submodule2
   - unit test for submodule2
- submodule3
   - unit test for submodule3
- parentpom file


parent pom file should have below

     <plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.8.2</version>
    <executions>
        <execution>
            <id>pre-unit-test</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
            <configuration>
                <destFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</destFile>
                <propertyName>surefireArgLine</propertyName>
            </configuration>
        </execution>

    </executions>
</plugin>


since parent pom is inherited in all the modules,all the modules have "coverage-reports/jacoco-ut.exec" 
when you run "test" lifecycle.

ok, now we need to collect all report and make it consolidated.

well, reportpom file does the magic.. all you need to do is, do the following in "reportpom" file 

Step1: add all the modules on which you need to collect the result
<dependencies>
    <dependency>
        <groupId>${project.groupId}</groupId>
        <artifactId>submodule1</artifactId>
        <version>${project.version}</version>
    </dependency>
    <dependency>
        <groupId>${project.groupId}</groupId>
        <artifactId>submodule2</artifactId>
        <version>${project.version}</version>
    </dependency>
    <dependency>
        <groupId>${project.groupId}</groupId>
        <artifactId>submodule3</artifactId>
        <version>${project.version}</version>
    </dependency>
</dependencies>

Step2: add report-aggregate, do the following
<build>
    <plugins>
        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.8.2</version>
            <executions>
                <execution>
                    <id>report-aggregate</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>report-aggregate</goal>
                    </goals>
                    <configuration>
                        <dataFileIncludes>**/jacoco-ut.exec</dataFileIncludes>
                        <outputDirectory>${project.reporting.outputDirectory}/jacoco-aggregate</outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

That's all, now you can run mvn commands.
  $  mvn package 

you have to run mvn package because, mvn test will only create test results, once the test is done, that needs to be aggregated. hence we need to run that command to consolidate.