SonarQube代碼質量管理平台安裝與配置


1.安裝說明

PS:為什么要有安裝說明?本人在網上找資料的時候發現很多教程是沒有交代清楚安裝環境的。所以,不清楚他們的教程是否適合我的環境。明確『安裝說明』方便網友了解我的配置環境,便於大家深入理解此教程。

虛擬機:VmWare workstation 12

操作用戶:root

系統環境:centos6.5-x86_64(最小化安裝)

軟件下載目錄:/root/opt

================

2.預置條件

1.需要JDK1.6+支持;

2.需要Mysql數據庫支持;(雖然SonarQube自帶了H2數據庫,但是為了方便管理數據推薦使用Mysql數據庫)

多說一句:JDK是必須安裝的,Mysql數據庫並不是必須要安裝的,如果公司有Mysql服務器,直接拿來使用就行,這次就沒有安裝Mysql直接用公司內網提供的Mysql數據庫。

================

3.安裝

在安裝之前,介紹一下SonarQube和SonarQube Runner之間的關系。

SonarQube是服務器端,它主要有兩個功能:1.分析源代碼;2.因為它內嵌了Apache模塊,所以提供Web端的界面訪問。

SonarQube Runner是一個利用SonarQube服務端分析代碼的命令行工具,可以把它簡單理解為客戶端。

下載地址:http://www.sonarqube.org/downloads/

所以,為了安裝和調試方便,建議SonarQube和SonarQube Runner都下載。

3.1安裝SonarQube

第一步:將下載的http://downloads.sonarsource.com/sonarqube/sonarqube-5.2.zip解壓后放到/usr/local目錄下。具體步驟如下:

[root@code-test opt]# wget -c http://downloads.sonarsource.com/sonarqube/sonarqube-5.2.zip

[root@code-test opt]# unzip -n sonarqube-5.2.zip -d /usr/local

第二步:配置環境變量

[root@code-test ~]# vi + /etc/profile

添加

SONAR_HOME=/usr/local/sonarqube-5.2

export SONAR_HOME

保存退出並使配置生效

[root@code-test ~]# source /etc/profile

第三步:配置sonar.properties

[root@code-test ~]# vi /usr/local/sonarqube-5.2/conf/sonar.properties

打開后,找到

sonar.host.url=http://localhost:80

sonar.jdbc.username=sonar

sonar.jdbc.password=sonar

sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance

sonar.web.host=0.0.0.0

sonar.web.context=

sonar.web.port=80

PS:SonarQube默認監聽9000端口。由於我這個虛擬機打算專門給Sonar使用。所以,修改成了80.sonar.jdbc.url中的連接地址也修改成了內網中Mysql服務器的地址。

第四步:啟動服務

[root@code-test ~]# cd /usr/local/sonarqube-5.2/bin/linux-x86-64

[root@code-test ~]# ./sonar.sh start

另外,啟動/停止/重啟命令如下: 
#./sonar.sh start   啟動服務 
#./sonar.sh stop    停止服務 
#./sonar.sh restart 重啟服務

第四步:訪問SonarQube Web管理界面。如果能夠看到這個界面證明SonarQube安裝成功啦。

 

3.2安裝SonarQube Runner

第一步:將下載的http://repo1.maven.org/maven2/org/codehaus/sonar/runner/sonar-runner-dist/2.4/sonar-runner-dist-2.4.zip解壓后放到/usr/local目錄下。具體步驟如下:

[root@code-test opt]# wget -c http://repo1.maven.org/maven2/org/codehaus/sonar/runner/sonar-runner-dist/2.4/sonar-runner-dist-2.4.zip

[root@code-test opt]# unzip -n sonar-runner-dist-2.4.zip -d /usr/local

第二步:配置環境變量

[root@code-test ~]# vi + /etc/profile

添加

SONAR_RUNNER_HOME=/usr/local/sonar-runner-2.4/

PATH=.:$SONAR_RUNNER_HOME/bin

export SONAR_RUNNER_HOME

 

#包含sonar和sonar-runner的環境變量設置

export SONAR_HOME=/usr/local/sonarqube-5.2

export SONAR_RUNNER_HOME=/usr/local/sonar-runner-2.4 export

PATH=$PATH:$SONAR_HOME/bin:$SONAR_RUNNER_HOME/bin

保存並退出

[root@code-test ~]# source /etc/profile

第三步:配置sonar-runner.properties

[root@code-test conf]# vi /usr/local/sonar-runner-2.4/conf/sonar-runner.properties

找到

sonar.host.url=http://localhost:80

sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8

sonar.jdbc.username=sonar

sonar.jdbc.password=sonar

sonar.login=admin

sonar.password=admin

將前面的#去掉

PS:剛才我們已經看到SonarQube已經可以訪問了,所以就將sonar.host.url改成了實際的訪問地址。

第四步:運行sonar-runner分析源代碼

Sonar官方已經提供了非常全的代碼樣例便於新手入門用。

下載地址:https://github.com/SonarSource/sonar-examples/archive/master.zip

下載后使用unzip解壓。進入php執行sonar-runner命令即可。操作命令如下:

[root@code-test opt]# wget -c https://github.com/SonarSource/sonar-examples/archive/master.zip

[root@code-test opt]# unzip master.zip

[root@code-test opt]# cd sonar-examples-master/projects/languages/php/php-sonar-runner

[root@code-test php-sonar-runner]# sonar-runner

如果能夠看到下面的輸出信息,證明你的SonarQube Runner安裝並配置正確啦。

INFO: ------------------------------------------------------------------------

INFO: EXECUTION SUCCESS

INFO: ------------------------------------------------------------------------

Total time: 2:59.167s

Final Memory: 17M/204M

INFO: ------------------------------------------------------------------------

第五步:看看SonarQube的Web界面,是否已經可以看到分析的結果啦。

 

SonarQube默認是沒有安裝中文語言包的。可以看到我的截圖顯示的是中文,因為我安裝了中文語言包。如何安裝語言包呢。進入SonarQube插件目錄,下載語言包即可。步驟如下:

[root@code-test ~]# cd /usr/local/sonarqube-5.1.1/extensions/plugins

[root@code-test plugins]# wget -c http://repo1.maven.org/maven2/org/codehaus/sonar-plugins/l10n/sonar-l10n-zh-plugin/1.8/sonar-l10n-zh-plugin-1.8.jar

PS:最新版本的語言包是sonar-l10n-zh-plugin-1.8.jar。

這是中文語言包的源碼地址:https://github.com/SonarCommunity/sonar-l10n-zh

如果大家有興趣可以和老外一起來維護這個源碼,如果想體驗最新的源碼直接下載下來,打包成jar就可以啦

================

4.配置時踩的那些坑

Sonar是一個非常不錯的代碼分析工具。但是網上很多教程講的都是Jenkins+Sonar的配置。如果按照網上的教程去做,你會崩潰死的.為什么?原因如下:

1.網上很多教程的Jenkins+Sonar是基於對Java源代碼進行分析的。所以,如果做php代碼分析,有一個非常重要的步驟是裝php插件。而如果將Jenkins+Sonar合在一起配置,坑很多,填到你崩潰為止;

2.Jenkins是一款自動化構建的工具,如果是第一次用,坑也非常多。

拋開Jenkins不談,直接用Sonnar就可以對源代碼進行質量分析。所以,你需要在Centos下先把Sonnar跑通,讓它能在命令行下分析源代碼。確定這一步沒有問題啦,再跟Jenkins做持續集成。

我就是一開始Jenkins+Sonar一起配置,忙了半天,還是配置不好。不是這出問題,就是那里出問題。

================

5.SonarQube可以分析的語言

SonarQube可以分析當下最常用、流行的語言。如:Ruby,Python,Php,Css,Javascript,Java,Go,Web,C#。支持20多種語言。SonarQube安裝成功后默認只能分析Java,如果想讓它分析其他語言需要安裝插件。

支持分析編程語言插件地址:

http://docs.sonarqube.org/display/PLUG/Plugin+Library

如果你是做Web開發的,可以選擇安裝Php,Css,Javascript,Web這四款就行啦。直接進入到SonarQube的Plugin目錄下載,成功后重啟SonarQube即可。

例如:安裝Php分析插件

[root@code-test ~]# cd /usr/local/sonarqube-5.1.1/extensions/plugins

[root@code-test plugins]# wget -c http://downloads.sonarsource.com/plugins/org/codehaus/sonar-plugins/php/sonar-php-plugin/2.6/sonar-php-plugin-2.6.jar

================

6.參考鏈接

http://blog.csdn.net/hunterno4/article/details/11687269

http://wenku.baidu.com/view/a5c2a3357375a417876f8f09.html

http://my.oschina.net/zj0303/blog/301474

http://www.cnblogs.com/gao241/p/3190701.html

 

備注:

漢化包sonar-l10n-zh-plugin-1.6.jar放在下面這個目錄

/usr/local/sonarqube-5.2/extensions/plugins

然后重啟sonar才能生效

cd /usr/local/sonarqube-5.2/bin/linux-x86-64

./sonar.sh  start


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM