基于Win10极简SonarQube C#代码质量分析
博客有些好些時間未更新了,這幾個月的時間里,離開了實習的公司、大學畢了業、來了新公司、轉了戶口,有點忙,最近總算稍微閑下來了,打算重新拾起博客,堅持寫下去。
言歸正轉,什么是SonarQube ?
?SonarQube(曾用名Sonar(聲納))是一個優秀的開源代碼分析系統管理系統,支持超過25+種編程語言,對.Net Core當然也是支持的。
最近公司做的項目是用的Framework開發的,久仰SonarQube大名,今天在本地搭建SonarQube之后對項目進行分析,效果驚人。揪出了系統中潛藏的若干Bug,功不可沒,所以在這里搭建的方法分享給大家,希望對大家有所幫助。
在網上找一些資料,關于Sonar的介紹在Linux平臺下較多,所以我下面的介紹主要是基于Win平臺的,其他平臺大同小異。
安裝Sonar主要有以下幾步:
安裝JAVA SDK
Sonar是一款基于JAVA開發的工具,安裝JAVA SDK的過程在此不再敘述,建議安裝好之后配置好JAVA_HOME的環境變量,以下是下載地址。
http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
?
安裝SonarQube
首先到官網下載安裝包,值得注意的是,該安裝包是不分平臺的,下載下來之后,選擇Windows的文件夾中StartSonar.bat文件運行即可。
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的分析插件。
https://github.com/SonarSource/sonar-scanner-msbuild/releases/download/4.3.1.1372/sonar-scanner-msbuild-4.3.1.1372-net46.zip
解壓之后,設置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 itis located (if it exists). It is possible to use another properties file by using the /s:filePath.xml flagThe 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.projectVersionThe 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
轉載于:https://www.cnblogs.com/CoderAyu/p/9416376.html
總結
以上是生活随笔為你收集整理的基于Win10极简SonarQube C#代码质量分析的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ConfigParser 模块
- 下一篇: oracle授权、表备份、用户管理