一、介绍
1:Sonar官网地址:
https://www.sonarqube.org/downloads/
一般有两个推荐版本:带星的属于稳定版
二、环境配置
Sonar需要一些辅助配置软件,需要自己装一下
1、 jdk的安装,版本要求要1.8,注意1.7版本的不支持;
2、 数据库的安装,我本地用的MySQL,5.6或者5.7两个版本都可以,我装的是5.6版本;
三、创建数据库
sonar需要java环境的支持,分析数据会保存到保存到数据库。支持mysql,oracle,PostgreSQL,SQLServer
1:mysql
CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci;
CREATE USER 'sonar' IDENTIFIED BY 'sonar';
GRANT ALL ON sonar.* TO'sonar'@'%' IDENTIFIED BY 'sonar';
GRANT ALL ON sonar.* TO'sonar'@'localhost' IDENTIFIED BY 'sonar';
四、sonar下载及配置
1:下载
打开浏览器,搜索sonarqube,进入官网,找到download按钮,下载安装包。下载之后,解压到任意目录。
2:启动
解压后,如果是 windows 64位系统,进入windows-x86-64 目录,双击InstallNTService.bat,成功之后,双击StartSonar.bat 启动。然后打开浏览器输入:http://127.0.0.1:9000/。可以看到一个界面。
3:配置sonar.properties
打开 sonar 解压目录的,conf,可以看到sonar.properties 文件中修改如下配置
sonar.jdbc.username=sonar
sonar.jdbc.password=sonar
#----- MySQL 5.6 or greater
# Only InnoDB storage engine is supported (not myISAM).
# Only the bundled driver is supported. It can not be changed.
sonar.jdbc.url=jdbc:mysql://121.42.37.157:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false
数据库弄好之后,同时需要复制一个数据库驱动放到安装目录的:extensions\jdbc-driver\mysql 下面。比如我用的是:mysql-connector-java-6.0.4.jar
五:下载SonarQube Scanner
1:下载
下载地址:
https://sonarsource.bintray.com/Distribution/sonar-scanner-cli/sonar-scanner-2.8.zip
下载之后,解压到随意目录。假设目录是:D:\sonar-scanner-2.8;将目录D:\sonar-scanner-2.8\bin 配置到环境变了path中。;
2:配置sonar-scanner.properties
打开D:\sonar-scanner-2.8\conf 下的sonar-scanner.properties 文件,进行如下配置:
#----- Default SonarQube server
sonar.host.url=http://localhost:9000
#----- Default source code encoding
#sonar.sourceEncoding=UTF-8
#----- Global database settings (not used for SonarQube 5.2+)
sonar.jdbc.username=sonar
sonar.jdbc.password=sonar
#----- PostgreSQL
#sonar.jdbc.url=jdbc:postgresql://localhost/sonar
#----- MySQL
sonar.jdbc.url=jdbc:mysql://121.42.37.157:3306/sonar?useUnicode=true&characterEncoding=utf8
#----- Oracle
#sonar.jdbc.url=jdbc:oracle:thin:@localhost/XE
#----- Microsoft SQLServer
#sonar.jdbc.url=jdbc:jtds:sqlserver://localhost/sonar;SelectMethod=Cursor
3:运行代码分析
启动了SonarQube,进入到项目的根目录创建一个文件sonar-project.properties
# must be unique in a given SonarQube instance
sonar.projectKey=my:project #项目唯一key(自己设置)
# this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 6.1.
sonar.projectName=ws #项目名称
sonar.projectVersion=1.0
# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
# This property is optional if sonar.modules is set.
sonar.sources=.
# Encoding of the source code. Default is default system encoding
#sonar.sourceEncoding=UTF-8
执行命令:
sonar-scanner #普通项目
mvn sonar:sonar #maven项目
然后就是等待执行结果,大概几分钟吧。成功之后,进入:http://127.0.0.1:9000/
故障排除
Java堆空间错误或者java.lang.OutOfMemoryError,
通过SONAR_SCANNER_OPTS环境变量增加内存:
export SONAR_SCANNER_OPTS="-Xmx512m"
在Windows环境中,请避免使用双引号,因为它们会被误解,并将两个参数合并为一个。
set SONAR_SCANNER_OPTS=-Xmx512m
帐号密码:
admin admin
MAVEN配置seting.xml