.NET Core 使用 Sonarqube 问题

2019年12月2日 5566点热度 3人点赞 2条评论
内容目录

.NET Core 使用 Sonarqube 问题

运行 Sonarqube 的简单方法

docker run -d --name sonarqube -p 9000:9000 sonarqube

默认账号密码是 admim

登陆后创建项目和 token,直接按照提示选择 C#.NET。

.NET Core 项目

先运行一下命令

dotnet tool install --global dotnet-sonarscanner

.NET FX 项目

到这里下载最新版本的 工具

https://github.com/SonarSource/sonar-scanner-msbuild/releases

解压到英文目录,将目录路径复制,打开环境变量,点击 Path,添加目录。

进入 sonar 文件目录,文件如下

.
├── Newtonsoft.Json.dll
├── SonarQube.Analysis.xml
├── sonar-scanner-4.2.0.1873
│   ├── bin
│   ├── conf
│   └── lib
├── SonarScanner.MSBuild.Common.dll
├── SonarScanner.MSBuild.dll
├── SonarScanner.MSBuild.PostProcessor.dll
├── SonarScanner.MSBuild.PreProcessor.dll
├── SonarScanner.MSBuild.runtimeconfig.json
├── SonarScanner.MSBuild.Shim.dll
├── SonarScanner.MSBuild.Tasks.dll
└── SonarScanner.MSBuild.TFS.dll

如果是 .NET Core ,就使用 dotnet sonarscanner,如果 .NET Fx,就用
dotnet SonarScanner.MSBuild.dll后面带的参数一致。

执行一下命令登陆 Sonarqube

dotnet sonarscanner begin /k:"{项目名称}" /d:sonar.host.url="http://{ip地址}:9000" /d:sonar.login="{Token密钥}"
Using the .NET Core version of the Scanner for MSBuild
Pre-processing started.
Preparing working directories...
09:39:30.41  Updating build integration targets...
09:39:30.444  Fetching analysis configuration settings...
09:39:32.229  Provisioning analyzer assemblies for cs...
09:39:32.232  Installing required Roslyn analyzers...
09:39:33.903  Provisioning analyzer assemblies for vbnet...
09:39:33.904  Installing required Roslyn analyzers...
09:39:34.034  Pre-processing succeeded.

分析、编译项目

打开项目目录,执行

 dotnet build {解决方案.sln}
 或者
 dotnet build {项目.csproj}

如果后面的代码报错

No analysable projects were found. SonarQube analysis will not be performed. Check the build summary report for details.
Generation of the sonar-properties file failed. Unable to complete SonarQube analysis.
13:04:37.856  Post-processing failed. Exit code: 1

就使用

 dotnet build 

将分析结果推送到 Sonarqube

在当前项目目录下执行

dotnet sonarscanner end /d:sonar.login="{Token}"

或者

dotnet {安装目录\SonarScanner.MSBuild.dll} end /d:sonar.login="{Token}"

如果出现

SonarScanner for MSBuild 4.3.1
Using the .NET Core version of the Scanner for MSBuild
Default properties file was found at C:\Users\GW\.dotnet\tools\.store\dotnet-sonarscanner\4.3.1\dotnet-sonarscanner\4.3.1\tools\netcoreapp2.1\any\SonarQube.Analysis.xml
Loading analysis properties from C:\Users\GW\.dotnet\tools\.store\dotnet-sonarscanner\4.3.1\dotnet-sonarscanner\4.3.1\tools\netcoreapp2.1\any\SonarQube.Analysis.xml
Post-processing started.
10:01:01.135  SonarQube analysis could not be completed because the analysis configuration file could not be found: H:\MQTT\Gw.MqttService\GwMqttClient.STD\.sonarqube\conf\SonarQubeAnalysisConfig.xml.
10:01:01.138  Post-processing failed. Exit code: 1

以上三个步骤的命令都需要在下面执行。

如果根据原文档和 Sonarqube 的提示去做,可能会出现

Pre-processing started.
Preparing working directories...
09:36:45.742  Updating build integration targets...
09:36:45.769  Fetching analysis configuration settings...
09:36:47.863  Failed to request and parse 'http://localhost:9000/api/server/version': 由于目标计算机积极拒绝,无法连接。 由于目标计算机积极拒绝,无法连接。

Unhandled Exception: System.Net.WebException: 由于目标计算机积极拒绝,无法连接。 由于目标计算机积极拒绝,无法连接。 ---> System.Net.Http.HttpRequestException: 由于目标计算机积极拒绝,无法连接。 ---> System.Net.Sockets.SocketException: 由于 目标计算机积极拒绝,无法连接。
   at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)
   --- End of inner exception stack trace ---
   at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)
   at System.Threading.Tasks.ValueTask`1.get_Result()
   at System.Net.Http.HttpConnectionPool.CreateConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   at System.Threading.Tasks.ValueTask`1.get_Result()
   at System.Net.Http.HttpConnectionPool.WaitForCreatedConnectionAsync(ValueTask`1 creationTask)
   at System.Threading.Tasks.ValueTask`1.get_Result()
   at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
   at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   at System.Net.Http.HttpClient.FinishSendAsyncUnbuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
   at System.Net.HttpWebRequest.SendRequest()
   at System.Net.HttpWebRequest.GetResponse()
   --- End of inner exception stack trace ---
   at System.Net.HttpWebRequest.GetResponse()
   at System.Net.WebClient.GetWebResponse(WebRequest request)
   at System.Net.WebClient.DownloadBits(WebRequest request, Stream writeStream)
   at System.Net.WebClient.DownloadDataInternal(Uri address, WebRequest& request)
   at System.Net.WebClient.DownloadString(Uri address)
   at SonarScanner.MSBuild.PreProcessor.SonarWebService.<>c__DisplayClass15_0.<DownloadServerVersion>b__0()
   at SonarScanner.MSBuild.PreProcessor.SonarWebService.DoLogExceptions[T](Func1 op, String url, Action1 onError)
   at SonarScanner.MSBuild.PreProcessor.SonarWebService.DownloadServerVersion()
   at SonarScanner.MSBuild.PreProcessor.SonarWebService.GetServerVersion()
   at SonarScanner.MSBuild.PreProcessor.SonarWebService.GetProperties(String projectKey, String projectBranch)
   at SonarScanner.MSBuild.PreProcessor.TeamBuildPreProcessor.FetchArgumentsAndRulesets(ISonarQubeServer server, ProcessedArgs args, TeamBuildSettings settings, IDictionary2& serverSettings, List1& analyzersSettings)
   at SonarScanner.MSBuild.PreProcessor.TeamBuildPreProcessor.DoExecute(ProcessedArgs localSettings)
   at SonarScanner.MSBuild.BootstrapperClass.PreProcess()
   at SonarScanner.MSBuild.BootstrapperClass.Execute()
   at SonarScanner.MSBuild.Program.Execute(String[] args, ILogger logger)
   at SonarScanner.MSBuild.Program.Main(String[] args)

痴者工良

高级程序员劝退师

文章评论

  • 痴者工良

    首先要在 项目下执行 `dotnet sonarscanner begin /k:"{项目名称}" /d:sonar.host.url="http://{ip地址}:9000" /d:sonar.login="{Token密钥}"`,才能创建 .sonarqube目录

    2020年2月11日