SonarQube的安裝和使用

1、SonarQube 介紹

SonarQube 是一個用于代碼質(zhì)量管理的開放平臺。通過插件機制,Sonar 可以集成不同的測試工具,代碼分析工具,以及持續(xù)集成工具。

與持續(xù)集成工具(如 Hudson、Jenkins 等)不同,Sonar 并不是簡單的把不同的代碼檢查工具結(jié)果(如 FindBugs、PMD 等)直接顯示在 Web 頁面上,而是通過不同的插件對這些結(jié)果進行再加工處理,通過量化的方式度量代碼質(zhì)量的變化,從而可以方便地對不同規(guī)模和種類的工程進行代碼質(zhì)量管理。

在對其他工具的支持方面, Sonar 不僅提供了對 IDE 的支持,可以在 Eclipse 和 IntelliJ IDEA 這些工具里聯(lián)機查看結(jié)果;同時 Sonar 還對大量的持續(xù)集成工具提供了接口支持,可以很方便的在持續(xù)集成中使用 Sonar。

此外,Sonar 的插件還可以對 Java 以外的其他變成語言提供支持,對國際化以及報告文檔化有良好的支持。

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、準備 Sonar 數(shù)據(jù)庫

為 Sonar 準備 MySQL 數(shù)據(jù)庫,版本必須大于 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、創(chuàng)建 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、創(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、配置并啟動 Sonar

sonar 的配置文件主要是配置相關(guān)啟動參數(shù)和數(shù)據(jù)庫的信息,數(shù)據(jù)庫不需要初始化,Sonar 啟動的過程中會自動初始化。

# 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ū)動

"""
默認情況 Sonar 有自帶的嵌入數(shù)據(jù)庫,如果需要使用類似 Oracle 數(shù)據(jù)庫,必須手動復制驅(qū)動 jar 包到 ${SONAR_HOME}/extensions/jdbc-driver/oracle 目錄下,其他支持的數(shù)據(jù)庫默認提供了驅(qū)動。

其他數(shù)據(jù)庫的配置可以參考官方文檔:[http://docs.sonarqube.org/display/HOME/SonarQube+Platform](http://docs.sonarqube.org/display/HOME/SonarQube+Platform)
"""

啟動 Sonar,可以在 Sonar 的配置文件來配置 Sonar Web 監(jiān)聽的 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 插件管理的兩種方法:
第一種方法:手動下載插件到相應(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 界面安裝插件

默認密碼:admin / admin

配置→ 系統(tǒng) → 更新中心 → Available → 訓責 Chinese Pack。

6.2、安裝語言插件

默認情況 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 進行分析

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 ,這個配置文件用來描述項目的相關(guān)信息,如項目名稱、版本等。

例子如下: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 的相關(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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
禁止轉(zhuǎn)載,如需轉(zhuǎn)載請通過簡信或評論聯(lián)系作者。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容