SonarQube简介
Sonar 是一个用于代码质量管理的开放平台。通过插件机制,Sonar 可以集成不同的测试工具,代码分析工具,以及持续集成工具。比如pmd-cpd、checkstyle、findbugs、Jenkins。通过不同的插件对这些结果进行再加工处理,通过量化的方式度量代码质量的变化,从而可以方便地对不同规模和种类的工程进行代码质量管理。同时 Sonar 还对大量的持续集成工具提供了接口支持,可以很方便地在持续集成中使用 Sonar。
此外,Sonar 的插件还可以对 Java 以外的其他编程语言(支持的语言包括:Java、PHP、C#、C、Cobol、PL/SQL、Flex等)提供支持,对国际化以及报告文档化也有良好的支持。可以说Sonar是目前最强大的代码质量管理工具之一。

安装环境
系统环境:centos7.0 -x86_64(最小化安装) 
前置条件:jdk1.8 , mysql-5.6
软件下载目录:/data/packages/
软件安装目录:/usr/local/
软件版本:sonarqube-6.7,sonar-runner-dist-2.4 sonar-scanner-3.0.3.778

预置条件

1、(可选) 卸载已有的open jdk,安装最新版本的java jdk,为后续集成maven服务做准备

# rpm -qa | grep jdk
  java-1.6.0-openjdk-1.6.0.0-1.45.1.11.1.el6.i686
# yum -y remove java-1.6.0-openjdk-1.6.0.0-1.45.1.11.1.el6.i686
  remove java-1.6.0-openjdk-1.6.0.0-1.45.1.11.1.el6.i686

安装JAVA, 请见 http://qiangsh.blog.51cto.com/3510397/1771748


2、安装Mysql数据库

//需要Mysql数据库支持;虽然SonarQube自带了H2数据库,但是为了方便管理数据推荐使用Mysql数据库

安装MySQL,请见 https://blog.51cto.com/qiangsh/1767495


3、数据库配置

使用InnoDB引擎,首先要做的是使用innodb_buffer_pool_size 参数为MySQL实例分配最大量的RAM,并为  参数提供至少15Mb  query_cache_size 。有关 更多信息,请阅读有关InnoDB性能优化基础知识的文章  。

进入数据库

#创建sonar数据库
mysql> CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci;
mysql> GRANT ALL ON sonar.* TO 'sonar'@'localhost' IDENTIFIED BY 'sonar@pw';
mysql> GRANT ALL ON sonar.* TO 'sonar'@'%' IDENTIFIED BY 'sonar@pw';
mysql> FLUSH PRIVILEGES;


在安装之前,介绍一下SonarQube和SonarQube Runner之间的关系。 
SonarQube是服务器端,它主要有两个功能:1.分析源代码;2.因为它内嵌了Apache模块,所以提供Web端的界面访问。
SonarQube Runner是一个利用SonarQube服务端分析代码的命令行工具,可以把它简单理解为客户端。
所以,为了安装和调试方便,建议SonarQube和SonarQube Runner都下载。

4、安装SonarQube

第一步:下载

sonarqube下载地址:http://www.sonarqube.org/downloads/
sonar-runner下载地址: 
http://repo1.maven.org/maven2/org/codehaus/sonar/runner/sonar-runner-dist/2.4/sonar-runner-dist-2.4.zip
sonar-scanner下载扫描器地址:https://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner

我已下载并上传百度云盘: https://pan.baidu.com/s/1Cq44IpHSy70_X3tIgBf9VA  密码:1mt0

wget https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-6.7.6.zip
wget http://repo1.maven.org/maven2/org/codehaus/sonar/runner/sonar-runner-dist/2.4/sonar-runner-dist-2.4.zip
wget https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-3.3.0.1492-linux.zip
unzip sonarqube-6.7.6.zip
unzip sonar-runner-dist-2.4.zip
unzip sonar-scanner-cli-3.3.0.1492-linux.zip
mv sonarqube-6.7.6 /usr/local/sonarqube
mv sonar-runner-2.4 /usr/local/sonar-runner
mv sonar-scanner-3.3.0.1492-linux /usr/local/sonar-scanner

第二步:配置环境变量

[root@qsh local]# vim /etc/profile
#--------------------------------------------------------------------
export SONAR_HOME=/usr/local/sonarqube
export SONAR_RUNNER_HOME=/usr/local/sonar-runner
export SONAR_SCANNER_HOME=/usr/local/sonar-scanner
PATH=$PATH:$SONAR_HOME/bin:$SONAR_RUNNER_HOME/bin:$SONAR_SCANNER_HOME/bin

[root@qsh local]# source /etc/profile

第三步:配置sonar.properties

[root@Nginx-Jenkins-Server sonarqube]# cd /usr/local/sonarqube/conf/
[root@Nginx-Jenkins-Server conf]# vim sonar.properties
#修改连接数据库用户密码
sonar.jdbc.username=sonar
sonar.jdbc.password=sonar@pw

#取消注释,修改数据库地址
sonar.jdbc.url=jdbc:mysql://127.0.0.1:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false

#取消下面两行注释
sonar.web.host=192.168.0.10
sonar.web.port=9000

sonarqube启动参数,修改配置: /usr/local/sonarqube/conf/wrapper.conf

ES启动参数,修改配置:/usr/local/sonarqube/elasticsearch/config/jvm.options

第四步:配置SonarQube Runner

[root@qsh conf]# cd /usr/local/sonar-runner/conf/
[root@qsh conf]# vim sonar-runner.properties
#----- Default SonarQube server
sonar.host.url=http://192.168.0.10:9000

#----- PostgreSQL
#sonar.jdbc.url=jdbc:postgresql://localhost/sonar

#----- MySQL
sonar.jdbc.url=jdbc:mysql://127.0.0.1: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

#----- Global database settings
sonar.jdbc.username=sonar
sonar.jdbc.password=sonar@pw

#----- Default source code encoding
sonar.sourceEncoding=UTF-8

#----- Security (when 'sonar.forceAuthentication' is set to 'true')
sonar.login=admin
sonar.password=admin
[root@qsh sonar-runner]# cd /usr/local/sonar-runner
[root@qsh sonar-runner]# mkdir /data/sonar-runner
[root@qsh sonar-runner]# vim sonar-project.properties
#-------------------------------------------------
#Required metadata
#对于较大的工程项目,经常出现内存不够的问题,需要利用环境变量设置虚拟机的内存
SONAR_RUNNER_OPTS=-Xmx512m-XX:MaxPermSize=128m
sonar.projectKey=my:project
sonar.projectName=yaok
sonar.projectVersion=1.0
#Paths to source directories.
#Paths are relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
#Do not put the "sonar-project.properties" file in the same directory with the source code.
#(i.e. never set the "sonar.sources" property to ".")
sonar.sources=/data/sonar-runner
#The value of the property must be the key of the language.
sonar.language=java
#Encoding of the source code
sonar.sourceEncoding=UTF-8
#Additional parameters
sonar.my.property=value


5、安装后启动SonarQube

用root无法启动lSonarQube,需要另外新建普通用户来启动

[root@qsh ~]# useradd esadmin
[root@qsh ~]# chown -R esadmin.esadmin /usr/local/sonarqube
[root@qsh ~]# vim /usr/local/sonarqube/elasticsearch/config/elasticsearch.yml

//开启以下两行

sonar.png

//切换用户,启动

[root@qsh ~]# su - esadmin
[esadmin@qsh ~]$ cd /usr/local/sonarqube/
[esadmin@qsh sonarqube]$ ./bin/linux-x86-64/sonar.sh start
Starting SonarQube...
Started SonarQube.

//检查是否正常启动

[esadmin@qsh sonarqube]$ ps -aux |grep sonar

//启动日志查看

#sonarqube
tail -f /usr/local/sonarqube/logs/sonar.log

#Es
tail -f /usr/local/sonarqube/logs/es.log

sonar111.png

附:启动的时候报错

1,错误:max file descriptors [4096] for elasticsearch process is too low, increase to at least
解决方法:

[root@qsh ]#vim /etc/security/limits.conf
#在文件最后添加下面两行
esadmin hard nofile 65536
esadmin soft nofile 65536

#查看文件限制命令
[esadmin@qsh sonarqube]$ ulimit -Hn
655350


2,错误,max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
解决方法:

切换到root用户修改配置sysctl.conf
[root@qsh]#vim /etc/sysctl.conf 
#添加下面配置:
vm.max_map_count=655360

并执行命令:
[root@qsh]# sysctl -p


6、安装后启动Sonar-Runner

[root@qsh sonar-runner]$ cd /usr/local/sonar-runner
[root@qsh sonar-runner]$ ./bin/sonar-runner -e -X

sonar2222.png如果能够看到下面的输出信息,证明你的SonarQube Runner安装并配置正确啦。

7、使用浏览器就可以直接登陆
http://192.168.0.39:9000/projects
Centos 7环境安装SonarQube和SonarQube Runner
//安装中文语言包

选择语言包,点击安装,安装好后重启,重新进入就是汉化版了Centos 7环境安装SonarQube和SonarQube Runner
ennd.png

插件安装位置: /usr/local/sonarqube/extensions/plugins/


8、配置SonarQube Scanner代码扫描工具

[root@qsh sonar-scanner]# cd /usr/local/sonar-scanner/conf/
[root@Nginx-Jenkins-Server conf]# vim sonar-scanner.properties
#Configure here general information about the environment, such as SonarQube server connection details for example
#No information about specific project should appear here

#----- Default SonarQube server
sonar.host.url=http://192.168.0.10:9000

#----- Default source code encoding
sonar.sourceEncoding=UTF-8

# 拉取sonar示例代码

[root@qsh ]# mkdir /data/sonar-examples
[root@qsh ]# cd /data/sonar-examples
[root@qsh sonar-examples]# git clone https://github.com/SonarSource/sonar-scanning-examples.git

#在项目的根目录中创建配置文件: sonar-project.properties

[root@qsh sonar-examples]# cd sonar-scanning-examples/sonarqube-scanner-maven/
[root@qsh sonarqube-scanner-maven]# vim sonar-project.properties
#------------------------------------------------------------------
# must be unique in a given SonarQube instance
sonar.projectKey=devops-demo
# this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 6.1.
sonar.projectName=devops-demo
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=.
sonar.java.binaries=.
 
# Encoding of the source code. Default is default system encoding
sonar.sourceEncoding=UTF-8

#代码扫描测试

[root@qsh sonarqube-scanner-maven]# sonar-scanner

sacn.png

//使用浏览器打开http://192.168.0.39:9000/projects ,查看扫描结果

end.png