1、安裝JDK和配置JAVA_HOME和CLASSPATH
2、安裝mysql數據庫
3、創建數據庫和用戶
mysql -u root -p
mysql> CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci;
mysql> CREATE USER 'sonar' IDENTIFIED BY 'sonar';
mysql> GRANT ALL ON sonar.* TO 'sonar'@'%' IDENTIFIED BY 'sonar';
mysql> GRANT ALL ON sonar.* TO 'sonar'@'localhost' IDENTIFIED BY 'sonar';
mysql> FLUSH PRIVILEGES;
4、下載sonarqube
wget https://sonarsource.bintray.com/Distribution/sonarqube/sonarqube-5.4.zip
unzip sonarqube-5.4.zip
vi /etc/profile
SONAR_HOME=/usr/jenkins/sonarqube-5.4
export SONAR_HOME
source /etc/profile
5、配置sonarqube
在sonarqube解壓包下編輯conf/sonar.properties文件,該文件中所有注釋掉的數據庫連接模板都是支持的,只需要去掉注釋更改為自己的數據庫連接即可,同時將默認的自帶數據庫連接注釋去掉。
一般需要配置以下屬性(這里使用MySQL數據庫,數據庫的引擎必須是innodb):
vi /usr/jenkins/sonarqube-5.4/conf/sonar.properties
sonar.jdbc.username=sonar
sonar.jdbc.password=sonar
sonar.jdbc.url=jdbc:mysql://127.0.0.1:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance
sonar.jdbc.maxActive=60
sonar.jdbc.maxIdle=5
sonar.jdbc.minIdle=2
sonar.jdbc.maxWait=5000
sonar.jdbc.minEvictableIdleTimeMillis=600000
sonar.jdbc.timeBetweenEvictionRunsMillis=30000
sonar.web.host=0.0.0.0
#修改snoar端口,默認為9000
sonar.web.port=9090
6、啟動sonarqube服務
/usr/jenkins/sonarqube-5.4/bin/linux-x86-64/sonar.sh console 啟動服務
/usr/jenkins/sonarqube-5.4/bin/linux-x86-64/sonar.sh start 啟動服務
/usr/jenkins/sonarqube-5.4/bin/linux-x86-64/sonar.sh stop 停止服務
/usr/jenkins/sonarqube-5.4/bin/linux-x86-64/sonar.sh restart重啟服務
啟動成功后在瀏覽器地址欄輸入http://localhost:9090即可訪問snoarQube。
缺省用戶名和密碼是admin/admin
7、漢化sonarqube
https://github.com/SonarQubeCommunity/sonar-l10n-zh/releases/
安裝目錄:$SONAR_HOME/extensions/plugins
wget https://github.com/SonarQubeCommunity/sonar-l10n-zh/releases/download/sonar-l10n-zh-plugin-1.9/sonar-l10n-zh-plugin-1.9.jar
8、在jenkins平台上安裝插件SonarQube Scanner for Jenkins
9、在jenkins配置SonarQube

如果設置為5.3 or higher選擇,就必須使用sonarQube account token進行連接,這個更安全。
Server authentication token項需要在sonar server端生成 ;訪問sonar server端
菜單==》配置==》權限==》用戶==》TOKENS==》Generate 生成token,將生成token添加到此處,完成添加。

10、下載安裝和配置sonar scanner
https://sonarsource.bintray.com/Distribution/sonar-scanner-cli/
wget https://sonarsource.bintray.com/Distribution/sonar-scanner-cli/sonar-scanner-2.8.zip
vi /home/jenkins/.bash_profile
SONAR_RUNNER_HOME=/usr/local/jenkins/sonar-scanner-2.8/
export PATH=$SONAR_RUNNER_HOME/bin:$PATH
source /home/jenkins/.bash_profile
測試是否安裝sonner scanner成功
輸入命令:sonar-runner -version出現以下信息,則表示環境變量設置成功

vi conf/sonar-scanner.properties
#Configure here general information about the environment, such as SonarQube DB details for example
#No information about specific project should appear here
#----- Default SonarQube server
sonar.host.url=http://localhost:9090
#----- 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
#----- MySQL
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8
11、在jenkins中配置sonar scanner
系統管理–》Global Tool Configuration

12、項目中添加sonar scanner分析
13、要讓jenkins編譯完成,自動將代碼傳給sonarqube進行分析,則要配置jenkins中的目標項目。
選擇自己的project==》配置==》add pre-build step

14、遇到的問題
在項目源碼根目錄執行命令時,可能會有svn錯誤,因為當sonar掃描的時候,會因為無法訪問.svn而掃描失敗,在sonar平台中用admin登錄后,點擊‘配置’-‘SCM’,如下圖:

關注公眾號獲取海量視頻

