到現在為止,已經將sonarQube服務器搭建好,服務器是Linux服務器,基於sonarQube6.1配置的
原理關於sonarQube的工作原理,暫時還不是太清楚,據同事的分析,是在本機調用服務器對本地的原代碼進行掃描,掃描完成之后,將結果保存至服務器,開發同事再登陸至服務器,查看報告並處理問題等
使用MSBuild掃描
針對是源碼已經存在本地的同事
參考官方文檔:http://docs.sonarqube.org/display/SCAN/From+the+Command+Line
下載:http://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner+for+MSBuild
(1)下載並解壓SonarQube Scanner for MSBuild
將https://github.com/SonarSource-VisualStudio/sonar-scanner-msbuild/releases/download/2.2/sonar-scanner-msbuild-2.2.0.24.zip 下載至本地,並解壓至一個路徑,例如"D:\ms\"中
(2)修改SonarQube.Analysis.xml文件
要修改的地方只是關於sonarQube服務器的一些配置,關於服務器URL、USER、PASSWORD等
注意,這里的參數User/PassWord是登陸sonarQube平台的用戶名和密碼,請不要與sonarQube的DB登陸用戶名和密碼搞混淆。
<?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">sonarQubeServerURL</Property> <Property Name="sonar.login">admin</Property> <Property Name="sonar.password">password</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> <Property Name="sonar.jdbc.username">sonar</Property> <Property Name="sonar.jdbc.password">sonar</Property> --> </SonarQubeAnalysisProperties>
(3)打開源代碼文件夾,例如”F:\test\ArasCheckItem\ArasCheckItem“,在當前的路徑打開cmd(按住Shift鍵並鼠標右鍵),或者打開cmd窗口,並通過命令進入至該源碼目錄
(3.1)運行命令MSBuild.SonarQube.Runner.exe,本文示例運行命令時加入路徑是因為我沒有將MSBuild的路徑加入至環境變量,如加入了環境變量,可以直接運行MSBuild.SonarQube.Runner.exe,請注意,參數請使用雙引號
參數說明:
/key(簡寫k):對應projectKey即項目的唯一代碼,如兩套源代碼使用同一個projectKey那掃描的結果將混在一起,所以一個項目需要有一個單獨的projectKey
/name(簡寫n):對應projectName即項目的名稱,為項目的一個顯示的名稱,建立使用完整的項目名稱
/version(簡寫v):對應projectVersion即項目的版本,項目在不同的時期版本也是不一樣的,如果方便,可以在sonarQube的服務器中查看到不同的版本代碼其中問題的變化
以下為完整參數與簡寫參數命令
"d:\ms\MSBuild.SonarQube.Runner.exe" begin /k:"testproject" /n:"mytestproject" /v:"1.9"
"d:\ms\MSBuild.SonarQube.Runner.exe" begin /key:"testproject" /name:"mytestproject" /version:"1.9"
(3.2)運行命令MSBuild.exe,這個MSBuild是系統安裝過VS后會存在的一個編譯器,以Visual Studio 2013為例,地址是:C:\Program Files (x86)\MSBuild\12.0\Bin
"C:\Program Files (x86)\MSBuild\12.0\Bin\MSBuild.exe" /t:Rebuild (這里的參數/t如果不指定也是可以掃描出來的,看下圖)
(3.3)運行命令MSBuild.SonarQube.Runner.exe,告訴sonarQube運行完成了,將報告上傳
"d:\ms\MSBuild.SonarQube.Runner.exe" end