# Jenkins-SonarQube Integration

In 
Published 2022-12-03

This tutorial explains to you how to integrate Jenkins and SonarQube.

# Prerequisites

  • Maven is installed and integrated with Jenkins.

For more information you can consult Maven Integration plugin.

  • We have a Spring Boot application which use Maven

It is better to have a Spring Boot application with some JUnit tests. You can take a look at Junit5 Example (with Maven).

  • SonarQube is installed and you have access to it

In my case I installed SonarQube on my local machine. You can take a look at the following tutorials: SonarQube Installation on Windows and Create a new Project.

# Jenkins-SonarQube integration steps

For sending SonarQube analyses from Jenkins to SonarQube, we need to add a new build step named "Invoke top-level Maven targets", choose Maven installation and add the Maven goals. You can add any goals, but we need sonar:sonar in order to use SonarQube in this step.

Click on "Advanced..." button and some new fields will appear:

Here we need to set the pom.xml file and the properties which are set to maven command.

In my case I used :

sonar.projectKey=MyProject-1
sonar.host.url=http://127.0.0.1:9000
sonar.login=sqp_fbc278b701f40bc801e95056e1ed602ccb99d698

These values are taken from the creation of the SonarQube project.

Now we can save the changes and run the job again.

In the Console Output (at the end of the logs) we can see something like this:

[INFO] --- sonar-maven-plugin:3.9.1.2184:sonar (default-cli) @ rest-demo ---
[INFO] User cache: C:\WINDOWS\system32\config\systemprofile\.sonar\cache
[INFO] SonarQube version: 9.8.0.63668
[INFO] Default locale: "en_US", source code encoding: "UTF-8"
[INFO] Load global settings
[INFO] Load global settings (done) | time=71ms
[INFO] Server id: 147B411E-AYXVdl2WhfNkW5ob3fBk
[INFO] User cache: C:\WINDOWS\system32\config\systemprofile\.sonar\cache
[INFO] Load/download plugins
[INFO] Load plugins index
[INFO] Load plugins index (done) | time=43ms
[INFO] Load/download plugins (done) | time=398ms
[INFO] Process project properties
[INFO] Process project properties (done) | time=12ms
[INFO] Execute project builders
[INFO] Execute project builders (done) | time=1ms
[INFO] Project key: MyProject-1
[INFO] Base dir: D:\GitHub-Projects\Projects\Spring-Boot\APIs\SimpleAPI\rest-demo
[INFO] Working dir: D:\GitHub-Projects\Projects\Spring-Boot\APIs\SimpleAPI\rest-demo\target\sonar
[INFO] Load project settings for component key: 'MyProject-1'
[INFO] Load project settings for component key: 'MyProject-1' (done) | time=24ms
[INFO] Auto-configuring with CI 'Jenkins'
[INFO] Load quality profiles
[INFO] Load quality profiles (done) | time=50ms
[INFO] Load active rules
[INFO] Load active rules (done) | time=1459ms
[INFO] Load analysis cache
[INFO] Load analysis cache (228 bytes) | time=11ms
[INFO] Load project repositories
[INFO] Load project repositories (done) | time=21ms
[INFO] Indexing files...
[INFO] Project configuration:
[INFO] 6 files indexed
[INFO] 0 files ignored because of scm ignore settings
[INFO] Quality profile for java: Sonar way
[INFO] Quality profile for xml: Sonar way
[INFO] ------------- Run sensors on module rest-demo
[INFO] Load metrics repository
[INFO] Load metrics repository (done) | time=16ms
[INFO] Sensor JavaSensor [java]
[INFO] Configured Java source version (sonar.java.source): 19
[INFO] JavaClasspath initialization
[INFO] JavaClasspath initialization (done) | time=13ms
[INFO] JavaTestClasspath initialization
[INFO] JavaTestClasspath initialization (done) | time=5ms
[INFO] Server-side caching is enabled. The Java analyzer will not try to leverage data from a previous analysis.
[INFO] Using ECJ batch to parse 4 Main java source files with batch size 427 KB.
[INFO] Starting batch processing.
[INFO] The Java analyzer cannot skip unchanged files in this context. A full analysis is performed for all files.
[INFO] 100% analyzed
[INFO] Batch processing: Done.
[INFO] Did not optimize analysis for any files, performed a full analysis for all 4 files.
[INFO] Using ECJ batch to parse 1 Test java source files with batch size 427 KB.
[INFO] Starting batch processing.
[INFO] 100% analyzed
[INFO] Batch processing: Done.
[INFO] Did not optimize analysis for any files, performed a full analysis for all 1 files.
[INFO] No "Generated" source files to scan.
[INFO] Sensor JavaSensor [java] (done) | time=1047ms
[INFO] Sensor JaCoCo XML Report Importer [jacoco]
[INFO] 'sonar.coverage.jacoco.xmlReportPaths' is not defined. Using default locations: target/site/jacoco/jacoco.xml,target/site/jacoco-it/jacoco.xml,build/reports/jacoco/test/jacocoTestReport.xml
[INFO] No report imported, no coverage information will be imported by JaCoCo XML Report Importer
[INFO] Sensor JaCoCo XML Report Importer [jacoco] (done) | time=2ms
[INFO] Sensor CSS Rules [javascript]
[INFO] No CSS, PHP, HTML or VueJS files are found in the project. CSS analysis is skipped.
[INFO] Sensor CSS Rules [javascript] (done) | time=0ms
[INFO] Sensor C# Project Type Information [csharp]
[INFO] Sensor C# Project Type Information [csharp] (done) | time=1ms
[INFO] Sensor C# Analysis Log [csharp]
[INFO] Sensor C# Analysis Log [csharp] (done) | time=10ms
[INFO] Sensor C# Properties [csharp]
[INFO] Sensor C# Properties [csharp] (done) | time=0ms
[INFO] Sensor SurefireSensor [java]
[INFO] parsing [D:\GitHub-Projects\Projects\Spring-Boot\APIs\SimpleAPI\rest-demo\target\surefire-reports]
[INFO] Sensor SurefireSensor [java] (done) | time=56ms
[INFO] Sensor HTML [web]
[INFO] Sensor HTML [web] (done) | time=2ms
[INFO] Sensor XML Sensor [xml]
[INFO] 1 source file to be analyzed
[INFO] 1/1 source file has been analyzed
[INFO] Sensor XML Sensor [xml] (done) | time=105ms
[INFO] Sensor Text Sensor [text]
[INFO] 6 source files to be analyzed
[INFO] 6/6 source files have been analyzed
[INFO] Sensor Text Sensor [text] (done) | time=8ms
[INFO] Sensor VB.NET Project Type Information [vbnet]
[INFO] Sensor VB.NET Project Type Information [vbnet] (done) | time=1ms
[INFO] Sensor VB.NET Analysis Log [vbnet]
[INFO] Sensor VB.NET Analysis Log [vbnet] (done) | time=10ms
[INFO] Sensor VB.NET Properties [vbnet]
[INFO] Sensor VB.NET Properties [vbnet] (done) | time=0ms
[INFO] ------------- Run sensors on project
[INFO] Sensor Analysis Warnings import [csharp]
[INFO] Sensor Analysis Warnings import [csharp] (done) | time=1ms
[INFO] Sensor Zero Coverage Sensor
[INFO] Sensor Zero Coverage Sensor (done) | time=6ms
[INFO] Sensor Java CPD Block Indexer
[INFO] Sensor Java CPD Block Indexer (done) | time=13ms
[INFO] SCM Publisher SCM provider for this project is: git
[INFO] SCM Publisher 6 source files to be analyzed
[WARNING] Could not find HEAD commit
[INFO] SCM Publisher 0/6 source files have been analyzed (done) | time=356ms
[WARNING] Missing blame information for the following files:
[WARNING]   * src/main/java/com/example/restdemo/employee/EmployeeService.java
[WARNING]   * src/main/java/com/example/restdemo/RestDemoApplication.java
[WARNING]   * src/test/java/com/example/restdemo/RestDemoApplicationTests.java
[WARNING]   * src/main/java/com/example/restdemo/employee/Employee.java
[WARNING]   * pom.xml
[WARNING]   * src/main/java/com/example/restdemo/employee/EmployeeController.java
[WARNING] This may lead to missing/broken features in SonarQube
[INFO] CPD Executor 1 file had no CPD blocks
[INFO] CPD Executor Calculating CPD for 3 files
[INFO] CPD Executor CPD calculation finished (done) | time=5ms
[INFO] Analysis report generated in 59ms, dir size=141.9 kB
[INFO] Analysis report compressed in 234ms, zip size=27.5 kB
[INFO] Analysis report uploaded in 37ms
[INFO] ANALYSIS SUCCESSFUL, you can find the results at: http://127.0.0.1:9000/dashboard?id=MyProject-1
[INFO] Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report
[INFO] More about the report processing at http://127.0.0.1:9000/api/ce/task?id=AYXlLdlMqAUl6Oj5wAId
[INFO] Analysis total time: 5.583 s
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  21.474 s
[INFO] Finished at: 2023-01-24T21:07:45+02:00
[INFO] ------------------------------------------------------------------------
Finished: SUCCESS