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/,如下是其網站截圖:
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目錄下,其結構以下: php
根據系統的不一樣進入不一樣目錄下啓動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。以下是啓動後的界面: java
3.2配置SonarQube
3.2.1修改配置文件sonar-runner.properties
因爲SonarQube默認使用的H2數據庫僅爲演示使用,實際上登陸系統以後就會看到提示,以下:
「bedded database should be used for evaluation purpose onlymysql
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」,以下圖所示:
默認已經安裝了一些插件了,可是本人運行的時候提示我更新,因而本人就按照提示進行了更新,若是不習慣英文界面的朋友,能夠安裝中文語言包,以下圖所示:
注:因爲未知緣由有時候看不到這個插件,不過仍能夠去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
下面的截圖是本人當前項目分析的幾個報告:
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文件,文件內容以下:
linux
# 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來運行代碼分析了,若是沒有添加環境變量也是能夠執行分析的,命令以下:
git
cd /tmp/ simple-web / /usr/local/sonar-scanner-2.8/bin/sonar-scanner
若是不出錯的話,會看到以下提示:
這時,就能夠瀏覽器中打開SonarQube網站來觀看結果了,這是simple-web的代碼分析結果:
能夠看到這個項目被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,以下圖所示:
github
編輯C:\ sonar-scanner-msbuild-3.0.0.629目錄下的SonarQube.Analysis.xml文件,最終結果以下:
web
<?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文件必須包含這些字符,則必須分別寫成"&","<",">"再寫入文件中。
<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&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false</Property>
<Property Name="sonar.jdbc.username">sonar</Property>
<Property Name="sonar.jdbc.password">sonar</Property>
sql
</SonarQubeAnalysisProperties>
說明:
1.由於在宿主機上須要經過192.168.60.198來鏈接部署了SonarQube的虛擬機,所以須要將原來的localhost改成192.168.60.198;
2.由於在xml中"&","<",">"等特殊字符須要轉義,因此須要將sonar.jdbc.url中url部分含有「&」替換爲「&」。
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對應的。
apache
"C:\Program Files (x86)\MSBuild\14.0\Bin\MSBuild.exe" ITManageClient.sln /t:Rebuild
注:ITManageClient.sln是解決方案名字,若是該目錄下只存在一個解決方案文件則可省略。
效果以下圖所示:
最後執行:
"C:\ sonar-scanner-msbuild-3.0.0.629 \MSBuild.SonarQube.Runner.exe" end
執行結果以下圖所示:
至此,已完成對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三處博客發佈,本人會負責在此四處答疑。