SonarQube代码质量管理平台安装与使用

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

准备工作;

1、jdk(不再介绍)

2、sonarqube:http://www.sonarqube.org/downloads/

3、SonarQube+Scanner:https://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner

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

SonarQube安装
预置条件
1.已安装JAVA环境
2.已安装有MySQL数据库
1.下载好sonarqube后,解压打开bin目录,启动相应OS目录下的StartSonar。如本文演示使用的是win的64位系统,则打开F:\sonar\sonarqube-7.1\bin\windows-x86-64\StartSonar.bat

2.启动浏览器,访问http://localhost:9000,如出现下图则表示安装成功。
这里写图片描述

数据库配置
1.打开mysql,新建一个数据库。
2.打开sonarqube安装目录下的F:\sonar\sonarqube-7.1\conf\sonar.properties文件
3.在mysql5.X节点下输入以下信息

sonar.jdbc.url=jdbc:mysql://127.0.0.1:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance
sonar.jdbc.username=root
sonar.jdbc.password=root
sonar.sorceEncoding=UTF-8
sonar.login=admin
sonar.password=admin

4.重启sonarqube服务,再次访问http://localhost:9000,会稍微有点慢,因为要初始化数据库信息
5.数据库初始化成功后,登录
6.搜索chinese Pack,安装中文语言包
7.安装成功后,重启sonarqube服务,再次访问http://localhost:9000/,即可看到中文界面
这里写图片描述

配置sonar scanner
二.安装
第一步:
将下载的压缩包解压缩到任意目录;

第二步:
打开Sonar Scanner根目录下的/conf/sonar-scanner.properties文件,配置如下:

sonar.host.url=http://localhost:9000 //sonar.host.url配置的是SonarQube服务器的地址

第三步:
新建系统变量:

SONAR_SCANNER_HOME=Sonar Scanner根目录

修改系统变量path,新增%SONAR_SCANNER_HOME%\bin(不新建SONAR_SCANNER_HOME直接新增path亦可);

打开cmd面板,输入sonar-scanner -version,出现下图,则表示环境变量设置成功:

三.使用
在项目根目录下新建sonar-project.properties文件,内容如下

# must be unique in a given SonarQube instance
sonar.projectKey=esb
# this is the name displayed in the SonarQube UI
sonar.projectName=esb
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=F:/program/esb/src

# Encoding of the source code. Default is default system encoding
sonar.sourceEncoding=UTF-8

sonar.my.property=value
sonar.java.binaries=F:/program/esb/WebRoot/WEB-INF/classes

设置完后,打开cmd面板,进入项目根目录下,然后输入“sonar-scanner”命令,执行代码分析:
这里写图片描述
分析成功的界面为:
这里写图片描述
然后打开http://localhost:9000 (SonarQube服务器),输入账号密码,即可查看代码分析结果。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章