博客有些好些時間未更新了,這幾個月的時間裏,離開了實習的公司、大學畢了業、來了新公司、轉了戶口,有點忙,最近總算稍微閒下來了,打算從新拾起博客,堅持寫下去。html
言歸正轉,什麼是SonarQube ?java
SonarQube(曾用名Sonar(聲納))是一個優秀的開源代碼分析系統管理系統,支持超過25+種編程語言,對.Net Core固然也是支持的。mysql
最近公司作的項目是用的Framework開發的,久仰SonarQube大名,今天在本地搭建SonarQube以後對項目進行分析,效果驚人。揪出了系統中潛藏的若干Bug,功不可沒,因此在這裏搭建的方法分享給你們,但願對你們有所幫助。git
在網上找一些資料,關於Sonar的介紹在Linux平臺下較多,因此我下面的介紹主要是基於Win平臺的,其餘平臺大同小異。github
安裝Sonar主要有如下幾步:sql
安裝JAVA SDK
Sonar是一款基於JAVA開發的工具,安裝JAVA SDK的過程在此再也不敘述,建議安裝好以後配置好JAVA_HOME的環境變量,如下是下載地址。數據庫
http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html編程
安裝SonarQube
首先到官網下載安裝包,值得注意的是,該安裝包是不分平臺的,下載下來以後,選擇Windows的文件夾中StartSonar.bat文件運行便可。api
https://www.sonarqube.org/#downloads服務器
若是java環境安裝正常,Sonar應該是能正常啓動的,啓動後瀏覽。啓動效果以下:
剛剛裝好是英文的,我是安裝了中文包,如何安裝中文包,後面會敘述。
配置Sonar
咱們須要對Sonar進行簡單配置,使其能鏈接上MySQL數據庫。
打開MySQL數據庫,執行如下指令。
CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci; CREATE USER 'sonar' IDENTIFIED BY 'sonar'; GRANT ALL ON sonar.* TO 'sonar'@'%' IDENTIFIED BY 'sonar'; GRANT ALL ON sonar.* TO 'sonar'@'localhost' IDENTIFIED BY 'sonar'; FLUSH PRIVILEGES;
該操做是爲Sonar建立數據庫並添加該數據庫的用戶,數據庫名稱是sonar ,用戶名是sonar,密碼是sonar。
打開sonar.properties將內容替換成以下:
sonar.jdbc.username=sonar sonar.jdbc.password=sonar sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false
其中sonar.jbc.url是mysql數據庫的鏈接字符串。
從新啓動Sonar(關閉運行startsonar.bat控制檯,並在任務管理器中關閉全部和java有關的進程,從新運行startsonor.bat),使用管理員帳戶登陸(admin/admin)。
登陸以後,安裝中文包,以下,安裝以後須要點擊從新啓動,啓動以後,Sonar就變成中文的了。
Sonar-Scanner for MSBuild安裝與配置
下載並解壓SonarQube Scanner for MSBuild,它是C# Framework的分析插件。
解壓以後,設置SonarQube Scanner for MSBuild的環境變量,如個人解壓路徑是:C:\MyWorkSpace\Tools\sonar-scanner-msbuild-4.3.1.1372-net46,則把該路徑添加到path下:
修改SonarQube.Analysis.xml文件
要修改的地方只是關於sonarQube服務器的一些配置,關於服務器URL、USER、PASSWORD等,修改以下:
<?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 SonarScanner.MSBuild.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://localhost:9000</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:mysql://localhost: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> </SonarQubeAnalysisProperties>
接下來,重要的一步,找到你電腦中的MSBuild.exe並添加到path環境變量,便於後面在命令行中調用MSBuild,個人是在vs 2017的安裝目錄下
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\amd64
C# 項目分析
CMD進入C#項目所在的根目錄,依此執行如下三條命令。
MSBuild.SonarQube.Runner.exe begin /k:"xxh.xzc.api" /n:"xhh.xzc.api" /v:"1.0"
MSBuild.exe /t:Rebuild
MSBuild.SonarQube.Runner.exe end
參數說明:
/key(簡寫k):對應projectKey即項目的惟一代碼,如兩套源代碼使用同一個projectKey那掃描的結果將混在一塊兒,因此一個項目須要有一個單獨的projectKey
/name(簡寫n):對應projectName即項目的名稱,爲項目的一個顯示的名稱,創建使用完整的項目名稱
/version(簡寫v):對應projectVersion即項目的版本,項目在不一樣的時期版本也是不同的,若是方便,能夠在sonarQube的服務器中查看到不一樣的版本代碼其中問題的變化
三條命令分別是分析的前期準備,MSBuild編譯,將報告上傳給SonarQube。
查看分析結果
最後,進入http://localhost:9000/projects 查看分析結果吧,驚喜不驚喜?
界面中功能強大,不少認爲絕對發現不了的Bug都展示出來了,還能夠查看單元測試的覆蓋率,相信若是堅持使用該工具,必定會對編碼習慣有很大幫助。
快快搭建一個SonarQube看看本身的代碼有沒有BUG!!
個人博客即將入駐「雲棲社區」,誠邀技術同仁一同入駐。
參考文獻:https://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner+for+MSBuild