How to add an Angular module built with Maven to a SonarQube analysis

featured image

We can use SonarQube to analyse all or only selected modules from our multi-module application built with Maven. Let’s configure an existing Angular module so that we can see a full SonarQube analysis for an example SpringBoot-Angular project.


As an example application, we’re going to use the efficient-mvp-example. The project consists of two modules: backend and frontend that are built together with Maven. To learn how to set up a similar application read the Integrate Angular with a Spring Boot project post.

I use SonarQube to analyse code and I run it in a docker container locally. To learn how to set up SonarQube on development environment read the Boost project quality with SonarQube – local code analysis post.

Furthermore, I use Maven to built the multi-module application and to run SonarQube analyses.

The starting point for this article is in the 6ed4becccd2e58200cfea3757455f07a04cd6d75 commit.

The work presented in this article is in the 6c7f7628d84ae401a5b3434ea4b901fc1788d29e commit.

The goal

We want to be able to run a SonarQube analysis with Maven for the module that uses Spring Boot, the module that uses Angular and for the project as a whole. You can see our objective on the image below:

Angular module in SonarQube analysis

Update the existing configuration

We already have a SonarQube instance running in a docker container where it can perform a code analysis for the backend module. Before we can achieve the same for the frontend module we need to change the existing config.

Update the parent pom

To the parent pom.xml file add the following properties:

As a result, we’ll be able to see Efficient MVP example on the list of SonarQube projects once we complete the remaining steps.

Update the backend pom

In the pom.xml for the backend module update the <sonar.projectKey> value and add a property for the project name:

As a result, we’ll be able to see Efficient MVP example (backend) on the list of SonarQube projects once we complete the remaining steps.

Prepare the Angular module for a SonarQube analysis

We’re going to specify properties, profiles and test configuration necessary to analyse our frontend module.

Add properties for sonar

In the pom.xml for the frontend module add the following properties:

Below you’ll find a short explanation for some of the tags used in the presented Maven config:

  • <sonar.sources> – in my application I generated the Angular project in the frontend/src/main directory as you can see on the image below. As I want SonarQube to run an analysis on almost all Angular code, I’m giving the path to the main folder. I also used this path to specify the value for the <sonar.tests> tag.
Angular directory tree that is used in SonarQube analysis configuration
  • <sonar.javascript.lcov.reportPaths> – we want to have a test coverage report in our SonarQube analysis for Angular. Therefore, as we can read in the SonarQube docs for test coverage, for typescript/javascript coverage we need to specify:

Comma-delimited list of paths to LCOV coverage report files. Paths may be absolute or relative to project root.
  • <sonar.coverage.exclusions> – remember to put here paths to all parts of the Angular app that you don’t want to see in the Coverage Report.
  • <sonar.exclusions> – remember to put here paths to all parts of the Angular app that you don’t want to analyse at all.

Add profiles for sonar

In order to have some flexibility with the frontend analysis, we’re going to add two profiles to our frontend/pom.xml file:

  • frontend-pre-sonar – for building the Angular project with frontend-maven-plugin and running tests;
  • frontend-sonar – for only running an analysis.

Add the following profiles to the frontend/pom.xml file:

In the frontend-pre-sonar profile we are calling the pre-sonar script:

Therefore, we need to actually provide this script in the package.json file:

As a result, Maven will not only build the Angular project but also execute all tests for it (if you call it with the frontend-pre-sonar profile).

Karma configuration

Building the Angular module and running tests for it is not enough to produce a complete SonarQube analysis. To see the actual test coverage we need a test report saved in the location that we gave in the <sonar.javascript.lcov.reportPaths> property.

Frist, install the karma-sonarqube-unit-reporter dependency (remember to execute this command in the folder with the package.json file):

Next, edit the karma.conf.js file to:

  • add the newly installed plugin,
  • adjust the dir for the coverageReporter (src/main/angular/coverage/ given in the pom.xml file corresponds to ./coverage in karma.cong.js)
  • add a new reporter type.

Given these points, you can see the changes I made in my config file on the snippet below:

Use SonarQube for a multi-module project

The commands below contain credentials required for my SonarQube instance. Remember to adjust them when running the mvn commands on your machine.

Run a SonarQube analysis for all configured modules

In this case, we’re going to use the frontend-pre-sonar profile to build and test the Angular project and the code-coverage profile to test the Spring Boot project:

Run a SonarQube analysis only for frontend (Angular)

In order to analyze solely the frontend module, we’re going to use only the frontend-pre-sonar profile to build and test the Angular project:

Run a SonarQube analysis only for backend (Spring Boot)

For analyzing the backend module, we’re going to use only the code-coverage profile to test the Spring Boot project:

A convenience script for running SonarQube analyses in multi-module project

Below you’ll find my convenience shell script that I find useful in calling just the analysis I need. I called it Remember to adjust the credentials for SonarQube if you want to use it:

Make sure that the file is executable and you can run analysis with the following options:

  • ./ -a – for all modules
  • ./ -b – for the backend module
  • ./ -f – for the frontend module


SonarQube does not show proper code coverage % for the Angular project

Make sure that when you run the mvn command, you:

  • called the clean build phase to remove old reports,
  • Maven actually builds the whole frontend module (calling the verify build phase),
  • added the fronted-pre-sonar profile to the frontend/pom file and the pre-sonar script to the package.json file,
  • called the command with the -Pfrontend-pre-sonar argument,
  • configured Karma so that the file is created in the coverage directory:
Test coverage report for Angular that will be used by SonarQube to show code coverage

Photo by Monstera from Pexels

Leave a Reply

Your email address will not be published. Required fields are marked *