Giter Site home page Giter Site logo

monorepo-maven's Introduction

Multi-module Apache Maven example

This project imports JaCoCo's aggregate XML report to be able to report coverage across modules as well as unit test coverage inside the module.

For a basic example see basic maven project.

Usage

  • Build the project, execute all the tests and analyze the project with SonarQube Scanner for Maven:
        mvn clean verify sonar:sonar

Description

This project consists of 3 modules.

  • module1 and module2 contain "business logic" and related unit tests.

  • tests module contains integration tests which test functionality using both modules. tests module is also the one which creates the aggregate coverage report imported into SonarQube.

To generate the report we configure the JaCoCo plugin to attach its agent to the JVM which is executing the tests in the top level pom.

This configuration is done in the <pluginManagment> section, so it will be applied on every submodule.

It is also configured inside the coverage profile, so this can be activated as needed (e.g. only in CI pipeline).

<build>
  <pluginManagement>
    <plugins>
      <plugin>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <executions>
          <execution>
            <goals>
              <goal>prepare-agent</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </pluginManagement>
</build>

Once we have configured JaCoCo to collect coverage data, we need to generate the XML coverage report to be imported into SonarQube.

We will use report-aggregate goal which collects data from all modules dependent on the tests module.

To achieve this we configure the JaCoCo plugin by configuring execution of report-aggregate goal in verify phase.

See pom.xml

<build>
  <plugins>
    <plugin>
      <groupId>org.jacoco</groupId>
      <artifactId>jacoco-maven-plugin</artifactId>
      <executions>
        <execution>
          <id>report</id>
          <goals>
            <goal>report-aggregate</goal>
          </goals>
          <phase>verify</phase>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

This will create a report in tests/target/site/jacoco-aggregate/jacoco.xml. To import this report we will set sonar.coverage.jacoco.xmlReportPaths property with the ${maven.multiModuleProjectDirectory} so every module knows where the coverage should be imported from

<properties>
  <sonar.coverage.jacoco.xmlReportPaths>${maven.multiModuleProjectDirectory}/tests/target/site/jacoco-aggregate/jacoco.xml</sonar.coverage.jacoco.xmlReportPaths>
</properties>

Alternately we can set this property on the command line with the -D switch:

mvn -Dsonar.coverage.jacoco.xmlReportPaths=C:\projects\sonar-scanning-examples\sonarscanner-maven-aggregate\tests\target\site\jacoco-aggregate\jacoco.xml clean verify sonar:sonar 

We have to use an absolute path, because the report will be imported for each module separately and the path is resolved relative to the module dir.

Documentation

SonarScanner for Maven

monorepo-maven's People

Contributors

joe-tingsanchali-sonarsource avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.