天天看點

開發人員學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/,以下是其網站截圖:

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目錄下,其結構如下:

根據系統的不同進入不同目錄下啟動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。如下是啟動後的界面:

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”,如下圖所示:

預設已經安裝了一些插件了,但是本人運作的時候提示我更新,于是本人就按照提示進行了更新,如果不習慣英文界面的朋友,可以安裝中文語言包,如下圖所示:

注:由于未知原因有時候看不到這個插件,不過仍可以去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目錄。

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檔案,檔案内容如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

<code># must be unique in a given SonarQube instance</code>

<code>sonar.projectKey=my:simple-web</code>

<code># this is the name displayed in the SonarQube UI</code>

<code>sonar.projectName=simple-web</code>

<code>sonar.projectVersion=1.0</code>

<code>sonar.language=java</code>

<code> </code> 

<code># Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.</code>

<code># Since SonarQube 4.2, this property is optional if sonar.modules is set.</code>

<code># If not set, SonarQube starts looking for source code from the directory containing</code>

<code># the sonar-project.properties file.</code>

<code>sonar.sources=src</code>

<code># Encoding of the source code. Default is default system encoding</code>

<code>sonar.sourceEncoding=UTF-8</code>

其中:

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來運作代碼分析了,如果沒有添加環境變量也是可以執行分析的,指令如下:

<code>cd</code> <code>/tmp/</code> <code>simple-web /</code>

<code>/usr/local/sonar-scanner-2</code><code>.8</code><code>/bin/sonar-scanner</code>

如果不出錯的話,會看到如下提示:

這時,就可以浏覽器中打開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,如下圖所示:

編輯C:\ sonar-scanner-msbuild-3.0.0.629目錄下的SonarQube.Analysis.xml檔案,最終結果如下:

<code>&lt;?</code><code>xml</code> <code>version</code><code>=</code><code>"1.0"</code> <code>encoding</code><code>=</code><code>"utf-8"</code> <code>?&gt;</code><code>&lt;!--  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]</code>

<code>--&gt;</code><code>&lt;</code><code>SonarQubeAnalysisProperties</code>  <code>xmlns:xsi</code><code>=</code><code>"http://www.w3.org/2001/XMLSchema-instance"</code> <code>xmlns:xsd</code><code>=</code><code>"http://www.w3.org/2001/XMLSchema"</code> <code>xmlns</code><code>=</code><code>"http://www.sonarsource.com/msbuild/integration/2015/1"</code><code>&gt;</code>

<code>  </code><code>&lt;</code><code>Property</code> <code>Name</code><code>=</code><code>"sonar.host.url"</code><code>&gt;http://192.168.60.198:9000&lt;/</code><code>Property</code><code>&gt;  </code><code>&lt;!--  &lt;Property Name="sonar.login"&gt;&lt;/Property&gt;  &lt;Property Name="sonar.password"&gt;&lt;/Property&gt;  --&gt;</code>  <code>&lt;</code><code>Property</code> <code>Name</code><code>=</code><code>"sonar.login"</code><code>&gt;admin&lt;/</code><code>Property</code><code>&gt;  &lt;</code><code>Property</code> <code>Name</code><code>=</code><code>"sonar.password"</code><code>&gt;admin&lt;/</code><code>Property</code><code>&gt;    </code><code>&lt;!-- Required only for versions of SonarQube prior to 5.2 --&gt;</code>  <code>&lt;!--  &lt;</code><code>Property</code> <code>Name</code><code>=</code><code>"sonar.jdbc.url"</code><code>&gt;jdbc:jtds:sqlserver://mySqlServer/sonar;instance=SQLEXPRESS;SelectMethod=Cursor&lt;/</code><code>Property</code><code>&gt;</code>

由于XML實體中不允許出現"&amp;","&lt;","&gt;"等特殊字元,否則XML文法檢查時将出錯,如果編寫的XML檔案必須包含這些字元,則必須分别寫成"&amp;amp;","&amp;lt;","&amp;gt;"再寫入檔案中。

  &lt;Property Name="sonar.jdbc.url"&gt;jdbc:mysql://192.168.60.198:3306/sonar?useUnicode=true&amp;characterEncoding=utf8&amp;rewriteBatchedStatements=true&amp;useConfigs=maxPerformance&amp;useSSL=false&lt;/Property&gt;

  --&gt;

  &lt;Property Name="sonar.jdbc.url"&gt;jdbc:mysql://192.168.60.198:3306/sonar?useUnicode=true&amp;amp;characterEncoding=utf8&amp;amp;rewriteBatchedStatements=true&amp;amp;useConfigs=maxPerformance&amp;amp;useSSL=false&lt;/Property&gt;

  &lt;Property Name="sonar.jdbc.username"&gt;sonar&lt;/Property&gt;

  &lt;Property Name="sonar.jdbc.password"&gt;sonar&lt;/Property&gt;

&lt;/SonarQubeAnalysisProperties&gt;

說明:

1.因為在主控端上需要通過192.168.60.198來連接配接部署了SonarQube的虛拟機,是以需要将原來的localhost改為192.168.60.198;

2.因為在xml中"&amp;","&lt;","&gt;"等特殊字元需要轉義,是以需要将sonar.jdbc.url中url部分含有“&amp;”替換為“&amp;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

首先執行如下指令:

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

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

執行結果如下圖所示:

接着執行:

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

注:ITManageClient.sln是解決方案名字,如果該目錄下隻存在一個解決方案檔案則可省略。

效果如下圖所示:

最後執行:

<code>"C:\ sonar-scanner-msbuild-3.0.0.629 \MSBuild.SonarQube.Runner.exe"</code> <code>end</code>

至此,已完成對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涉及的知識點非常多,在本篇并沒有一一展開,更多的知識點需要大家去自行學習掌握了。

本文轉自周金橋51CTO部落格,原文連結: http://blog.51cto.com/zhoufoxcn/1948103,如需轉載請自行聯系原作者