1、SonarQube 介绍
SonarQube 是一个用于代码质量管理的开放平台。通过插件机制,Sonar 可以集成不同的测试工具,代码分析工具,以及持续集成工具。
与持续集成工具(如 Hudson、Jenkins 等)不同,Sonar 并不是简单的把不同的代码检查工具结果(如 FindBugs、PMD 等)直接显示在 Web 页面上,而是通过不同的插件对这些结果进行再加工处理,通过量化的方式度量代码质量的变化,从而可以方便地对不同规模和种类的工程进行代码质量管理。
在对其他工具的支持方面, Sonar 不仅提供了对 IDE 的支持,可以在 Eclipse 和 IntelliJ IDEA 这些工具里联机查看结果;同时 Sonar 还对大量的持续集成工具提供了接口支持,可以很方便的在持续集成中使用 Sonar。
此外,Sonar 的插件还可以对 Java 以外的其他变成语言提供支持,对国际化以及报告文档化有良好的支持。
Sonar 的相关下载和文档可以在下面的链接中找到:
http://www.sonarqube.org/downloads/
需要注意最新版本的 Sonar 需要至少 JDK 1.8 及以上版本。
2、安装 JDK
yum install -y java-1.8.0
3、安装 SoanrQube
cd /usr/local/src
wget https://sonarsource.bintray.com/Distribution/sonarqube/sonarqube-6.3.zip
unzip sonarqube-6.3.zip
mv sonarqube-6.3 /usr/local/
ln -s /usr/local/sonarqube-6.3 /usr/local/sonarqube
4、准备 Sonar 数据库
为 Sonar 准备 MySQL 数据库,版本必须大于 5.6。
4.1、下载 mysql 二进制包
cd /usr/local/src
wget http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.30-linux-glibc2.5-x86_64.tar.gz
4.2、创建 mysql 用户
groupadd mysql
useradd -r -g mysql -s /bin/false mysql
4.3、解压 mysql 二进制包
cd /usr/local/src
tar zxf mysql-5.6.30-linux-glibc2.5-x86_64.tar.gz
mv mysql-5.6.30-linux-glibc2.5-x86_64 /usr/local
chown -R mysql:mysql /usr/local/mysql-5.6.30-linux-glibc2.5-x86_64
ln -s /usr/local/mysql-5.6.30-linux-glibc2.5-x86_64 /usr/local/mysql
chown -R mysql:mysql /usr/local/mysql
4.4、初始化mysql
/usr/local/mysql/scripts/mysql_install_db \
--defaults-file=/usr/local/mysql/my.cnf \
--user=mysql --basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data
4.5、启动 mysql
/usr/local/mysql/bin/mysqld_safe --defaults-file=/usr/local/mysql/my.cnf &
4.6、创建sonar数据库
CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci;
GRANT ALL ON sonar.* TO 'sonar'@'localhost' IDENTIFIED BY 'sonar@pw';
GRANT ALL ON sonar.* TO 'sonar'@'%' IDENTIFIED BY 'sonar@pw';
FLUSH PRIVILEGES;
5、配置并启动 Sonar
sonar 的配置文件主要是配置相关启动参数和数据库的信息,数据库不需要初始化,Sonar 启动的过程中会自动初始化。
# cd /usr/local/sonarqube/conf/
# ls
sonar.properties wrapper.conf
编写配置文件,修改数据库配置
# vim sonar.properties
sonar.jdbc.username=sonar
sonar.jdbc.password=sonar@pw
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance
配置 Java 访问数据库驱动
"""
默认情况 Sonar 有自带的嵌入数据库,如果需要使用类似 Oracle 数据库,必须手动复制驱动 jar 包到 ${SONAR_HOME}/extensions/jdbc-driver/oracle 目录下,其他支持的数据库默认提供了驱动。
其他数据库的配置可以参考官方文档:[http://docs.sonarqube.org/display/HOME/SonarQube+Platform](http://docs.sonarqube.org/display/HOME/SonarQube+Platform)
"""
启动 Sonar,可以在 Sonar 的配置文件来配置 Sonar Web 监听的 IP 地址和端口,默认是 9000 端口。
# vim sonar.properties
sonar.web.host=0.0.0.0
sonar.web.port=9000
启动
/usr/local/sonarqube/bin/linux-x86-64/sonar.sh start
6、Sonar 插件使用
6.1、安装中文插件
Sonar 和 Jenkins 一样有丰富的插件,而且插件的安装也比较简单,这里使用 Sonar 中文支持的例子,介绍 Sonar 插件管理的两种方法:
第一种方法:手动下载插件到相应目录下
下载中文 jar 包到插件目录下
cd /usr/local/sonarqube/extensions/plugins
wget https://github.com/SonarQubeCommunity/sonar-l10n-zh/releases/download/sonar-l10n-zh-plugin-1.11/sonar-l10n-zh-plugin-1.11.jar
重启 sonar
/usr/local/sonarqube/bin/linux-x86-64/sonar.sh restart
第二种方法:使用 Web 界面安装插件
默认密码:admin / admin
配置→ 系统 → 更新中心 → Available → 训责 Chinese Pack。
6.2、安装语言插件
默认情况 SonarQube 安装成功后只能分析 Java,如果想让它分析其他语言需要安装插件,例如安装常用的 Python、PHP、CSS、JavaScript 等你需要的其他语言。如果由于网络问题使用 Web 界面安装插件失败的化,就可以使用第一种方式,手工下载插件,下载地址:
https://github.com/SonarQubeCommunity/
cd /usr/local/sonarqube/extensions/plugins/
wget https://sonarsource.bintray.com/Distribution/sonar-php-plugin/sonar-php-plugin-2.9.2.1744.jar
wget http://downloads.sonarsource.com/plugins/org/codehaus/sonar-plugins/python/sonar-python-plugin/1.5/sonar-python-plugin-1.5.jar
wget https://github.com/SonarQubeCommunity/sonar-css/releases/download/1.10/sonar-css-plugin.jar
# 重启 Sonar
/usr/local/sonarqube/bin/linux-x86-64/sonar.sh restart
7、使用 Sonar Scanner 进行分析
7.1、安装 Sonar Scanner
cd /usr/local/src/
wget https://sonarsource.bintray.com/Distribution/sonar-scanner-cli/sonar-scanner-2.6.1.zip
unzip sonar-scanner-2.6.1.zip
mv sonar-scanner-2.6.1 /usr/local/
ln -s /usr/local/sonar-scanner-2.6.1/ /usr/local/sonar-scanner
7.2、配置 Sonar Scanner
# vim /usr/local/sonar-scanner/conf/sonar-scanner.properties
sonar.host.url=http://localhost:9000
sonar.sourceEncoding=UTF-8
7.3、分析项目代码
如果你要使用 Sonar Scanner 分析一个项目代码,需要在项目的根路径下防止一个配置文件 sonar-project.properties ,这个配置文件用来描述项目的相关信息,如项目名称、版本等。
例子如下:http://docs.sonarqube.org/display/SONAR/Analysis+Parameters
# must be unique in a given SonarQube instance
sonar.projectKey=my:project
# this is the name displayed in the SonarQube UI
sonar.projectName=My project
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=.
# Encoding of the source code. Default is default system encoding
#sonar.sourceEncoding=UTF-8
使用官方测试项目进行实践
官方为各种常用语言提供了代码案例,可以进行测试 Sonar 的相关功能。
cd /usr/local/src
wget https://github.com/SonarSource/sonar-examples/archive/master.zip -O sonar-examples.zip
unzip sonar-examples.zip
cd sonar-examples-master/projects/languages/php/php-sonar-runner-unit-tests/
ls -lh
可以看到官方提供的案例中的 sonar-project.properties 的例子
cat sonar-project.properties
# Required metadata
sonar.projectKey=org.sonarqube:php-simple-sq-scanner
sonar.projectName=PHP :: Simple Project :: SonarQube Scanner
sonar.projectVersion=1.0
# Comma-separated paths to directories with sources (required)
sonar.sources=src
# Language
sonar.language=php
# Encoding of the source files
sonar.sourceEncoding=UTF-8