1、SonarQube 介紹
SonarQube 是一個(gè)用于代碼質(zhì)量管理的開放平臺(tái)灼捂。通過插件機(jī)制离例,Sonar 可以集成不同的測試工具,代碼分析工具悉稠,以及持續(xù)集成工具宫蛆。
與持續(xù)集成工具(如 Hudson、Jenkins 等)不同的猛,Sonar 并不是簡單的把不同的代碼檢查工具結(jié)果(如 FindBugs耀盗、PMD 等)直接顯示在 Web 頁面上,而是通過不同的插件對(duì)這些結(jié)果進(jìn)行再加工處理卦尊,通過量化的方式度量代碼質(zhì)量的變化叛拷,從而可以方便地對(duì)不同規(guī)模和種類的工程進(jìn)行代碼質(zhì)量管理。
在對(duì)其他工具的支持方面猫牡, Sonar 不僅提供了對(duì) IDE 的支持胡诗,可以在 Eclipse 和 IntelliJ IDEA 這些工具里聯(lián)機(jī)查看結(jié)果;同時(shí) Sonar 還對(duì)大量的持續(xù)集成工具提供了接口支持淌友,可以很方便的在持續(xù)集成中使用 Sonar。
此外骇陈,Sonar 的插件還可以對(duì) Java 以外的其他變成語言提供支持震庭,對(duì)國際化以及報(bào)告文檔化有良好的支持。
Sonar 的相關(guān)下載和文檔可以在下面的鏈接中找到:
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婿崭、準(zhǔn)備 Sonar 數(shù)據(jù)庫
為 Sonar 準(zhǔn)備 MySQL 數(shù)據(jù)庫拨拓,版本必須大于 5.6。
4.1氓栈、下載 mysql 二進(jìn)制包
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渣磷、創(chuàng)建 mysql 用戶
groupadd mysql
useradd -r -g mysql -s /bin/false mysql
4.3、解壓 mysql 二進(jìn)制包
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醋界、啟動(dòng) mysql
/usr/local/mysql/bin/mysqld_safe --defaults-file=/usr/local/mysql/my.cnf &
4.6、創(chuàng)建sonar數(shù)據(jù)庫
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提完、配置并啟動(dòng) Sonar
sonar 的配置文件主要是配置相關(guān)啟動(dòng)參數(shù)和數(shù)據(jù)庫的信息形纺,數(shù)據(jù)庫不需要初始化,Sonar 啟動(dòng)的過程中會(huì)自動(dòng)初始化徒欣。
# cd /usr/local/sonarqube/conf/
# ls
sonar.properties wrapper.conf
編寫配置文件逐样,修改數(shù)據(jù)庫配置
# 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 訪問數(shù)據(jù)庫驅(qū)動(dòng)
"""
默認(rèn)情況 Sonar 有自帶的嵌入數(shù)據(jù)庫,如果需要使用類似 Oracle 數(shù)據(jù)庫,必須手動(dòng)復(fù)制驅(qū)動(dòng) jar 包到 ${SONAR_HOME}/extensions/jdbc-driver/oracle 目錄下脂新,其他支持的數(shù)據(jù)庫默認(rèn)提供了驅(qū)動(dòng)秽澳。
其他數(shù)據(jù)庫的配置可以參考官方文檔:[http://docs.sonarqube.org/display/HOME/SonarQube+Platform](http://docs.sonarqube.org/display/HOME/SonarQube+Platform)
"""
啟動(dòng) Sonar,可以在 Sonar 的配置文件來配置 Sonar Web 監(jiān)聽的 IP 地址和端口戏羽,默認(rèn)是 9000 端口担神。
# vim sonar.properties
sonar.web.host=0.0.0.0
sonar.web.port=9000
啟動(dòng)
/usr/local/sonarqube/bin/linux-x86-64/sonar.sh start
6、Sonar 插件使用
6.1始花、安裝中文插件
Sonar 和 Jenkins 一樣有豐富的插件妄讯,而且插件的安裝也比較簡單,這里使用 Sonar 中文支持的例子酷宵,介紹 Sonar 插件管理的兩種方法:
第一種方法:手動(dòng)下載插件到相應(yīng)目錄下
下載中文 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 界面安裝插件
默認(rèn)密碼:admin / admin
配置→ 系統(tǒng) → 更新中心 → Available → 訓(xùn)責(zé) Chinese Pack亥贸。
6.2、安裝語言插件
默認(rèn)情況 SonarQube 安裝成功后只能分析 Java浇垦,如果想讓它分析其他語言需要安裝插件炕置,例如安裝常用的 Python、PHP男韧、CSS朴摊、JavaScript 等你需要的其他語言。如果由于網(wǎng)絡(luò)問題使用 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 進(jìn)行分析
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韭寸、分析項(xiàng)目代碼
如果你要使用 Sonar Scanner 分析一個(gè)項(xiàng)目代碼春哨,需要在項(xiàng)目的根路徑下防止一個(gè)配置文件 sonar-project.properties ,這個(gè)配置文件用來描述項(xiàng)目的相關(guān)信息恩伺,如項(xiàng)目名稱赴背、版本等。
例子如下: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
使用官方測試項(xiàng)目進(jìn)行實(shí)踐
官方為各種常用語言提供了代碼案例莫其,可以進(jìn)行測試 Sonar 的相關(guān)功能癞尚。
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