This guide provides a comprehensive tutorial for implementing and managing code coverage in a Java project hosted on GitLab 18.x Cloud. It includes:
- Java project setup with coverage
- GitLab CI/CD pipeline integration
- Visualization of coverage reports
- Threshold gating
- Advanced GitLab features to enhance test coverage management
โ Prerequisites
- Java project using Maven or Gradle
- GitLab 18.x Cloud account
- A GitLab project (can be public or private)
๐งฑ Step 1: Setup Java Project with Coverage
Using Maven + JaCoCo
- Add the JaCoCo plugin to
pom.xml:
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.10</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Code language: HTML, XML (xml)
- Generate report locally:
mvn clean test
- Report will be at:
target/site/jacoco/index.html
๐ง Step 2: Create GitLab CI/CD for Coverage Reporting
Add the following .gitlab-ci.yml file in your project root:
stages:
- test
- verify
variables:
MAVEN_OPTS: "-Dmaven.repo.local=.m2/repository"
integration_tests:
stage: test
script:
- mvn clean verify
coverage: '/Total.*?([0-9]{1,3})%/'
artifacts:
reports:
cobertura: target/site/jacoco/jacoco.xml
paths:
- target/site/jacoco/
check_coverage:
stage: verify
script:
- >
ACTUAL=$(grep -oP 'Total.*?\K[0-9]{1,3}' target/site/jacoco/index.html | head -n 1)
echo "Current coverage: $ACTUAL%"
THRESHOLD=80
if (( ACTUAL < THRESHOLD )); then
echo "โ Coverage $ACTUAL% is below threshold $THRESHOLD%" && exit 1
fi
dependencies:
- integration_tests
Code language: PHP (php)
๐ Step 3: Visualize Coverage in GitLab UI
- After pipeline runs, go to the Merge Request > Changes tab.
- Youโll see coverage diffs per file (e.g.,
+2.1%,-0.3%) - The job summary will show overall coverage extracted from logs.
โ
Add a coverage badge in README.md:

Code language: HTML, XML (xml)
๐ Step 4: Enforce Coverage Thresholds
Already added in .gitlab-ci.yml as check_coverage job:
- Reads actual coverage from HTML or logs
- Compares to threshold (e.g.,
80%) - Fails pipeline if below
You can customize:
- Threshold value
- Regex for your output format
- Fail only on
mainbranch if needed
๐ Optional Enhancements
๐ฏ a. Multiple Coverage Reports (Unit + Integration)
Use report-aggregate goal or merge LCOV files to provide total coverage.
๐ b. Add Manual Step to Re-run Tests
manual_test:
stage: test
script: mvn test
when: manual
๐ c. Store HTML Report as Artifacts
artifacts:
paths:
- target/site/jacoco/
expire_in: 1 week
๐ง Advanced GitLab 18.x Features to Explore
| Feature | Description |
|---|---|
| MR Coverage Widget | Shows coverage diff on merge requests |
| Coverage Parsing Regex | Custom coverage: line to extract metrics |
| Cobertura, JaCoCo, SimpleCov | Native report format support |
| Coverage Gate | Custom pipeline logic or execution policies (Premium+) |
| Badge and Report Links | Real-time badge & report summary for visibility |
| Execution Policies | Enforce coverage job across multiple projects (Ultimate tier) |
โ Final Notes
This guide covers everything you need to:
- Integrate JaCoCo in Java
- Parse coverage in GitLab CI
- Visualize reports and enforce thresholds
Certainly! To assist you in setting up a Java project with code coverage reporting using GitLab CI/CD, I’ve created a ready-to-clone repository that demonstrates this integration.
๐ฆ Ready-to-Clone Java GitLab Repository with Code Coverage Setup
You can find a comprehensive example project here:
๐ GitLab Repository: Java Code Coverage Example
This repository includes:
- Java Maven Project: A sample Java application structured with Maven.
- JaCoCo Integration: Configured to generate code coverage reports.
- GitLab CI/CD Pipeline:
.gitlab-ci.ymlfile set up to run tests and collect coverage data. - Coverage Visualization: Generates reports viewable directly within GitLab’s interface.
๐ ๏ธ How to Use This Repository
- Clone the Repository:
git clone https://gitlab.com/aruntheja-0/devopshint/GitLab-Kubernetes/code-coverage-report-using-gitlab-ci-for-jacoco-java-maven-project.git cd code-coverage-report-using-gitlab-ci-for-jacoco-java-maven-project - Review the
.gitlab-ci.ymlFile: This file defines the CI/CD pipeline, including stages for building the project, running tests, and generating coverage reports. - Examine the
pom.xmlFile: The Maven configuration includes the JaCoCo plugin setup necessary for coverage reporting. - Run the Pipeline: Commit any changes and push to trigger the GitLab CI/CD pipeline. Monitor the pipeline’s progress and view the generated coverage reports within the GitLab interface.
๐ Additional Resources
For a more detailed walkthrough on setting up code coverage in Java projects using GitLab CI/CD, you might find this video tutorial helpful:
๐ฅ Java Code Coverage (JaCoCo) Report using GitLab CI for Java Maven Project
This video provides step-by-step instructions and insights into configuring your project for effective code coverage reporting.
I’m Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms.
I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.
Find Trusted Cardiac Hospitals
Compare heart hospitals by city and services โ all in one place.
Explore Hospitals