七叶笔记 » golang编程 » 团队没人review代码?让sonar来帮你,水平秒提升

团队没人review代码?让sonar来帮你,水平秒提升

不论你认为自己的代码写的多牛逼,sonar总能给你找到你还能改进的地方,sonar就是这么牛逼

SonarQube是管理代码质量一个开放平台,可以快速的定位代码中潜在的或者明显的错误,下面将会介绍一下这个工具的安装、配置以及使用。

准备工作;

1、jdk(不再介绍)

2、sonarqube:

3、SonarQube+Scanner: bin tray.com/Distribution/sonar-scanner-cli/sonar-scanner-2.5.zip

4、 mysql 数据库(不再介绍)

一、安装篇

1.下载好sonarqube后,解压打开bin目录,启动相应OS目录下的启动文件。

直接win下载,然后上传到 linux 服务器;下载地址:;版本:SonarQube 6.7.2

linux目录:/opt/softs

解压命令:

 sudo unzip -q sonarqube-6.7.2.zip -d /opt/softs  

启动命令:

 cd /opt/softs/sonarqube-6.7.2/bin/linux-x86-64 ./sonar.sh start  

这里启动可能报错“sonar es can not run elasticsearch as root”;

上面链接解决之后,出现下面第二步

2.启动浏览器,访问,如出现下图则表示安装成功。

二、配置MySql数据库篇

1.打开mysql,新建一个数据库。

2.打开sonarqube安装目录下的/opt/softs/sonarqube-6.7.2/conf/sonar.properties文件

3.在mysql5.X节点下输入以下信息

 sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false
sonar.jdbc.username=sonar
sonar.jdbc.password=Test1234  

url是数据库连接地址,username是数据库用户名,jdbc.password是数据库密码,login是sonarqube的登录名,sonar.password是sonarqube的密码

4.重启sonarqube服务,再次访问,会稍微有点慢,因为要初始化数据库信息

这里可能出现“sonar配置mysql Unrecoverable indexation failures”

5.数据库初始化成功后,登录;默认用户名和密码是admin;admin

三、安装中文插件

1.登录之后依次选择下面,参照图片安装

“Administrator” -> “Marketplace”

2.等待安装完毕之后重启。

3.再次登录时;界面就变成中文的了。

四、win10安装SonarQube+Scanner使用篇

1.下载,我下载的是:

2.打开D:\sonar\sonar-scanner-2.8\conf\sonar-runner.properties文件

3.mysql节点下输入以下信息

 sonar.jdbc.url=jdbc:mysql://localhost:3306/qjfsonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance
sonar.jdbc.username=sonar
sonar.jdbc.password=sonar  

注意:如果测试项目与服务器不在同一台机子,则需要添加服务器的IP:

 #----- Default SonarQube server
sonar.host.url=  

3.配置环境变量

a.新建变量,name=SONAR_RUNNER_HOME。value=D:\sonar\sonar-scanner-2.8

b.打开path,输入%SONAR_RUNNER_HOME%\bin;

c.sonar-runner -version,出现以下信息,则表示环境变量设置成功

五、linux安装SonarQube+Scanner使用篇

1.下载,我下载的是:

2.解压到目录(可解压到任意目录)。修改系统路 径 path,如下:

2.1.切换到root用户

2.2.通过修改 profile 文件:
vim /etc/profile

2.3.输入如下内容:

 #sonar-scanner profile export SONAR_SCANNER_HOME=/opt/softs/sonarqube-6.7.2/sonar-scanner-2.8export PATH=${SONAR_SCANNER_HOME}/bin:${PATH}  

2.4要让刚才的修改马上生效,需要执行以下代码

# source /etc/profile

3.验证是否配置正确;输入:

 $ sonar-runner -v  

出现下图就证明ok

六、使用篇

1.打开要进行代码分析的项目根目录,新建sonar-project.properties文件

2.输入以下信息

 # must be unique in a given SonarQube instance
sonar.projectKey=my:project
# this is the name displayed in the SonarQube UI
sonar.projectName=apiautocore
sonar.projectVersion=1.0
 
# Path is relative to the sonar-project.properties file.  Replace  "\" by "/" on Windows.
# Since SonarQube 4.2, this property is optional if sonar.modules is set. 
# If not set, SonarQube starts looking for source code from the directory containing 
# the sonar-project.properties file.
sonar.sources=src
 
# Encoding of the source code. Default is default system encoding
#sonar.sourceEncoding=UTF-8  

其中:projectName是项目名字,sources是源文件所在的目录

3.设置成功后,启动sonarqube服务,并启动cmd

4.在cmd进入项目所在的根目录,输入命令:sonar-runner,分析成功后会出现下图

5.打开,我们会看到主页出现了分析项目的概要图

6.我们点击项目,选择问题链接,会看到分析代码的bug,哇,好多

7.选择一个最严重的bug,看看

原来是这个地方会出现空指针异常,原因是我没有进行初始化就使用这个变量了。

相关文章