From 1f997a18c61d7eb4fe8b9af1ee828893ec70929b Mon Sep 17 00:00:00 2001
From: Pierre Smeyers <pierre.smeyers@gmail.com>
Date: Sun, 25 Aug 2024 17:33:14 +0200
Subject: [PATCH] feat(JaCoCo): add JaCoCo Coverage Reports integration

---
 README.md                     | 5 +++--
 templates/gitlab-ci-maven.yml | 3 +++
 2 files changed, 6 insertions(+), 2 deletions(-)

diff --git a/README.md b/README.md
index b246a46..9086da6 100644
--- a/README.md
+++ b/README.md
@@ -89,14 +89,15 @@ to compute code coverage during unit tests execution.
 
 In addition it makes the necessary to [integrate code coverage stats into your GitLab project](https://docs.gitlab.com/ee/ci/testing/code_coverage.html#view-code-coverage-results-in-the-mr) (report badge and viewable coverage in merge requests).
 
-If yo want to fix the JaCoCo plugin version or tweak the default configuration, you may have to configure the
+If you want to fix the JaCoCo plugin version or tweak the default configuration, you may have to configure the
 [JaCoCo Maven Plugin](https://www.eclemma.org/jacoco/trunk/doc/maven.html) in your `pom.xml`, but be aware of the
 following:
 
 * do not declare JaCoCo executions for `prepare-agent` and `report` goals as each would run twice during
   unit tests (not necessarily with the expected configuration). If you really need to do so anyway, you'll have to
   override the `$MAVEN_BUILD_ARGS` variable to remove the explicit invocation to JaCoCo goals.
-* make sure the `report` goal computes a CSV report, that is used by the Maven template to compute the global coverage stat.
+* make sure the `report` goal computes a CSV report (that's default behavior), that is used by the Maven template to compute the global coverage stat,
+* make sure the `report` goal computes an XML report (that's default behavior), that is required to support [GitLab code coverage intergation](https://docs.gitlab.com/ee/ci/testing/test_coverage_visualization/jacoco.html).
 
 More info:
 
diff --git a/templates/gitlab-ci-maven.yml b/templates/gitlab-ci-maven.yml
index 6fd4562..e5814d7 100644
--- a/templates/gitlab-ci-maven.yml
+++ b/templates/gitlab-ci-maven.yml
@@ -629,6 +629,9 @@ mvn-build:
     reports:
       junit:
         - "${MAVEN_PROJECT_DIR}/**/target/*-reports/TEST-*.xml"
+      coverage_report:
+        coverage_format: jacoco
+        path: "${MAVEN_PROJECT_DIR}/**/target/site/jacoco/jacoco.xml"
     paths:
       # version may have been altered
       - "${MAVEN_PROJECT_DIR}/**/pom.xml"
-- 
GitLab