DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Using Python Libraries in Java
  • DGS GraphQL and Spring Boot
  • How to Build a New API Quickly Using Spring Boot and Maven
  • Configurable Feign Client Retry With Reusable Library and DRY

Trending

  • Measuring the Impact of AI on Software Engineering Productivity
  • Understanding IEEE 802.11(Wi-Fi) Encryption and Authentication: Write Your Own Custom Packet Sniffer
  • Agentic AI for Automated Application Security and Vulnerability Management
  • Cookies Revisited: A Networking Solution for Third-Party Cookies
  1. DZone
  2. Coding
  3. Java
  4. JaCoCo in Maven Multi-Module Projects

JaCoCo in Maven Multi-Module Projects

By 
Alex Soto user avatar
Alex Soto
·
Aug. 13, 12 · Interview
Likes (4)
Comment
Save
Tweet
Share
80.9K Views

Join the DZone community and get the full member experience.

Join For Free
Code coverage is an important measurement used during our development that describes the degree to which source code is tested.

In this post I am going to explain how to run code coverage using Maven and JaCoCo plugins in multi-module projects.

JaCoCo is a code coverage library for Java, which has been created by the EclEmma team. It has a plugin for Eclipse, and can be run with Ant and Maven too.

Now we will focus only on a Maven approach.

In a project with only one module is as easy as registering a build plugin:
<plugin>
	<groupId>org.jacoco</groupId>
	<artifactId>jacoco-maven-plugin</artifactId>
	<version>0.5.7.201204190339</version>
	<executions>
		<execution>
			<goals>
				<goal>prepare-agent</goal>
			</goals>
		</execution>
	        <execution>
	                <id>report</id>
		        <phase>prepare-package</phase>
		        <goals>
			       <goal>report</goal>
		        </goals>
	       </execution>
	</executions>
</plugin>
And now running mvn package in site/jacoco directory, a coverage report will be present in different formats.



But with multimodule projects a new problem arises. How do we merge the metrics of all subprojects into only one file so we can have a quick overview of all subprojects? For now the Maven JaCoCo Plugin does not support it.

There are many alternatives and I am going to cite the most common:

  • Sonar. It has the disadvantage that you need to install Sonar (maybe you are already using it, but maybe not).

  • Jenkins. The plugin for JaCoCo is still under development. Moreover you need to run a build job to inspect your coverage. This is good in terms of continuous integration but could be a problem if you are trying to "catch" some piece of code that have not been covered with previously implemented tests.

  • Arquillian JaCoCo Extension. Arquillian is a container test framework that has an extension which during test execution can capture the coverage. It's a good option if you are using Arquillian. The disadvantage is that maybe your project does not require a container.

  • Ant. You can use an Ant task with Maven. JaCoCo Ant tasks can merge results from multiple JaCoCo file results. Note that this is the most generic solution, and this is the chosen approach that we are going to use.

The first thing to do is add a JaCoCo plugin to the parent pom so all projects could generate a coverage report. Of course, if there are modules which do not require coverage, the plugin definition should be changed from parent pom to specific projects.
<plugin>
	<groupId>org.jacoco</groupId>
	<artifactId>jacoco-maven-plugin</artifactId>
	<version>0.5.7.201204190339</version>
	<executions>
		<execution>
			<goals>
				<goal>prepare-agent</goal>
			</goals>
		</execution>
	        <execution>
	                <id>report</id>
		        <phase>prepare-package</phase>
		        <goals>
			       <goal>report</goal>
		        </goals>
	       </execution>
	</executions>
</plugin>


The next step is creating a specific submodule for appending all results of the JaCoCo plugin by using an Ant task. I suggest using something like project-name-coverage.

Then let's open generated pom.xml and we are going to insert the required plugins to join all coverage information. To append them.  As we have already written we are going to use a JaCoCo Ant task which has the ability to open all JaCoCo output files and append all their content into one. So the first thing to do is download the jar which contains the JaCoCo Ant task. To automate the download process, we are going to use maven dependency plugin:
<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-dependency-plugin</artifactId>
		<executions>
		<!-- Copy the ant tasks jar. Needed for ts.jacoco.report-ant . -->
			<execution>
				<id>jacoco-dependency-ant</id>
					<goals>
						<goal>copy</goal>
					</goals>
				<phase>process-test-resources</phase>
				<inherited>false</inherited>
				<configuration>
					<artifactItems>
						<artifactItem>
							<groupId>org.jacoco</groupId>
							<artifactId>org.jacoco.ant</artifactId>
							<version>${jacoco.version}</version>
						</artifactItem>
					</artifactItems>
					<stripVersion>true</stripVersion>
					<outputDirectory>${basedir}/target/jacoco-jars</outputDirectory>
				</configuration>
			</execution>
		</executions>
</plugin>
During process-test-resources phase Jacoco Ant artifact will be downloaded and copied to the target directory so it can be registered into the pom without worrying about the jar location.

We also need a way to handle Ant tasks from Maven. And this is as simple as using maven antrun plugin, which you can specify any ant command in its configuration section. See next simple example:
<plugin>
	<groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-antrun-plugin</artifactId>
    	<version>1.6</version>
     <executions>
     		<execution>
          		<phase>compile</phase>
               	<goals>
               		<goal>run</goal>
               	</goals>
               	<configuration>
               		<target>
                     <!-- Execute an ant task within maven -->
                     	<echo message="Hello World from pom.xml"/>
                    	</target>
              	</configuration>
         	</execution>
     	</executions>
</plugin>
Notice that we can specify any Ant task in the target tag. And now we are ready to start configuring the JaCoCo Ant task.  The JaCoCo report plugin requires you set the location of the build directory, class directory, source directory or generated-source directory. For this purpose we are going set them as properties.

<build.directory.projecta>../projectA/target</build.directory.projecta>
<build.directory.projectb>../projectB/target</build.directory.projectb>

<classes.directory.projecta>../projectA/target/classes</classes.directory.projecta>
<classes.directory.projectb>../projectB/target/classes</classes.directory.projectb>

<sources.directory.projecta>../projectA/src/main/java</sources.directory.projecta>
<sources.directory.projectb>../projectB/src/main/java</sources.directory.projectb>

<generated-sources.directory.projecta>../projectA/target/generated-sources/annotations</generated-sources.directory.projecta>
<generated-sources.directory.projectb>../projectB/target/generated-sources/annotations</generated-sources.directory.projectb>
And now the Ant task part which will go into target tag of the antrun plugin.

First we need to define report task.
<taskdef name="report" classname="org.jacoco.ant.ReportTask">
	<classpath path="${basedir}/target/jacoco-jars/org.jacoco.ant.jar" />
</taskdef>
Do you see that org.jacoco.ant.jar file is downloaded by the dependency plugin?  You don't need to worry about copying it manually.

Then we are going to call report task as defined in taskdef section.
<report>
	<executiondata>
		<fileset dir="${build.directory.projecta}">
			<include name="jacoco.exec" />
		</fileset>
		<fileset dir="${build.directory.projectb}">
			<include name="jacoco.exec" />
		</fileset>
	</executiondata>
	<structure name="JaCoCo-Multi Project">
		<group name="JaCoCo-Multi">
			<classfiles>
				<fileset dir="${classes.directory.projecta}" />
				<fileset
					dir="${classes.directory.projectb}" />
			</classfiles>
			<sourcefiles encoding="UTF-8">
				<fileset dir="${sources.directory.projecta}" />
				<fileset
					dir="${sources.directory.projectb}"></fileset>
				<fileset
					dir="${generated-sources.directory.projecta}"></fileset>
				<fileset
					dir="${generated-sources.directory.projectb}"></fileset>
			</sourcefiles>
		</group>
	</structure>
	<html destdir="${basedir}/target/coverage-report/html" />
	<xml destfile="${basedir}/target/coverage-report/coverage-report.xml" />
	<csv destfile="${basedir}/target/coverage-report/coverage-report.csv" />
</report>
Within the executiondata element, we specify locations where JaCoCo execution data files are stored. By default this is the target directory, and for each project we need to add one entry for each submodule.

The next element is structure. This element defines the report structure, and can be defined with a hierarchy of group elements. Each group  should contain class files and source files of all projects that belongs to that group. In our example only one group is used.

And finally we are setting output format using html, xml and csv tags.

Complete Code:
<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-antrun-plugin</artifactId>
	<version>1.6</version>
	<executions>
		<execution>
			<phase>post-integration-test</phase>
			<goals>
				<goal>run</goal>
			</goals>
			<configuration>
				<target>
				<!-- Execute an ant task within maven -->
					<echo message="Generating JaCoCo Reports" />
					<taskdef name="report" classname="org.jacoco.ant.ReportTask">
						<classpath path="${basedir}/target/jacoco-jars/org.jacoco.ant.jar" />
					</taskdef>
					<mkdir dir="${basedir}/target/coverage-report" />
					<report>
						<executiondata>
							<fileset dir="${build.directory.projecta}">
								<include name="jacoco.exec" />
							</fileset>
							<fileset dir="${build.directory.projectb}">
								<include name="jacoco.exec" />
							</fileset>
						</executiondata>
						<structure name="jacoco-multi Coverage Project">
							<group name="jacoco-multi">
								<classfiles>
									<fileset dir="${classes.directory.projecta}" />
									<fileset dir="${classes.directory.projectb}" />
								</classfiles>
								<sourcefiles encoding="UTF-8">
									<fileset dir="${sources.directory.projecta}" />
									<fileset dir="${sources.directory.projectb}"></fileset>
                                                                </sourcefiles>
							</group>
						</structure>
						<html destdir="${basedir}/target/coverage-report/html" />
						<xml destfile="${basedir}/target/coverage-report/coverage-report.xml" />
						<csv destfile="${basedir}/target/coverage-report/coverage-report.csv" />
					</report>
				</target>
			</configuration>
		</execution>
	</executions>
	<dependencies>
		<dependency>
			<groupId>org.jacoco</groupId>
			<artifactId>org.jacoco.ant</artifactId>
			<version>${jacoco.version}</version>
		</dependency>
	</dependencies>
</plugin>
And now simply run mvn clean verify and in my-project-coverage/target/coverage-report, a report with code coverage of all projects will be presented.

Hope you find this post useful.

We Keep Learning,
Alex.
Apache Maven

Published at DZone with permission of Alex Soto, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Using Python Libraries in Java
  • DGS GraphQL and Spring Boot
  • How to Build a New API Quickly Using Spring Boot and Maven
  • Configurable Feign Client Retry With Reusable Library and DRY

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!