04 通过SonarQube进行质量分析的两种方式

根据上一章,我们知道,可以利用SonarScanner对代码进行扫描,同步扫描结果到数据库,从而SoanrQube Server对结果进行分析统计,图表展示。

集成SonarQube进行质量分析的两种方式:

1:通过SonarScanner工具,扫描本地代码,同步到数据库;(省略,详细见上一章SonarScanner的测试)

03 sonarQube+sonarScanner 环境搭建+测试

2:通过集成sonar-maven-plugin插件,对代码进行质量分析

java demo下载地址:

链接:https://pan.baidu.com/s/1qF0899iIcVXV8_zzeC0rgQ

提取码:0eha

(1):SonarQube7.3 安装完成,Maven3.6.1安装完成,Java Demo(测试用例)准备完成。

(2):修改maven ,config下面的settings.xml,添加如下配置

添加

<profiles>

<profile>

<id>sonar</id>

<properties>

<sonar.jdbc.url>jdbc:mysql://172.172.0.107:3316/sonar</sonar.jdbc.url>

<sonar.jdbc.driver>com.mysql.jdbc.Driver</sonar.jdbc.driver>

<sonar.jdbc.username>sonar</sonar.jdbc.username>

<sonar.jdbc.password>sonar</sonar.jdbc.password>

<sonar.host.url>http://172.172.0.107:9000</sonar.host.url> <!-- Sonar服务器访问地址 -->

<sonar.login>admin</sonar.login> ##特别注意,如果不能匿名访问必须要加

<sonar.password>admin</sonar.password> ##特别注意,如果不能匿名访问必须要加

</properties>

</profile>

</profiles>

sonar.login,sonar.password 不加会产生的问题:

  • Failed to execute goal org.sonarsource.scanner.maven:sonar-maven-plugin:3.3.0.603:sonar (default-cli) on project demo: Insufficient privileges
  • Failed to execute goal org.sonarsource.scanner.maven:sonar-maven-plugin:3.3.0.603:sonar (default-cli) on project demo: You're only authorized to execute a local (preview) SonarQube analysis without pushing the results to the SonarQube server. Please contact your SonarQube administrator. -> [Help 1]

(3):配置代码中的pom,进行代码质量检测

java demo中pom添加如下配置

<plugin>

<groupId>org.sonarsource.scanner.maven</groupId>

<artifactId>sonar-maven-plugin</artifactId>

<version>3.3.0.603</version>

</plugin>

如图

04 通过SonarQube进行质量分析的两种方式

 

(4):编译验证

在Java demo 根目录,执行:mvn verify sonar:sonar,查看sonarqube服务器

 

常见问题:

  • Failed to execute goal org.sonarsource.scanner.maven:sonar-maven-plugin:3.6.0.1398:sonar (default-cli) on project demo: Error when executing blame for file src/main/java/com/example/demo/SonarTestApplication.java: svn: E210004: Malformed networ k data -> [Help 1] 

解决 配置SonarQube服务 ,配置》SCM》,打开Disable the SCM Sensor 

04 通过SonarQube进行质量分析的两种方式

 

(5)添加单元测试覆盖率检测

  • java项目pom中添加插件,jacoco-maven-plugin,用于单元测试生成覆盖率报告

官网参考地址:https://www.eclemma.org/jacoco/trunk/doc/maven.html

<plugin>

<groupId>org.jacoco</groupId>

<artifactId>jacoco-maven-plugin</artifactId>

<version>0.8.1</version>

<configuration>

<includes>

<include>com/**/*</include>

</includes>

</configuration>

<executions>

<execution>

<id>pre-test</id>

<goals>

<goal>prepare-agent</goal>

</goals>

</execution>

<execution>

<id>post-test</id>

<phase>test</phase>

<goals>

<goal>report</goal>

</goals>

</execution>

</executions>

</plugin>

(6)SonarQube安装jacoco插件

04 通过SonarQube进行质量分析的两种方式

(7)进入java demo 项目根目录:mvn sonar:sonar

      项目target目录

04 通过SonarQube进行质量分析的两种方式

       SonarServer项目分析情况

04 通过SonarQube进行质量分析的两种方式

(8)其他方式:

  • mvn clean org.jacoco:jacoco-maven-plugin:prepare-agent install -Dmaven.test.failure.ignore=true

  • mvn sonar:sonar

 

注意点:

SonarQube覆盖率0%的问题

  • 所有的测试单元报告和测试覆盖率报告,都是Maven打包编译的时候生成好的,要先package,再mvn sonar:sonar
  • 检查,是否添加了单元测试用
  • 单元测试类好像必须要以Test结尾,集成测试类必须要以IT结尾

 

问题:

  • [ERROR] Failed to execute goal org.sonarsource.scanner.maven:sonar-maven-plugin:3.6.1.1688:sonar (default-cli) on project SonarTest: Please provide compiled classes of your project with sonar.java.binaries property -> [Help 1]

解决:这是代码没有编译,先编译生成class文件

  • Skipping JaCoCo execution due to missing execution data file,大多数原因是因为没有编译测试类,可以使用 clean package -Dmaven.test.skip=false

05 构建一个简单的Jenkins项目