開發人員學Linux(10):CentOS7安裝配置代碼質量管理平臺SonarQube6.4

1.前言
上一章講述瞭如何配置使用源代碼管理工具SVN並使之與Apache集成,從而實現代碼的變更可追溯,雖然在大多數團隊裏強調代碼提交之前必須找團隊中經驗豐富的人來審覈通過後方可提交,但這一條有時候不是所有時候都能得到滿足,有沒有依賴於機制而不是人來保證代碼質量呢,我們知道計算機的缺點也是優點之一就是可以忠實執行指令。答案是有的,那就是SonarQube,其官方網址爲:https://www.sonarqube.org/,目前最新版本爲6.4。
SonarQube是一個開源平臺,用於管理源代碼的質量。Sonar 不只是一個質量數據報告工具,更是代碼質量管理平臺。支持的語言包括:Java、PHP、C#、C、Cobol、PL/SQL、Flex 等。主要特點:
代碼覆蓋:通過單元測試,將會顯示哪行代碼被選中
改善編碼規則
搜尋編碼規則:按照名字,插件,激活級別和類別進行查詢
項目搜尋:按照項目的名字進行查詢
對比數據:比較同一張表中的任何測量的趨勢
本篇將講述如何在CentOS7下配置使用SonarQube。
2.準備
2.1前置條件
安裝SonarQube需要服務器上已經安裝Java運行環境和數據庫,SonarQube支持的數據庫有Oracle、MySQL、PostgreSQL、SQL Server等,考慮這個系列下來我們已經在CentOS7上安裝了Oracle JDK和MySQL5.17.8,因此我們在此基礎上安裝SonarQube,請大家回憶一下《開發人員學Linux(3):CentOS7中安裝JDK8和Tomcat8》和《開發人員學Linux(6):CentOS7編譯安裝MySQL5.17.8多實例及主從複製》,對於SonarQube來說,Tomcat和MySQL主從複製是非必須的。
另外需要注意的是:
1.運行SonarQube需要較大的內存,官方建議至少2G以上,我將我的虛擬機調整爲4核4G內存了;
2.運行SonarQube時官方要求如果是MySQL,要求數據庫默認引擎爲InnoDB而不是MyISAM,而在前面的章節中我們恰恰是配置的默認引擎爲MyISAM,需要打開/usr/local/mysql-5.7.18/data/3306/my.cnf將[mysqld]節點下的“default-storage-engine=MyISAM”改爲“default-storage-engine= InnoDB”。此外,還要求必須是UFT8編碼,這個在之前的配置文件已經配置爲UFT8,所以無需改動;
3.運行SonarQube6.4需要JDK8,我們之前安裝的就是Oracke JDK8,因此可以忽略。
4.瀏覽SonarQube結果需要IE 11/Microsoft Edge或FireFox、Chrome的最新版本。
2.2軟件準備
sonarqube-6.4.zip,下載地址:https://sonarsource.bintray.com/Distribution/sonarqube/sonarqube-6.4.zip
sonar-scanner-2.8.zip :https://sonarsource.bintray.com/Distribution/sonar-scanner-cli/sonar-scanner-2.8.zip
另外,如果需要使用SonarQube對C#進行代碼質量分析,需要下載sonar-scanner-msbuild和MSBuild,其中要求MSBuild在V14.0以上,不想安裝MSBuild的同學可以直接安裝Microsoft Build Tools 2015。
sonar-scanner-msbuild-3.0.0.629.zip,下載地址:
https://github.com/SonarSource/sonar-scanner-msbuild/releases/download/3.0.0.629/sonar-scanner-msbuild-3.0.0.629.zip,說實在很難下載,可以去我的百度網盤下載,網址:https://pan.baidu.com/s/1pLblLMz
Microsoft Build Tools 2015,下載地址:
https://download.microsoft.com/download/E/E/D/EEDF18A8-4AED-4CE0-BEBE-70A83094FC5A/BuildTools_Full.exe
通過上面提供的軟件已經滿足在命令行下對代碼進行分析的條件了,爲了方便IDE開發人員,還提供了SonarLintwei,其網址爲:http://www.sonarlint.org/,以下是其網站截圖:
 wKiom1lsEbHyZIPLAAGmYgX3vq4800.png-wh_50 
3.安裝
將下載的sonarqube-6.4.zip和sonar-scanner-2.8.zip上傳到CentOS7的root目錄下(也可通過wget下載,因爲本人經常搗騰虛擬機,而有些放在國外網站上的文件下載速度極慢,所以習慣下載到本地存放)。
3.1安裝SonarQube6.4
執行以下命令,將SonarQube安裝到/usr/local/ sonarqube-6.4目錄。
cd /root
unzip /root/sonarqube-6.4.zip –d /usr/local
啓動SonarQube的文件在/usr/local/sonarqube-6.4/bin目錄下,其結構如下:
wKioL1lsEcSyCbdiAADwdhGvFFE198.png-wh_50 

根據系統的不同進入不同目錄下啓動SonarQube,本人的CentOS7是64位的,因此在本人的虛擬機上啓動SonarQube的腳本爲:/usr/local/sonarqube-6.4/bin/linux-x86-64/sonar.sh。
啓動:/usr/local/sonarqube-6.4/bin/linux-x86-64/sonar.sh start
重啓:/usr/local/sonarqube-6.4/bin/linux-x86-64/sonar.sh restart
關閉:/usr/local/sonarqube-6.4/bin/linux-x86-64/sonar.sh stop
這是我們就可以從瀏覽器中打開SonarQube的結果頁面了,網址是:http://192.168.60.198:9000(這個IP是我的虛擬機外部訪問IP,在虛擬機內部可以通過http://localhost:9000訪問,因在外部操作方便故本人習慣於是用外部地址訪問,下同),默認管理員賬戶是:admin/admin。如下是啓動後的界面:
wKiom1lsEdSh10JaAAEJoCrO78A630.png-wh_50 

3.2配置SonarQube
3.2.1修改配置文件sonar-runner.properties
由於SonarQube默認使用的H2數據庫僅爲演示使用,實際上登錄系統之後就會看到提示,如下:
“bedded database should be used for evaluation purpose only

The embedded database will not scale, it will not support upgrading to newer versions of SonarQube, and there is no support for migrating your data out of it into a different database engine. ”
因此需要對SonarQube進行配置,這裏以MySQL5.17.8爲例:
通過vim /usr/local/sonarqube-6.4/conf/sonar.properties打開SonarQube的配置文件,作如下修改:
1.“sonar.jdbc.username=”改爲:“sonar.jdbc.username=sonar”
2.“sonar.jdbc.password=”改爲:“sonar.jdbc.password= sonar”
3.“sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false”去掉註釋,即刪除前面的“#”。
上面的用戶名和密碼是鏈接到SonarQube系統的所使用的數據庫的賬號信息,同時在數據庫url中指定了SonarQue所使用的數據庫爲sonar,如果與實際環境中不一致需要相應修改。
3.2.2MySQL數據設置
首先啓動數據庫:
/usr/local/mysql-5.7.18/data/3306/mysql start
然後登錄數據庫:
/usr/local/mysql-5.7.18/bin/mysql -u root -p -S ./data/3306/mysql.sock
輸入具有管理權限的賬號和密碼後,以此進行創建數據庫、授權用戶操作:
CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci;
GRANT ALL ON sonar.* TO 'sonar'@'%' IDENTIFIED BY 'sonar';  FLUSH PRIVILEGES;
經歷過Java開發的開發人員肯定清楚,Java訪問數據庫需要JDBC驅動,在/usr/local/sonarqube-6.4/lib/jdbc目錄下默認內置了h2/mssql/mysql/postgresql的JDBC驅動,如果是使用其它類型數據庫作爲SonarQube的數據,需要將對應的JDBC驅動下載並存放到指定目錄,如Oracle的JDBC驅動存放位置:/usr/local/sonarqube-6.4/extensions/jdbc-driver/oracle。
至此已完成了SonarQube和MySQL數據庫的配置工作,可以通過如下命令重啓:
/usr/local/sonarqube-6.4/bin/linux-x86-64/sonar.sh restart
重啓之後就不會出現要求更換數據庫的提示了。配置JDBC之後第一次運行時需要較長時間,因爲需要初始化數據庫。
3.3安裝插件
在完成SonarQube配置後,在虛擬機所在宿主機上通過http://192.168.60.198:9000並是用系統管理員賬號登錄後就可以安裝插件了。進入安裝插件的路徑是:“Administration”-“System”-“Update Center”,如下圖所示:
wKioL1lsEe2Bx7jEAAAgvWHNZiA757.png-wh_50 
默認已經安裝了一些插件了,但是本人運行的時候提示我更新,於是本人就按照提示進行了更新,如果不習慣英文界面的朋友,可以安裝中文語言包,如下圖所示:
wKioL1lsEf7CdvfFAACkyHhm4GI079.png-wh_50 
注:由於未知原因有時候看不到這個插件,不過仍可以去https://github.com/SonarQubeCommunity/sonar-l10n-zh下載插件然後按照說明放到對應目錄,最新插件下載地址爲:https://github.com/SonarQubeCommunity/sonar-l10n-zh/releases/tag/sonar-l10n-zh-plugin-1.16,按照本文情況將其放到/usr/local/sonarqube-6.4/extensions/plugins目錄下即可。
不過本人還是想盡量創造一些英文環境來鞏固英語,畢竟大量的系統和框架都是英文的,連其說明文檔也是英文的,因此在這裏就不安裝了。
3.4安裝sonar-scanner-2.8
執行以下命令,將SonarQube安裝到/usr/local/ sonar-scanner-2.8目錄。
cd /root
unzip /root/sonar-scanner-2.8.zip –d /usr/local
啓動sonar-scanner的文件在/usr/local/ sonar-scanner-2.8/bin目錄下,爲今後便於運行,可以將sonar-scanner的安裝路徑添加到/etc/profile中:
在其中增加:
export SONAR_SCANNER_HOME=/usr/local/sonar-scanner-2.8
並將sonar-scanner的bin文件夾添加到PATH變量,如下是本人機器上PATH的設置:
export PATH=$PATH:$JAVA_HOME/bin:/usr/local/apache-tomcat-8.5.15/bin:/usr/local/httpd-2.4.25/bin:/usr/local/svn/bin:/usr/local/php/bin:$SONAR_RUNNER_HOME/bin
然後執行source /etc/profile使配置生效。
3.5配置sonar-scanner-2.8
打開/usr/local/sonar-scanner-2.8/conf/sonar-scanner.properties根據實際安裝情況做修改,主要改動如下:
“#sonar.host.url=http://localhost:9000”改爲“sonar.host.url=http://localhost:9000”;(即去掉註釋符號“#”)
“#sonar.sourceEncoding=UTF-8”改爲“#sonar.sourceEncoding=UTF-8”;(即去掉註釋符號“#”,如果程序源代碼不是UTF-8則需要相應修改,建議程序源代碼文件編碼使用UTF-8)
“#sonar.jdbc.username=sonar”改爲“sonar.jdbc.username=sonar”; (即去掉註釋符號“#”)
“#sonar.jdbc.password=sonar”改爲“sonar.jdbc.password=sonar”;(即去掉註釋符號“#”)
“#sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8”改爲“sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8”;(即去掉註釋符號“#”)
上面配置的是SonarQube數據庫的信息,對代碼進行掃描之後需要將結果保存到數據庫;
至此,sonar-scanner已具備對代碼進行分析的條件了。
4.使用sonar-scanner
下面的截圖是本人當前項目分析的幾個報告:
wKioL1lsEiridwXMAAB52KUXez8137.png-wh_50 
4.1使用sonar-scanner分析代碼
sonar-scanner是SonarQube默認推薦的代碼分析工具,使用起來也相對簡單,分以下幾個步驟:
1.在要測試的項目中創建一個sonar-project.properties;
2.在sonar-project.properties文件中配置項目的相關信息,如項目的名稱、版本、編程所使用的語言、程序文件所使用的編碼等;
在這裏就一個簡單例子爲例:
將本人用Java寫的一個本人早年練手的Java EE項目simple-web來進行分析,將其上傳到服務器/tmp/目錄下;
在/tmp/ simple-web /目錄下創建sonar-project.properties文件,文件內容如下:

# must be unique in a given SonarQube instance
sonar.projectKey=my:simple-web
# this is the name displayed in the SonarQube UI
sonar.projectName=simple-web
sonar.projectVersion=1.0
sonar.language=java
 
# 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=src
 
# Encoding of the source code. Default is default system encoding
sonar.sourceEncoding=UTF-8


其中:
sonar.projectKey必須在整個SonarQube中全局唯一;
sonar.projectName用於在網頁中顯示的項目名稱;
sonar.projectVersion用於標識源代碼版本;
sonar.language用於標識項目源代碼所使用的開發語言;
sonar.sources用於指定源代碼在整個文件中的起始路徑;在Java Maven項目中默認都會有個src目錄存放源代碼,因此這裏的值爲“src”,當然如果不存在類似目錄結構,也可以用“.”標識當前路徑。
sonar.sourceEncoding:項目中源代碼文件的編碼,用Visual Studio創建的項目默認都是UTF-8編碼,用Eclipse作爲IDE的需要在IDE中進行設置,當然如果難以轉碼也可以用實際編碼;
3.由於已經將sonar-scanner的路徑洗洗添加到CentOS7的PATH環境變量中,所以可以直接sonar-runner來運行代碼分析了,如果沒有添加環境變量也是可以執行分析的,命令如下:

cd /tmp/ simple-web /
/usr/local/sonar-scanner-2.8/bin/sonar-scanner


如果不出錯的話,會看到如下提示:
wKioL1lsElCyPqz5AABp29bqHnk668.png-wh_50 
這時,就可以瀏覽器中打開SonarQube網站來觀看結果了,這是simple-web的代碼分析結果:
 wKiom1lsElygnsrXAACgmIgNhno401.png-wh_50
可以看到這個項目被SonarQube分析後的總體情況:項目總代碼行數,單元測試覆蓋率,bug數,可能會有問題的代碼,代碼編寫風格存在問題的總數、重複的代碼數。
在上述網頁點擊相應數據指標會看到比較明晰的數據來源。
當然,上述的分析結果只是建議,就像有人去小賣部買礦泉水被收5元,買的人跟老闆論理:“這上面都說建議零售價4元的。”老闆說:“我不接受建議”。我查看一下所列出的問題,比如catch了exception沒有記錄到日誌是會被看做vulnerability。
當然,有時候你可以自信沒有問題,但是被同一個組的其他人看到每次要解釋又很麻煩,那麼可以在原有規則上進行修改即可。
4.2分析C#代碼
對於使用Visual Studio創建的.net項目是需要使用MSBuild.exe來進行分析的,因此無法在Linux系統上進行分析,可以採用sonar-scanner-msbuild-2.2.0.24.zip在命令行下分析或者安裝SonarLint插件在Visual Studio中進行分析。這裏演示一下如何使用sonar-scanner-msbuild來進行分析。
4.2.1sonar-scanner-msbuild安裝配置
首先,將sonar-scanner-msbuild-3.0.0.629.zip下載到本地並進行解壓(前文已提供下載地址),本人將它解壓到C:\ sonar-scanner-msbuild-3.0.0.629,如下圖所示:

wKioL1lsEm7yzlK6AAEeGtnm2LY263.png-wh_50 
編輯C:\ sonar-scanner-msbuild-3.0.0.629目錄下的SonarQube.Analysis.xml文件,最終結果如下:

<?xml version="1.0" encoding="utf-8" ?><!--  This file defines properties which would be understood by the SonarQube Scanner for MSBuild, if not overridden (see below)  By default the MSBuild.SonarQube.Scanner.exe picks-up a file named SonarQube.Analysis.xml in the folder it  is located (if it exists). It is possible to use another properties file by using the /s:filePath.xml flag   The overriding strategy of property values is the following:  - A project-specific property defined in the MSBuild *.*proj file (corresponding to a SonarQube module) can override:  - A property defined in the command line (/d:propertyName=value) has which can override:  - A property defined in the SonarQube.Analysis.xml configuration file [this file] which can override:  - A property defined in the SonarQube User Interface at project level which can override:  - A property defined in the SonarQube User Interface at global level which can't override anything.   Note that the following properties cannot be set through an MSBuild project file or an SonarQube.Analysis.xml file:  sonar.projectName, sonar.projectKey, sonar.projectVersion  The following flags need to be used to set their value: /n:[SonarQube Project Name] /k:[SonarQube Project Key] /v:[SonarQube Project Version]
--><SonarQubeAnalysisProperties  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.sonarsource.com/msbuild/integration/2015/1">
  <Property Name="sonar.host.url">http://192.168.60.198:9000</Property>  <!--  <Property Name="sonar.login"></Property>  <Property Name="sonar.password"></Property>  -->  <Property Name="sonar.login">admin</Property>  <Property Name="sonar.password">admin</Property>    <!-- Required only for versions of SonarQube prior to 5.2 -->  <!--  <Property Name="sonar.jdbc.url">jdbc:jtds:sqlserver://mySqlServer/sonar;instance=SQLEXPRESS;SelectMethod=Cursor</Property>


由於XML實體中不允許出現"&","<",">"等特殊字符,否則XML語法檢查時將出錯,如果編寫的XML文件必須包含這些字符,則必須分別寫成"&amp;","&lt;","&gt;"再寫入文件中。
  <Property Name="sonar.jdbc.url">jdbc:mysql://192.168.60.198:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false</Property>
  -->
  <Property Name="sonar.jdbc.url">jdbc:mysql://192.168.60.198:3306/sonar?useUnicode=true&amp;characterEncoding=utf8&amp;rewriteBatchedStatements=true&amp;useConfigs=maxPerformance&amp;useSSL=false</Property>
  <Property Name="sonar.jdbc.username">sonar</Property>
  <Property Name="sonar.jdbc.password">sonar</Property>

</SonarQubeAnalysisProperties>
說明:
1.因爲在宿主機上需要通過192.168.60.198來連接部署了SonarQube的虛擬機,因此需要將原來的localhost改爲192.168.60.198;
2.因爲在xml中"&","<",">"等特殊字符需要轉義,所以需要將sonar.jdbc.url中url部分含有“&”替換爲“&amp;”。
3.MSBuild.exe需要14.0以上版本,如果本地安裝了Visual Studio 2015及以上版本則已經滿足條件,否則需要安裝Microsoft Build Tools 2015,Microsoft Build Tools 2015下載鏈接前文已經給出,下載到本地然後安裝。
4.2.2使用sonar-scanner-msbuild分析代碼
在命令下進入要到分析的項目的目錄,如本人要分析的項目的目錄爲:C:\ITManageSolution,然後找到MSBuild.exe的路徑,在本人機器上路徑爲:C:\Program Files (x86)\MSBuild\14.0\Bin\MSBuild.exe
首先執行如下命令:

"C:\sonar-scanner-msbuild-3.0.0.629\MSBuild.SonarQube.Runner.exe" begin /key:"my:ITManageSolution" /name:"ITManageSolution" /version:"0.9"


上述命令中的/key、/name、 /version是和前面提及的Java類型項目的代碼分析配置文件sonar-project.properties中的sonar.projectKey、sonar.projectName、sonar.projectVersion對應的。

執行結果如下圖所示:
wKioL1lsErLjLj-iAADBlNDpgPw967.png-wh_50 
接着執行:

"C:\Program Files (x86)\MSBuild\14.0\Bin\MSBuild.exe" ITManageClient.sln /t:Rebuild


注:ITManageClient.sln是解決方案名字,如果該目錄下只存在一個解決方案文件則可省略。
效果如下圖所示:
wKiom1lsEsaCwwDTAADfZTJnJZA032.png-wh_50 
最後執行:

"C:\ sonar-scanner-msbuild-3.0.0.629 \MSBuild.SonarQube.Runner.exe" end


執行結果如下圖所示:
wKioL1lsEt6Q34NCAADmjAw2Q6Y839.png-wh_50 
至此,已完成對ITManageSolution這個C#項目的代碼分析,在http://192.168.60.198:9000中可以看到分析報告。報告查看方法這裏就不再贅述了。
需要注意的是如果在C:\ITManageSolution目錄下存在多個解決方案文件,需要針對每個解決方案運行一次上述過程。這也很好理解,畢竟使用Visual Studio也是需要打開兩次分別編譯的,這樣一來在SonarQube中也是會顯示爲多個項目。
4.3與持續集成工具集成來分析代碼
在SonarQube中提及了多種代碼分析方式,包含:
SonarQube Scanner for MSBuild: 分析.NET項目代碼;
SonarQube Scanner for Maven: 在Maven項目中執行分析;
SonarQube Scanner for Gradle: 執行Gradle分析(抱歉,這種方式沒試過)
SonarQube Scanner for Ant: 在使用了Ant的項目中進行代碼分析;
SonarQube Scanner For Jenkins: 在Jenkins中執行代碼分析
SonarQube Scanner:以命令行形式執行代碼分析
在本篇中講述了SonarQube Scanner和SonarQube Scanner for MSBuild兩種方式,SonarQube Scanner For Jenkins將會在下一篇講述Jenkins的時候講,其它方式需要大家去嘗試了。
5.總結
SonarQube是一個通過插件來支持對多種開發語言編寫的項目進行分析的開源代碼質量管理平臺,在本篇講述瞭如何在CentOS7上進行安裝和配置及使用SonarQube,在SonarQube支持的六種代碼分析方式中,以SonarQube Scanner和SonarQube Scanner for MSBuild爲例分別講述了對Java項目和C#項目進行代碼質量分析。由於SonarQube涉及的知識點非常多,在本篇並沒有一一展開,更多的知識點需要大家去自行學習掌握了。


聲明:本文首發於本人個人微信訂閱號:zhoujinqiaoIT,其後會同時在本人的CSDN、51CTO及oschina三處博客發佈,本人會負責在此四處答疑。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章