Sonar-代码质量检测搭建

下载安装SonarQube

下载官网路径:https://www.sonarqube.org/downloads/

研究搭建使用版本:SonarQube7.8  (7.8以上不支持mysql数据库)mysql版本(5.6-5.8)软件环境JDK1.8

下载链接:https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-7.8.zip

 

windows安装

解压目录:D:\xxx\sonarqube-7.8

Sonarqube内置集成elasticsearch

./elasticsearch/conf/elasticsearch-.yml 集群配置

集群配置参考链接说明:https://www.cnblogs.com/zlslch/p/6419948.html

此次研究单机部署,无需修改配置

 

sonar配置 ./conf/sonar.properties

数据库配置

#--------------------------------------------------------------------------------------------------

# DATABASE

#

# IMPORTANT:

# - The embedded H2 database is used by default. It is recommended for tests but not for

#   production use. Supported databases are MySQL, Oracle, PostgreSQL and Microsoft SQLServer.

# - Changes to database connection URL (sonar.jdbc.url) can affect SonarSource licensed products.

 

# User credentials.

# Permissions to create tables, indices and triggers must be granted to JDBC user.

# The schema must be created first.

#数据账号,密码

sonar.jdbc.username=xxx

sonar.jdbc.password=xxx

 

#----- Embedded Database (default)

# H2 embedded database server listening port, defaults to 9092

#sonar.embeddedDatabase.port=9092

 

#----- DEPRECATED

#----- MySQL >=5.6 && <8.0

# Support of MySQL is dropped in Data Center Editions and deprecated in all other editions

# Only InnoDB storage engine is supported (not myISAM).

# Only the bundled driver is supported. It can not be changed.

#数据库连接

sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?serverTimezone=GMT%2B8&tinyInt1isBit=false&useUnicode=true&characterEncoding=utf8&connectTimeout=5000&socketTimeout=60000&autoReconnect=true&failOverReadOnly=false&useSSL=false

 

#----- Oracle 11g/12c

#sonar.jdbc.url=jdbc:oracle:thin:@localhost:1521/XE

 

 

#----- PostgreSQL 9.3 or greater

#sonar.jdbc.url=jdbc:postgresql://localhost/sonarqube?currentSchema=my_schema

 

 

#----- Microsoft SQLServer 2014/2016/2017 and SQL Azure

#sonar.jdbc.url=jdbc:sqlserver://localhost;databaseName=sonar;integratedSecurity=true

 

# Use the following connection string if you want to use SQL Auth while connecting to MS Sql Server.

#sonar.jdbc.url=jdbc:sqlserver://localhost;databaseName=sonar

 

#----- Connection pool settings

#最大连接数据库连接数,设置为0时,表示没有限制

sonar.jdbc.maxActive=60

 

# The maximum number of connections that can remain idle in the

# pool, without extra ones being released, or negative for no limit.

#最大等待连接中的数量,设置为0时,表示没有限制

sonar.jdbc.maxIdle=5

 

# The minimum number of connections that can remain idle in the pool,

# without extra ones being created, or zero to create none.

#最小等待连接中的数量

sonar.jdbc.minIdle=2

 

# The maximum number of milliseconds that the pool will wait (when there

# are no available connections) for a connection to be returned before

# throwing an exception, or <= 0 to wait indefinitely.

#最大等待秒数,单位为毫秒, 超过时间会报出错误信息

sonar.jdbc.maxWait=5000

#配置一个连接在池中最小生存的时间,单位是毫秒

sonar.jdbc.minEvictableIdleTimeMillis=600000

#配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒

sonar.jdbc.timeBetweenEvictionRunsMillis=30000

注:数据库配置开启后每次sonar项目同步结果很慢,扫描后要等几分钟,进行数据处理落库

 

访问参数配置

# Web context. When set, it must start with forward slash (for example /sonarqube).

# The default value is root context (empty value).

#访问系统应用名称

sonar.web.context=

# TCP port for incoming HTTP connections. Default value is 9000.

#暴露服务端口

sonar.web.port=9000

 

 

# The maximum number of connections that the server will accept and process at any given time.

# When this number has been reached, the server will not accept any more connections until

# the number of connections falls below this value. The operating system may still accept connections

# based on the sonar.web.connections.acceptCount property. The default value is 50.

#Http访问最大线程数

sonar.web.http.maxThreads=50

 

# The minimum number of threads always kept running. The default value is 5.

#Http访问最小线程数

sonar.web.http.minThreads=5

 

# The maximum queue length for incoming connection requests when all possible request processing

# threads are in use. Any requests received when the queue is full will be refused.

# The default value is 25.

#起动的线程数达到最大时,接受排队的请求个数

sonar.web.http.acceptCount=25

 

注:其他参数忽略

windows环境下启动

 cd  D:\xxxx\sonarqube-7.8\sonarqube-7.8\bin\windows-x86-64

双击启动:StartSonar.bat

启动成功结果:

访问页面:http:localhost:9000/

登录:http://localhost:9000/sessions/new

默认admin/admin

 

linux安装

./elasticsearch/conf/elasticsearch-.yml 集群配置

同windows

sonar配置 ./conf/sonar.properties

同windows

Linux环境下启动

cd /xxx/sonarqube-7.8/bin/linux-x86-64

./sonar.sh start

访问页面:http://192.168.xxx.xxx:9000/about

后续同window

Linux遇到启动问题

1.can not run elasticsearch as root 

Sonarqube 内置 elasticsearch , elasticsearch 启动不能使用root 用户

#解决方案:

#root 用户创建子账号

#新建用户,设置密码

adduser sonar

passwd sonar 

#es目录赋权

chown -R sonar:sonar ./sonarqube-7.8/*

 

2.max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]

 

#解决方案:

#root 用户

vi /etc/sysctl.conf

#增加配置

vm.max_map_count=262144

#生效命令:

/sbin/sysctl -p

 

3.max file descriptors [65535] for elasticsearch process is too low, increase to at least [65536] 

 

#解决方案:

#root 用户

vi /etc/security/limits.conf

 

#最下面增加

  * soft nofile 65536

  * hard nofile 65536

 

#切换用户重新登录

#检测当前用户句柄数

ulimit -a

 

IDEA集成SonarLint

插件安装

File ->Setings -> Pluins -> sonarLint

安装后重启IDEA服务

Eclipse 集成SonarLint

插件安装

help -> eclipse marketplace...,搜索sonarlint, Install

 

从Window -> show view 中查看SonarLint

注:Eclipse低版本不支持插件安装

离线安装

下载地址:

https://bintray.com/sonarsource/SonarLint-for-Eclipse/download_file?file_path=releases%2Forg.sonarlint.eclipse.site-3.6.0.201806071228-RELEASE.zip

安装后restart,重启Eclipse

Maven Settings sonar同步全局配置

Settings配置

#找到maven 环境使用的settings.xml 文件 增加如下配置

<profile>

<id>sonar</id>

<activation>

<activeByDefault>true</activeByDefault>

</activation>

<properties>

<sonar.host.url>

  http://192.168.xxx.xxx:9000

</sonar.host.url>

 </properties>

</profile>

Sonar扫描结果将会同步至:http://192.168.xxx.xxx:9000 服务器

注:IDEA,Eclipse也可以内部集成配置同步地址,此处只做Maven全局配置

 

Maven项目POM配置Sonar

<plugin>
  <groupId>org.sonarsource.scanner.maven</groupId>
    <artifactId>sonar-maven-plugin</artifactId>
    <version>3.6.0.1398</version>
</plugin>

 

<plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
        <encoding>utf-8</encoding>
        <skip>true</skip>
    </configuration>
</plugin>

注:maven-compiler-plugin -> configuration 去掉java版本号,容易冲突

Maven update

IDEA同步Sonar扫描

未同步前IDEA→Settings → SonarLint →Rules 配置规则

未同步前SonarQube服务端

 

双击 sonar:sonar 后,等待同步结果

SUCCESS 表示成功

访问:http://192.168.xxx.xxx:9000/projects 

可以看到新增 :fills-tools

 

 

 

Eclipse 同步 Sonar 扫描

未同步前Eclipse→Window → preferences →SonarLint ->  Rule Configuration配置规则

项目sonar扫描

 

Goals: 输入 sonar:sonar

Debug/Run 运行

 

后续同IDEA

Maven 同步Sonar 扫描

windows 控制台cd 到系统目录下执行命令

mvn sonar:sonar

成功后会将扫描信息同步到服务端

后续同IDEA

 

注:IDEA ,Eclipse 实现同 maven 即 sonar:sonar

SonarQube 关键字解释

 

复杂度(Complexity)
指标(英文)指标(中文)描述
Complexity复杂度基于代码的分支计算出来的复杂度,即圈复杂度。当一个方法的控制流多了一个分支,它的复杂度就会增加1。每个方法的最小复杂度为1。
Congnitive Complexity理解复杂度理解代码的控制流的难易程度
覆盖率(Coverage)
Condition coverage分支覆盖被单元测试覆盖到的分支数
Condition coverage(%)分支覆盖率在包含一些布尔表达式的每一行代码中,条件覆盖率简单地回答了以下问题:“是否将每个布尔表达式都计算为true和false?”这是单元测试执行期间遵循的流控制结构中可能的条件的密度。
分支覆盖率 = (CT + CF)/(2*B)
其中,CT为至少一次被评估为true的分支数,CF为至少一次被评估为false的分支数,B为总分支数。
Condition coverage on new code新代码的分支覆盖率同上,但只针对新代码和更新的代码
Line coverage代码覆盖率代码行覆盖率,即被单元测试覆盖的行数/总代码行数
Line coverage on new code新代码覆盖率新代码行覆盖率,公式同上,但只针对新代码或更新的代码
Coverage覆盖率总覆盖率,即分支覆盖率和代码行覆盖率的总和,它旨在提供更加准确的数据去展现源代码被单元测试覆盖的程度。公式:(CT+CF+LC)/(2*B+EL)
Coverage on new code新覆盖率同上,但只针对新代码和更新的代码
Lines to cover代码行被单元测试覆盖的代码行数
Lines to cover on new code覆盖的新代码被单元测试覆盖的代码行数(只针对新增代码或更新的代码)
Skipped unit tests单元测试忽略数忽略的单元测试用例数
Uncovered conditions未覆盖分支单元测试未覆盖的分支数
Uncovered conditions on new code未覆盖新分支单元测试未覆盖的新增分支数
Uncovered lines未覆盖的代码单元测试未覆盖的代码行数
Uncovered lines on new code未覆盖的新代码单元测试未覆盖的新增代码行数
Unit tests duration单元测试持续时间单元测试所持续的时间
Unit test errors单元测试错误数单元测试执行错误数,即运行异常
Unit test failures单元测试失败数单元测试执行失败数,即运行正常但是得到非预期结果
Unit test success density (%)单元测试成功(%)(单元测试用例数-(单元测试错误数+单元测试失败数))/单元测试用例数*100
Unit tests单元测试数单元测试用例数
重复(Duplication)
Duplicated blocks重复块重复行块数。如何定义一个块是否重复块:
JAVA类项目:
- 无论标记和行数多少,只要有至少10个连续重复的语句,则定义为重复块;
Duplicated blocks on new code新代码中的重复块同上,但只针对新代码
Duplicated files重复文件重复的文件数
Duplicated lines重复行重复的代码行数
Duplicated lines (%)重复行(%)重复行数/总行数*100
Duplicated lines on new code新代码中的重复行同上,但只针对新代码
Duplicated lines(%)on new code新代码中的重复行密度(%)同上,但只针对新代码
问题(Issues)
指标(英文)指标(中文)描述
Blocker violations阻断违规阻断违规数
Confirmed issues确认问题确认问题数
Critical violations严重违规严重违规数
false positive violations误判问题误判问题数
Info violationd提示违规提示违规数
violations违规违规数
major violations主要违规主要违规数
minor violations次要违规次要违规数
New blocker violations新阻断违规新阻断违规数
New critical violations新严重违规新严重违规数
New info violations新提示违规新提示违规数
New violations新违规新违规数
New major violations新主要违规新主要违规数
New minor violations新次要违规新次要违规数
Open issues开启问题状态为开启的问题数
Reopen issues重开问题状态为重开的问题数
Unfixed issues不修复的问题状态为不修复的问题数
可维护性(Maintainability )
指标(英文)指标(中文)描述
Technical Debt技术债务修复所有可维护性问题所需的工作量,单位为分钟。当该值以天为单位展示时默认为8小时/天。
Technical Debt on new code新代码的技术债务同上,但只针对新代码。
Code Smells坏味道坏味道数
Maintainability Rating on new code 新代码可维护率同上,但只针对新代码。
New code smell新增坏味道新增坏味道数
Technical Debt Ratio技术债务比率开发成本与修复成本的比率。
Technical Debt Ratio on new code新技术债务比率开发成本与修复成本的比率。
该比率=修复成本/开发成本,也=修复成本/(代码行数*每行代码开发成本)
每行代码开发成本为0.06天
可靠性(Reliability)
指标(英文)指标(中文)描述
New Vulnerabilities新增漏洞新增安全漏洞数
Security Rating安全比率A = 0 安全漏洞
B = 至少1个Minor漏洞
C = 至少1个Major漏洞
D = 至少1个Critical漏洞
E = 至少1个Blocker漏洞
Security Rating on new code新代码安全率同上,但只针对新代码。
Security remediation effort安全修复工作修复所有安全漏洞所需的工作量,单位为分钟。当该值以天为单位展示时默认为8小时/天。
Security remediation effort on new code新代码的安全修复工作同上,但只针对新代码。
Vulnerabilities漏洞安全漏洞数
大小(Documentation)
指标(英文)指标(中文)描述
Classes类数目
Comment lines注释行数 
Comments(%)注释(%)注释行数/(注释行数+代码行数)
Directories目录目录数
Files文件文件数
Functions方法方法数
Lines行数物理行数,包含回车。
Lines of code代码行数代码行数,包含至少一个非空格、非tab和非注释的字母的代码行数
Lines of new code新增代码行数新增代码行数,包含至少一个非空格、非tab和非注释的字母的代码行数
Projects项目项目数
Statements语句语句数

 

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值