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>

如图

 

(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 

 

(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插件

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

      项目target目录

       SonarServer项目分析情况

(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结尾

进行代码检测的时候,必须要先compile编译,否则报错

mvn在打包的时候,会进行单元测试,所有单元测试的结果是在package之后

问题:

  • [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文件,即compile

  • Skipping JaCoCo execution due to missing execution data file,大多数原因是因为没有编译测试类,可以使用 clean package -Dmaven.test.skip=false
  • Property 'sonar.jdbc.url' is not supported any more. It will be ignored. There is no longer any DB connection to the SQ database. Property 'sonar.jdbc.username' is not supported any more. It will be ignored. There is no longer any DB connection to the SQ database. Property 'sonar.jdbc.password' is not supported any more. It will be ignored. There is no longer any DB connection to the SQ database.
    解释:You only need to configure the sonar.host.url. All communication between scanner and server is done with web services, and the scanner no longer talks to the database at all.
    Maven中的setting.xml修改配置如下,注释掉sonar.jdbc.*相关的配置

05 构建一个简单的Jenkins项目

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值