日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

一文搞定SonarQube接入C#(.NET)代碼質(zhì)量分析

 測(cè)試開(kāi)發(fā)技術(shù) 2022-06-18 發(fā)布于廣東


1. 前言

      C#語(yǔ)言接入Sonar代碼靜態(tài)掃描相較于Java、Python來(lái)說(shuō),相對(duì)麻煩一些。Sonar檢測(cè)C#代碼時(shí)需要預(yù)先編譯,而且C#代碼必須用MSbuid進(jìn)行編譯,如果需要使用SonarQube對(duì)C#進(jìn)行代碼質(zhì)量分析,則需要下載Sonar-Scanner-MSBuild和MSBuild,其中要求MSBuild在V14.0以上。

2. Sonar-Scanner for MSBuild安裝與配置

1、下載SonarQube Scanner for MSBuild,它是C# Framework的Sonar分析插件。

下載地址:

https://github.com/SonarSource/sonar-scanner-msbuild/releases/download/4.3.1.1372/sonar-scanner-msbuild-4.3.1.1372-net46.zip

2、下載并解壓之后,設(shè)置SonarQube Scanner for MSBuild的環(huán)境變量。

例如我的解壓路徑是:C:\Users\Administrator\Downloads\sonar-scanner-msbuild-4.3.1.1372-net466,則把該路徑添加到Path下。

SonarQube Scanner for MSBuild解壓目錄如下圖所示:

3、修改SonarQube.Analysis.xml文件,要修改的地方只是關(guān)于SonarQube服務(wù)器的一些配置,如服務(wù)器URL、USER、PASSWORD等,詳細(xì)配置修改如下:

<?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./2001/XMLSchema-instance" xmlns:xsd="http://www./2001/XMLSchema" xmlns="http://www./msbuild/integration/2015/1">     <Property Name="sonar.host.url">http://sonar_ip:sonar_port</Property>  <Property Name="sonar.login">login_username</Property>  <Property Name="sonar.password">login_password</Property>    <!-- Required only for versions of SonarQube prior to 5.2 -->     <Property Name="sonar.jdbc.url">jdbc:mysql://db_ip:db_port/sonar?useUnicode=true;characterEncoding=utf8;rewriteBatchedStatements=true;useConfigs=maxPerformance;useSSL=false</Property>  <Property Name="sonar.jdbc.username">jdbc.username</Property>  <Property Name="sonar.jdbc.password">jdbc.password</Property>  </SonarQubeAnalysisProperties>

3. MSBuild安裝與配置

     Visual Studio IDE在編譯*.sln解決方案時(shí)默認(rèn)是調(diào)用msbuild.exe來(lái)實(shí)現(xiàn)的。如果你的機(jī)器上沒(méi)有裝有Visual Studio,那么也可以單獨(dú)使用MSBuild來(lái)編譯.sln(工程解決方案)或.csproj(項(xiàng)目)。MSBuild可以直接通過(guò).NETFramework來(lái)安裝獲得。

msbuild.exe的路徑一般如下:

X86: C:\Program Files (x86)\MSBuild\14.0\Bin\MSBuild.exeX64: C:\Program Files (x86)\MSBuild\14.0\Bin\amd64\MSBuild.exe

msbuild.exe 目錄如下所示:

將MSBuild.exe添加到Path環(huán)境變量,便于后面在命令行中調(diào)用MSBuild。

msbuild常用編譯命令:

MSBuild MyApp.sln /t:Rebuild /p:Configuration=ReleaseMSBuild MyApp.csproj /t:Clean /p:Configuration=Debug;/p:Platform=x86;TargetFrameworkVersion=v3.5 編譯為 Release 代碼 -p:configuration="release"清理項(xiàng)目 -t:clean重新編譯 -t:rebuild編譯項(xiàng)目 -t:build 默認(rèn)可以忽略這個(gè)參數(shù)發(fā)布 -t:Publish 注意:這里的 -t 和 /t 作用是相同的。

4. Sonar+命令行分析C#代碼

1、打開(kāi)CMD,切換到指定的項(xiàng)目根目錄,必須和.sln或者.csproj同級(jí)目錄。例如以\hcloud\Common\KDY.WebApi.Core項(xiàng)目為例,如下圖所示。

2、使用MSBuild方式進(jìn)行Sonar Scanner掃描代碼前期準(zhǔn)備文件生成,CMD命令下運(yùn)行:

SonarScanner.MSBuild.exe begin /k:"hcloud.Common.KDY.WebApi.Core" /n:"hcloud.Common.KDY.WebApi.Core" /v:"1.0"

命令執(zhí)行結(jié)果如下:

參數(shù)說(shuō)明:

  • /key(簡(jiǎn)寫(xiě)k):對(duì)應(yīng)projectKey即項(xiàng)目的唯一代碼,如兩套源代碼使用同一個(gè)projectKey那掃描的結(jié)果將混在一起,所以一個(gè)項(xiàng)目需要有一個(gè)單獨(dú)的projectKey

  • /name(簡(jiǎn)寫(xiě)n):對(duì)應(yīng)projectName即項(xiàng)目的名稱,為項(xiàng)目的一個(gè)顯示的名稱,建立使用完整的項(xiàng)目名稱

  • /version(簡(jiǎn)寫(xiě)v):對(duì)應(yīng)projectVersion即項(xiàng)目的版本,項(xiàng)目在不同的時(shí)期版本也是不一樣的,如果方便,可以在sonarQube的服務(wù)器中查看到不同的版本代碼其中問(wèn)題的變化

執(zhí)行上述命令后,在項(xiàng)目目錄下,生成.sonarqube目錄。

3、通過(guò)MSBuild命令編譯項(xiàng)目,在CMD命令行下執(zhí)行:

MSBuild.exe /t:Rebuild   (默認(rèn)為Debug模式)
或者MSBuild.exe /t:Rebuild /p:Configuration=Release  (指定編譯模式) 或者MSBuild.exe D:\hcloud\Common\Common.sln /t:Rebuild  (指定具體的.sln解決方案)

編譯項(xiàng)目運(yùn)行結(jié)果如下所示:

0個(gè)錯(cuò)誤,則代表MSBuild編譯成功,編譯成功后,在當(dāng)前目錄下會(huì)生成一個(gè)obj目錄。(編譯成功后默認(rèn)生成Debug產(chǎn)物),SonarQube分析C#項(xiàng)目工程時(shí),前提需要MSBuild能預(yù)編譯成功,如果存在錯(cuò)誤,則無(wú)法成功完成后續(xù)Sonar分析動(dòng)作。

4、分析C#掃描結(jié)果,將分析報(bào)告上傳給SonarQube,CMD命令下運(yùn)行:

SonarScanner.MSBuild.exe end

執(zhí)行結(jié)果如下圖所示:

溫馨提示:

1、如果運(yùn)行出現(xiàn)錯(cuò)誤請(qǐng)檢查sonar server的log,路徑為Snoar\sonarqube-6.7\logs下的sonar.log,web.log和access.log。

 2、如果遇到需要檢測(cè)比較大的項(xiàng)目,可能上傳的mysql數(shù)據(jù)量會(huì)很大,會(huì)超出默認(rèn)的mysql上傳的最大值,此時(shí)需要設(shè)置mysql的max_allowed_packet。

5、查看Sonar分析掃描后的結(jié)果,訪問(wèn)http://10.0.0.147:9000/dashboard?id=hcloud.Common.KDY.WebApi.Core,分析結(jié)果如下圖所示:

5. Jenkins+Sonar+MSBuild分析C#代碼

1、編譯.NET(C#)應(yīng)用程序可通過(guò)微軟提供的MSBuild工具,先安裝插件MSBuild,在Jenkins中搜索并安裝MSBuild插件,如下圖所示。

2、插件安裝完畢后,進(jìn)入系統(tǒng)管理->全局工具配置(Global Tool Configuration)找到MSBuild配置選項(xiàng),如下圖所示。

3、配置SonarScanner for MSBuild,如下圖所示。

4、由于示例中的Jenkins服務(wù)是部署在Linux系統(tǒng)中,故此處可添加一臺(tái)Windows主機(jī)(10.0.0.148)作為C#項(xiàng)目編譯運(yùn)行環(huán)境,在Windows從節(jié)點(diǎn)配置中,添加并配置相應(yīng)工具,如下圖所示。

5、新建并配置JOB,添加JOB運(yùn)行節(jié)點(diǎn)(編譯C#工程項(xiàng)目的運(yùn)行機(jī)),如下圖所示。

6、配置源碼管理及其它所需配置(較為簡(jiǎn)單,此處省略)后,添加并配置構(gòu)建選項(xiàng),如下圖所示。

7、JOB構(gòu)建運(yùn)行結(jié)果如下圖所示。

8、JOB構(gòu)建成功后,Sonar代碼分析報(bào)告如下圖所示。

6. 常見(jiàn)問(wèn)題

1、解決SonarQube檢測(cè)C#執(zhí)行成功,但不能獲取檢測(cè)結(jié)果的問(wèn)題,現(xiàn)象如下圖所示。

由圖中可以看到文件掃描成功了,但是卻沒(méi)有任何文件被發(fā)現(xiàn),所有的指標(biāo)數(shù)據(jù)皆為0。

解決方案

將Sonar插件中的C#插件改為5.9的版本即可。修改方式將plugin目錄下原本的C#插件刪除掉,將5.9版本的插件放入進(jìn)來(lái)。重啟SonarQube后問(wèn)題即可解決。(備注示例中的SonarQube版本為6.7.5)

plugin目錄替換后如下圖所示:

2. Jenkins +MSBuild+Sonar構(gòu)建編譯Job時(shí)提示Running the Scanner for MSBuild under Local System or Network Service account is not supported. Please, use a local or domain user account instead.

現(xiàn)象如下圖所示:

解決方法:

登錄從節(jié)點(diǎn)10.0.0.148(windows主機(jī)),右擊我的電腦選擇管理然后從管理界面里面找到服務(wù)或者在cmd界面輸入services.msc打開(kāi)服務(wù)管理界面,從服務(wù)管理界面找到j(luò)enkins slave服務(wù),右鍵點(diǎn)擊屬性,在彈出的對(duì)話框中切換到登陸標(biāo)簽,默認(rèn)登錄方式為本地系統(tǒng)賬號(hào),此處我們選擇此賬戶。然后輸入賬戶和密碼點(diǎn)擊確定,完成以上操作以后重新啟動(dòng)jenkins slave服務(wù)然后再重新執(zhí)行即可。

修改方式如下圖所示:

3、Jenkins單獨(dú)構(gòu)建沒(méi)問(wèn)題,Sonar靜態(tài)檢查代碼單獨(dú)執(zhí)行也沒(méi)問(wèn)題,但是Jenkins+Sonar集成時(shí)出現(xiàn)未經(jīng)授權(quán)問(wèn)題,現(xiàn)象如下圖所示。

解決方案:

原因是由于Jenkins上已經(jīng)通過(guò)admin生成了Token來(lái)進(jìn)行連接認(rèn)證,需要注釋掉SonarQube.Analysis.xml里面的sonar.login和sonar.password,刪除或者注釋后,再重新執(zhí)行即可。

修改如下圖所示(下圖采用注釋來(lái)解決該問(wèn)題的)。

    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多