Skip to content

Spring Boot With SonarQube#

SonarQube Server#

  • You need to create a sonarqube server, if you don’t know how to create one so you can view Docker With SonarQube.

Junit 4 Configuration#

  • If you are using Junit 4 in your Spring Boot project. So let add this plugin into pom.xml file of your Spring Boot project as below.
pom.xml
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
    <java.version>1.8</java.version><sonar.coverage.jacoco.xmlReportPaths>target/site/jacoco/jacoco.xml</sonar.coverage.jacoco.xmlReportPaths>
    <sonar.exclusions>
        **/models/*.class,
        **/repositories/*.class,
        **/constants/*.class
    </sonar.exclusions>
    <jacoco.version>0.8.3</jacoco.version>
</properties>

<dependencies>
....

    <dependency>
        <groupId>org.junit.vintage</groupId>
        <artifactId>junit-vintage-engine</artifactId>
        <version>5.4.0</version>
        <scope>test</scope>
    </dependency>

....

</dependencies>


<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>

            <!-- Jacoco report: code coverage report -->
        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>${jacoco.version}</version>
            <!-- packages are excluded -->
            <configuration>
                <excludes>
                    <exclude>**/models/*.class</exclude>
                    <exclude>**/repositories/*.class</exclude>
                    <exclude>**/constants/*.class</exclude>
                </excludes>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                </execution>
                <!-- attached to Maven test phase -->
                <execution>
                    <id>report</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

<profiles>
    <profile>
        <id>dev</id>
        <properties>
            <activeatedProperties>dev</activeatedProperties>
            <!-- sonarqube url and port -->
            <sonar.host.url>http://localhost:9000</sonar.host.url>
            <!-- sonarqube username -->
            <sonar.login>admin</sonar.login>
            <!-- sonarqube password -->
            <sonar.password>password</sonar.password>
        </properties>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
</profiles>

Junit 5 Configuration#

  • If you are using Junit 5 in your Spring Boot project. So let add this plugin into pom.xml file of your Spring Boot project as below.
pom.xml
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<properties>  
    <java.version>1.8</java.version>  
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  <sonar.coverage.jacoco.xmlReportPaths>target/site/jacoco/jacoco.xml</sonar.coverage.jacoco.xmlReportPaths>  
    <sonar.exclusions>        
            **/models/*.class,  
            **/repositories/*.class,  
            **/constants/*.class  
    </sonar.exclusions>  
    <jacoco.version>0.8.8</jacoco.version>  
    <eureka.client.version>3.1.0</eureka.client.version>  
</properties>


<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>2.6.3</version>
        </plugin>

        <!-- add test engine for junit 5 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.0.0-M7</version>
            <dependencies>
                <dependency>
                    <groupId>org.junit.jupiter</groupId>
                    <artifactId>junit-jupiter-engine</artifactId>
                    <version>5.4.0</version>
                </dependency>
            </dependencies>
        </plugin>

        <!-- Jacoco report: code coverage report -->
        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>${jacoco.version}</version>
            <configuration>
                <excludes>
                    <exclude>**/models/*.class</exclude>
                    <exclude>**/repositories/*.class</exclude>
                    <exclude>**/constants/*.class</exclude>
                </excludes>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                </execution>
                <!-- attached to Maven test phase -->
                <execution>
                    <id>report</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

<profiles>
    <profile>
        <id>dev</id>
        <properties>
            <activeatedProperties>dev</activeatedProperties>
            <sonar.host.url>http://localhost:9000</sonar.host.url>
            <sonar.login>admin</sonar.login>
            <sonar.password>password</sonar.password>
        </properties>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
</profiles>

Testing#

  • Now, you can command mvn clean package sonar:sonar to scan your SpringBoot project. Then you will see an Url that contain a SonarScan report. Just click it and you can view SonarQube reports.

 #zoom

 #zoom

See Also#