基于Win10极简SonarQube C#代码质量分析
博客有些好些時間未更新了,這幾個月的時間里,離開了實(shí)習(xí)的公司、大學(xué)畢了業(yè)、來了新公司、轉(zhuǎn)了戶口,有點(diǎn)忙,最近總算稍微閑下來了,打算重新拾起博客,堅(jiān)持寫下去。
言歸正轉(zhuǎn),什么是SonarQube ?
?SonarQube(曾用名Sonar(聲納))是一個優(yōu)秀的開源代碼分析系統(tǒng)管理系統(tǒng),支持超過25+種編程語言,對.Net Core當(dāng)然也是支持的。
最近公司做的項(xiàng)目是用的Framework開發(fā)的,久仰SonarQube大名,今天在本地搭建SonarQube之后對項(xiàng)目進(jìn)行分析,效果驚人。揪出了系統(tǒng)中潛藏的若干Bug,功不可沒,所以在這里搭建的方法分享給大家,希望對大家有所幫助。
在網(wǎng)上找一些資料,關(guān)于Sonar的介紹在Linux平臺下較多,所以我下面的介紹主要是基于Win平臺的,其他平臺大同小異。
安裝Sonar主要有以下幾步:
安裝JAVA SDK
Sonar是一款基于JAVA開發(fā)的工具,安裝JAVA SDK的過程在此不再敘述,建議安裝好之后配置好JAVA_HOME的環(huán)境變量,以下是下載地址。
http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
?
安裝SonarQube
首先到官網(wǎng)下載安裝包,值得注意的是,該安裝包是不分平臺的,下載下來之后,選擇Windows的文件夾中StartSonar.bat文件運(yùn)行即可。
https://www.sonarqube.org/#downloads
如果java環(huán)境安裝正常,Sonar應(yīng)該是能正常啟動的,啟動后瀏覽。啟動效果如下:
剛剛裝好是英文的,我是安裝了中文包,如何安裝中文包,后面會敘述。
配置Sonar
我們需要對Sonar進(jìn)行簡單配置,使其能連接上MySQL數(shù)據(jù)庫。
打開MySQL數(shù)據(jù)庫,執(zhí)行以下指令。
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創(chuàng)建數(shù)據(jù)庫并添加該數(shù)據(jù)庫的用戶,數(shù)據(jù)庫名稱是sonar ,用戶名是sonar,密碼是sonar。
打開sonar.properties將內(nèi)容替換成如下:
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數(shù)據(jù)庫的連接字符串。
重新啟動Sonar(關(guān)閉運(yùn)行startsonar.bat控制臺,并在任務(wù)管理器中關(guān)閉所有和java有關(guān)的進(jìn)程,重新運(yùn)行startsonor.bat),使用管理員賬戶登錄(admin/admin)。
登錄之后,安裝中文包,如下,安裝之后需要點(diǎn)擊重新啟動,啟動之后,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
解壓之后,設(shè)置SonarQube Scanner for MSBuild的環(huán)境變量,如我的解壓路徑是:C:\MyWorkSpace\Tools\sonar-scanner-msbuild-4.3.1.1372-net46,則把該路徑添加到path下:
?
修改SonarQube.Analysis.xml文件
要修改的地方只是關(guān)于sonarQube服務(wù)器的一些配置,關(guān)于服務(wù)器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環(huán)境變量,便于后面在命令行中調(diào)用MSBuild,我的是在vs 2017的安裝目錄下
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\amd64
?
C# 項(xiàng)目分析
CMD進(jìn)入C#項(xiàng)目所在的根目錄,依此執(zhí)行以下三條命令。
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?
參數(shù)說明:
/key(簡寫k):對應(yīng)projectKey即項(xiàng)目的唯一代碼,如兩套源代碼使用同一個projectKey那掃描的結(jié)果將混在一起,所以一個項(xiàng)目需要有一個單獨(dú)的projectKey
/name(簡寫n):對應(yīng)projectName即項(xiàng)目的名稱,為項(xiàng)目的一個顯示的名稱,建立使用完整的項(xiàng)目名稱
/version(簡寫v):對應(yīng)projectVersion即項(xiàng)目的版本,項(xiàng)目在不同的時期版本也是不一樣的,如果方便,可以在sonarQube的服務(wù)器中查看到不同的版本代碼其中問題的變化
三條命令分別是分析的前期準(zhǔn)備,MSBuild編譯,將報(bào)告上傳給SonarQube。
?
查看分析結(jié)果
最后,進(jìn)入http://localhost:9000/projects? 查看分析結(jié)果吧,驚喜不驚喜?
界面中功能強(qiáng)大,很多認(rèn)為絕對發(fā)現(xiàn)不了的Bug都展現(xiàn)出來了,還可以查看單元測試的覆蓋率,相信如果堅(jiān)持使用該工具,一定會對編碼習(xí)慣有很大幫助。
快快搭建一個SonarQube看看自己的代碼有沒有BUG!!
?
我的博客即將入駐“云棲社區(qū)”,誠邀技術(shù)同仁一同入駐。
?
參考文獻(xiàn):https://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner+for+MSBuild
轉(zhuǎn)載于:https://www.cnblogs.com/CoderAyu/p/9416376.html
總結(jié)
以上是生活随笔為你收集整理的基于Win10极简SonarQube C#代码质量分析的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ConfigParser 模块
- 下一篇: C#6.0语言规范(八) 语句