Using Maven and JaCoCo for Consolidated Multi-Module Code Coverage Reports

Play Voice
December 1, 2023
Technology

The JaCoCo plugin for Maven only reports code coverage for tests run in a particular module rather than reporting coverage across modules. This can be an issue for multi-module projects where unit tests from one module test code in other modules. Some projects are set up with unit tests in a separate module all by themselves, making the JaCoCo code coverage reports nearly useless. No matter what setup you run, JaCoCo won’t report code coverage well for integration tests that run across modules.There are a number of possible solutions to address this challenge. One of the most obvious solutions is to avoid having a separate module for unit tests, rather including unit tests in the appropriate module. That probably won’t get you the whole way and may be a significant refactoring project.Maven users can also create a separate reporting module with dependencies. Use this module to create dependencies to all the modules you need for your coverage report. Make sure the module has access to the right files, and accurate location paths are included.You can use an additional tool to make up for this deficit. Some possible solutions are SonarQube, Jenkins, and Arquillian.Since the Maven JaCoCo plugin doesn’t support merging of subproject metrics into a single report file, a possible workaround solution is to use Ant with Maven. The task objectives with this approach can be summarized as follows:

  • Add JaCoCo plugin to the Parent POM. If specific modules don’t have to be included in the test coverage, change the plugin definition based on specific projects as required.
  • Create a submodule to append all results from JaCoCo using the Ant task.
  • Specify the Ant command from Maven using the Maven Antrun plugin.
  • Configure the JaCoCo Ant task configuration process with the necessary information such as location of build, class, source or generated-source directories.
  • From the Antrun plugin, define the reporting tasks. Specify locations to store the JaCoCo report execution data. This information can be covered for each submodule within each project.
  • Create a reporting structure based on requirements, such that each group of elements will contain information only on the specific set of modules.

Hopefully, this helps you find some solutions (or at least workarounds) to the multi-module code coverage reporting when using Maven and JaCoCo. By the end of the day, while testing teams like to aim for 100 percent test coverage, it’s also important to measure it and avoid unnecessary testing. This concern is even more critical for handling multiple modules in test coverage reporting. Make sure your Maven tool handles all the necessary conditions and paths nicely, thereby accurately ensuring 100 percent test coverage across multi-module projects.

Conclusion

Have a specific concern bothering you?

Try our complimentary 2-week POV engagement
I have read and accept the Privacy Policy
Our Latest Blogs
How to Build a Risk Management Platform for Payment Gateways Like Stripe [with example client case study]
Read More >
How is AI in DevOps Transforming Software Development
Read More >
Top DevOps Tools You Need to Streamline Your Workflow in 2024
Read More >

About The Author

Harsh Raval

Speak to our Experts
Lets Talk

Our Latest Blogs

November 20, 2024

How to Build a Risk Management Platform for Payment Gateways Like Stripe [with example client case study]

Read More →
October 28, 2024

How is AI in DevOps Transforming Software Development

Read More →
October 23, 2024

Top DevOps Tools You Need to Streamline Your Workflow in 2024

Read More →