.NET Core Using SonarQube Issues

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

.NET Core Using Sonarqube Issues

Simple Way to Run Sonarqube

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

The default username and password are admim

After logging in, create a project and a token, and directly follow the prompt to select C#.NET.

.NET Core Project

First, run the following command

dotnet tool install --global dotnet-sonarscanner

.NET FX Project

Download the latest version of the tool from here

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

Unzip to an English directory, copy the directory path, open the environment variables, click on Path, and add the directory.

Enter the sonar file directory, as shown below

.
├── 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

If it is .NET Core, use dotnet sonarscanner; if it is .NET Fx, use dotnet SonarScanner.MSBuild.dll with the same parameters.

Execute the following command to log in to Sonarqube

dotnet sonarscanner begin /k:"{Project Name}" /d:sonar.host.url="http://{IP Address}:9000" /d:sonar.login="{Token Key}"
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.

Analyze and Compile Project

Open the project directory and execute

 dotnet build {solution.sln}
 or
 dotnet build {project.csproj}

If the following code reports an error

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

Use

 dotnet build 

Pushing Analysis Results to Sonarqube

Execute the following in the current project directory

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

Or

dotnet {Install Directory\SonarScanner.MSBuild.dll} end /d:sonar.login="{Token}"

If you see

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

All three command steps above need to be executed below.

If you follow the original documentation and Sonarqube's prompts, you might encounter

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': The target machine actively refused it. Unable to connect. The target machine actively refused it. Unable to connect.

Unhandled Exception: System.Net.WebException: The target machine actively refused it. Unable to connect. The target machine actively refused it. Unable to connect. ---> System.Net.Http.HttpRequestException: The target machine actively refused it. ---> System.Net.Sockets.SocketException: The target machine actively refused it. 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.ValueTask1.get_Result() at System.Net.Http.HttpConnectionPool.CreateConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken) at System.Threading.Tasks.ValueTask1.get_Result() at System.Net.Http.HttpConnectionPool.WaitForCreatedConnectionAsync(ValueTask1 creationTask) at System.Threading.Tasks.ValueTask1.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(Task1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts) at System.Net.HttpWebRequest.SendRequest() 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, List`1& 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)

痴者工良

高级程序员劝退师

文章评论