Find the Best Cosmetic Hospitals

Explore trusted cosmetic hospitals and make a confident choice for your transformation.

โ€œInvest in yourself โ€” your confidence is always worth it.โ€

Explore Cosmetic Hospitals

Start your journey today โ€” compare options in one place.

Gitlab – Code Coverage in Java with GitLab – Complete Guide

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

  1. 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)
  1. 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

  1. After pipeline runs, go to the Merge Request > Changes tab.
  2. Youโ€™ll see coverage diffs per file (e.g., +2.1%, -0.3%)
  3. The job summary will show overall coverage extracted from logs.

โœ… Add a coverage badge in README.md:

![coverage report](https://gitlab.com/<group>/<project>/badges/main/coverage.svg)
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 main branch 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

FeatureDescription
MR Coverage WidgetShows coverage diff on merge requests
Coverage Parsing RegexCustom coverage: line to extract metrics
Cobertura, JaCoCo, SimpleCovNative report format support
Coverage GateCustom pipeline logic or execution policies (Premium+)
Badge and Report LinksReal-time badge & report summary for visibility
Execution PoliciesEnforce 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
Java Code Coverage(JaCoCo) Report using GitLab CI for Java Maven ...

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.yml file set up to run tests and collect coverage data.
  • Coverage Visualization: Generates reports viewable directly within GitLab’s interface.

๐Ÿ› ๏ธ How to Use This Repository

  1. 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
  2. Review the .gitlab-ci.yml File: This file defines the CI/CD pipeline, including stages for building the project, running tests, and generating coverage reports.
  3. Examine the pom.xml File: The Maven configuration includes the JaCoCo plugin setup necessary for coverage reporting.
  4. 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.


Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services โ€” all in one place.

Explore Hospitals
Iโ€™m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I have worked at <a href="https://www.cotocus.com/">Cotocus</a>. I share tech blog at <a href="https://www.devopsschool.com/">DevOps School</a>, travel stories at <a href="https://www.holidaylandmark.com/">Holiday Landmark</a>, stock market tips at <a href="https://www.stocksmantra.in/">Stocks Mantra</a>, health and fitness guidance at <a href="https://www.mymedicplus.com/">My Medic Plus</a>, product reviews at <a href="https://www.truereviewnow.com/">TrueReviewNow</a> , and SEO strategies at <a href="https://www.wizbrand.com/">Wizbrand.</a> Do you want to learn <a href="https://www.quantumuting.com/">Quantum Computing</a>? <strong>Please find my social handles as below;</strong> <a href="https://www.rajeshkumar.xyz/">Rajesh Kumar Personal Website</a> <a href="https://www.youtube.com/TheDevOpsSchool">Rajesh Kumar at YOUTUBE</a> <a href="https://www.instagram.com/rajeshkumarin">Rajesh Kumar at INSTAGRAM</a> <a href="https://x.com/RajeshKumarIn">Rajesh Kumar at X</a> <a href="https://www.facebook.com/RajeshKumarLog">Rajesh Kumar at FACEBOOK</a> <a href="https://www.linkedin.com/in/rajeshkumarin/">Rajesh Kumar at LINKEDIN</a> <a href="https://www.wizbrand.com/rajeshkumar">Rajesh Kumar at WIZBRAND</a> <a href="https://www.rajeshkumar.xyz/dailylogs">Rajesh Kumar DailyLogs</a>

Related Posts

Top 10 Subscription Management Software Tools in 2026: Features, Pros, Cons & Comparison

Introduction Subscription management software is designed to streamline and optimize the process of managing recurring billing, customer subscriptions, and related business operations. In 2026, with the rapid…

Read More

Top 10 AI Data Integration Tools in 2026: Features, Pros, Cons & Comparison

Introduction In 2026, AI data integration tools are pivotal for businesses navigating the complexities of modern data ecosystems. These tools combine artificial intelligence with data integration processes…

Read More

Top 10 Fleet Management Tools in 2026: Features, Pros, Cons & Comparison

Introduction In 2026, the logistics and transportation industries are evolving rapidly, and managing a fleet of vehicles has never been more complex. Fleet management software has become…

Read More

Top 10 AI Academic Plagiarism Checkers Tools in 2026: Features, Pros, Cons & Comparison

Introduction In 2026, AI academic plagiarism checkers have become indispensable tools for students, educators, researchers, and institutions striving to uphold academic integrity. With the rise of AI-generated…

Read More

Top 10 Travel Management Software Tools in 2026: Features, Pros, Cons & Comparison

Introduction In 2026, travel management software (TMS) has become a crucial tool for businesses, travel agencies, and frequent travelers. These tools automate the booking, tracking, and management…

Read More

Top 10 No-Code Platforms Tools in 2026: Features, Pros, Cons & Comparison

Introduction In 2026, no-code platforms have become essential for businesses and individuals looking to build powerful applications, websites, and automations without the need for programming knowledge. These…

Read More
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
0
Would love your thoughts, please comment.x
()
x