maven - Code coverage report using gitlab-ci.yml file

Maven - Code coverage report using gitlab-ci.yml file

To generate a code coverage report using Maven in GitLab CI/CD pipeline, you typically need to configure Maven to run your tests and then integrate a code coverage tool like JaCoCo or Cobertura. Here's a step-by-step guide on how to set this up using a gitlab-ci.yml file:

Step-by-Step Guide

1. Configure Maven for Code Coverage

First, ensure your Maven pom.xml file includes dependencies for testing and code coverage plugin. Here's an example configuration for JaCoCo:

<!-- pom.xml --> <dependencies> <!-- Your project dependencies --> </dependencies> <build> <plugins> <!-- Maven Surefire Plugin for running tests --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.0.0-M5</version> <configuration> <skipTests>false</skipTests> </configuration> </plugin> <!-- JaCoCo Plugin for code coverage --> <plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>0.8.7</version> <executions> <execution> <id>default-prepare-agent</id> <goals> <goal>prepare-agent</goal> </goals> </execution> <execution> <id>default-report</id> <phase>prepare-package</phase> <goals> <goal>report</goal> </goals> </execution> </executions> </plugin> </plugins> </build> 

2. Configure GitLab CI Pipeline (gitlab-ci.yml)

Next, configure your .gitlab-ci.yml file to define stages and jobs for Maven build, test execution, and code coverage report generation:

# .gitlab-ci.yml stages: - build - test - coverage variables: MAVEN_CLI_OPTS: "-s .m2/settings.xml --batch-mode" before_script: - mvn $MAVEN_CLI_OPTS install maven_build: stage: build script: - mvn $MAVEN_CLI_OPTS clean package maven_test: stage: test script: - mvn $MAVEN_CLI_OPTS test maven_coverage: stage: coverage script: - mvn $MAVEN_CLI_OPTS jacoco:report artifacts: paths: - target/site/jacoco/index.html 

Explanation:

  • Stages: Define three stages (build, test, coverage) to execute Maven build, run tests, and generate code coverage report sequentially.

  • Variables: Set MAVEN_CLI_OPTS to specify Maven settings and batch mode.

  • before_script: Executes Maven install before any stage to ensure dependencies are resolved.

  • maven_build: Maven build stage to compile and package your application.

  • maven_test: Maven test stage to execute unit tests.

  • maven_coverage: Maven coverage stage to generate JaCoCo report (jacoco:report). The generated report HTML files are saved as artifacts, which you can download/view in GitLab.

3. Viewing Code Coverage Report

After running the pipeline, navigate to your GitLab project's CI/CD pipelines. In the coverage stage, you will find the artifacts generated by JaCoCo under target/site/jacoco/index.html. Click on the artifact link to view the detailed code coverage report in your browser.

Notes:

  • Configuration: Modify versions and paths according to your project structure and Maven setup.

  • Coverage Tool: Replace JaCoCo with Cobertura or another code coverage tool if preferred, adjusting the Maven plugin configuration accordingly.

  • Artifacts: Ensure target/site/jacoco/index.html is correctly specified in artifacts paths to capture the generated report.

By following these steps, you can integrate Maven with GitLab CI/CD to generate and view code coverage reports for your Java project. Adjust configurations as needed based on your specific project requirements and preferences for code coverage analysis.

Examples

  1. Generate code coverage report in GitLab CI using Maven

    • Description: Setting up GitLab CI to generate a code coverage report using Maven and integrating it into GitLab pipelines.
    • Code:
      stages: - test test: stage: test script: - mvn clean verify artifacts: paths: - target/site/jacoco/ coverage: '/Overall : (\d+(?:\.\d+)?%)$/' 
  2. Integrate Jacoco code coverage with GitLab CI

    • Description: Configuring GitLab CI to integrate Jacoco for generating code coverage reports during build stages using Maven.
    • Code:
      stages: - test test: stage: test script: - mvn clean verify artifacts: paths: - target/site/jacoco/ coverage: '/Overall : (\d+(?:\.\d+)?%)$/' 
  3. Configure Maven and GitLab CI for code coverage

    • Description: Step-by-step configuration to enable Maven and GitLab CI to generate and display code coverage reports.
    • Code:
      stages: - test test: stage: test script: - mvn clean verify artifacts: paths: - target/site/jacoco/ coverage: '/Overall : (\d+(?:\.\d+)?%)$/' 
  4. Generate Cobertura report in GitLab CI with Maven

    • Description: Generating Cobertura code coverage reports in GitLab CI using Maven build configurations.
    • Code:
      stages: - test test: stage: test script: - mvn clean verify artifacts: paths: - target/site/cobertura/ coverage: '/Line coverage: (\d+(?:\.\d+)?%)$/' 
  5. Add SonarQube code coverage to GitLab CI pipeline

    • Description: Integrating SonarQube for code coverage analysis in GitLab CI pipelines using Maven.
    • Code:
      stages: - test - sonarqube test: stage: test script: - mvn clean verify sonarqube: stage: sonarqube image: sonarsource/sonar-scanner-cli script: - sonar-scanner only: - master 
  6. GitLab CI Maven JaCoCo code coverage report

    • Description: Setting up GitLab CI to generate JaCoCo code coverage reports using Maven during CI/CD pipelines.
    • Code:
      stages: - test test: stage: test script: - mvn clean verify artifacts: paths: - target/site/jacoco/ coverage: '/Overall : (\d+(?:\.\d+)?%)$/' 
  7. Configure GitLab CI for Maven test coverage report

    • Description: Configuring GitLab CI to display Maven-generated test coverage reports in the pipeline interface.
    • Code:
      stages: - test test: stage: test script: - mvn clean verify artifacts: paths: - target/site/jacoco/ coverage: '/Overall : (\d+(?:\.\d+)?%)$/' 
  8. Integrate Maven and GitLab CI for test coverage

    • Description: Integrating Maven with GitLab CI to automate the generation of test coverage reports during CI/CD processes.
    • Code:
      stages: - test test: stage: test script: - mvn clean verify artifacts: paths: - target/site/jacoco/ coverage: '/Overall : (\d+(?:\.\d+)?%)$/' 
  9. GitLab CI pipeline for Maven code coverage

    • Description: Creating a GitLab CI pipeline configuration to include Maven-generated code coverage reports as artifacts.
    • Code:
      stages: - test test: stage: test script: - mvn clean verify artifacts: paths: - target/site/jacoco/ coverage: '/Overall : (\d+(?:\.\d+)?%)$/' 
  10. Setup Maven Surefire and GitLab CI for coverage report

    • Description: Setting up Maven Surefire plugin with GitLab CI to generate and display comprehensive test coverage reports.
    • Code:
      stages: - test test: stage: test script: - mvn clean verify artifacts: paths: - target/site/jacoco/ coverage: '/Overall : (\d+(?:\.\d+)?%)$/' 

More Tags

ignore-case homekit vue-component vcf-vcard device-admin system.net.mail multiple-input https ecmascript-5 client-side

More Programming Questions

More Mixtures and solutions Calculators

More Statistics Calculators

More Animal pregnancy Calculators

More Pregnancy Calculators