简体   繁体   English

如何在C#.NET项目上运行SonarQube?

[英]How do I run SonarQube on a C#.NET project?

I have a C#/.NET project that I want to scan using SonarQube (C# language). 我有一个C#/ .NET项目,我想用SonarQube(C#语言)扫描。

I am getting the scan to pass but with many errors (Over 200 repeated errors) 我正在通过扫描,但有很多错误(超过200个重复错误)

Provide an 'AssemblyVersion' attribute for this assembly.

This above message is repeated many times and accounts for a majority of SonarQube bugs I see in my report. 上面的消息重复多次,占我在报告中看到的大部分SonarQube错误。

SonarQube properties file SonarQube属性文件

# Comma-separated paths to directories with sources (required)
#sonar.sources=controllers,services
sonar.sources=.
# Language
sonar.language=cs

sonar.visualstudio.solution=ProjectName.sln

sonar.sourceEncoding=UTF-8
sonar.cobol.copy.diretories=/copy
sonar.visualstudio.enable=true

I even ran this msbuild (using MSBuild.exe Version 14 which I downloaded from https://www.microsoft.com/en-us/download/details.aspx?id=48159 ) batch file in my project 我甚至在我的项目中运行了这个msbuild(使用我从https://www.microsoft.com/en-us/download/details.aspx?id=48159下载的MSBuild.exe版本14)批处理文件

C:\Users\pwrdbyadmin\Desktop\sonar-scanner-msbuild-3.0.2.656\SonarQube.Scanner.MSBuild.exe begin /k:"org.sonarqube:sonarqube-scanner-msbuild" /n:"ProjectName" /v:"1.0"
"C:\Program Files (x86)\MSBuild\14.0\Bin\amd64\MSBuild.exe"  /t:Rebuild
C:\Users\pwrdbyadmin\Desktop\sonar-scanner-msbuild-3.0.2.656\SonarQube.Scanner.MSBuild.exe end

Downloaded SonarQube 6.5 and ran following command to start server 下载SonarQube 6.5并运行以下命令启动服务器

C:\Users\pwrdbyadmin\Desktop\sonarqube-6.5\bin\windows-x86-32\StartSonar.bat

Command I run from within project 命令我在项目中运行

C:\Users\username\Desktop\sonar-scanner-3.0.3.778-windows\bin\sonar-scanner.bat

I still cannot resolve many of the .NET errors in my project. 我仍然无法解决项目中的许多.NET错误。

How can I resolve these obvious C# errors that come about after I run SonarQube on my project, do I need Visual Studio installed, do I need to build my project in Visual Studio? 如何在我的项目上运行SonarQube后解决这些明显的C#错误,我是否需要安装Visual Studio,是否需要在Visual Studio中构建我的项目? What are the proper steps I need to follow to scan my project? 扫描我的项目需要遵循哪些正确的步骤?

EDIT 编辑

The new three liner allowed analysis to work without any Insufficient Privilege error showing up at the end. 新的三个班轮允许分析工作,最后没有出现任何Insufficient Privilege错误。 I used VS 2017's latest MSBuild.exe in my 3 line command 我在我的3行命令中使用了VS 2017的最新MSBuild.exe

C:\Users\<UserName>\Desktop\sonar-scanner-msbuild-3.0.2.656\SonarQube.Scanner.MSBuild.exe begin /d:"sonar.host.url=http://localhost:9000" /d:"sonar.login=<login>" /d:"sonar.password=<password>" /k:"org.sonarqube:sonarqube-scanner-msbuild" /n:"<ProjectName>" /v:"1.0"
"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\MSBuild.exe"  /t:Rebuild
C:\Users\<UserName>\Desktop\sonar-scanner-msbuild-3.0.2.656\SonarQube.Scanner.MSBuild.exe end /d:"sonar.login=<login>" /d:"sonar.password=<password>"

In spite of using the updated command sequence, I still get the Assembly Version errors in my report. 尽管使用了更新的命令序列,我仍然在报告中收到程序集版本错误。

Do I need to delete the project and re-analyze. 我是否需要删除项目并重新分析。 Also, even thought my 3 liner shows MSBuild 15 used the yellow notice comes up saying I am using MSBuild 12. I am not sure why. 此外,甚至认为我的3班轮显示MSBuild 15使用黄色通知出现说我正在使用MSBuild 12.我不知道为什么。

UPDATE: Screenshot of duplicate files/folders in Code Smell Analysis. 更新:Code Smell Analysis中重复文件/文件夹的屏幕截图。

在此输入图像描述

I am not sure where is the SonarQube.Properties file is used. 我不确定使用SonarQube.Properties文件的位置。 I couldn't find that in my setup. 我在我的设置中找不到。

Make sure all the projects you are scanning has "Properties" folder and under that AssemblyInfo.cs which contains assembly version. 确保您扫描的所有项目都有“属性”文件夹,并且在包含程序集版本的AssemblyInfo.cs下。 The file must be included in the project. 该文件必须包含在项目中。 You can see that from Visual Studio. 您可以从Visual Studio中看到它。

Make sure you have provided the SonarQube address and authentication details in the SonarQube.Analysis.xml 确保您已在SonarQube.Analysis.xml中提供了SonarQube地址和身份验证详细信息

sonar.host.url - URL to your SonarQube server sonar.login - Analysis token of a user with Execute Analysis permissions. sonar.host.url - SonarQube服务器的URL sonar.login - 具有执行分析权限的用户的分析令牌。 Required only if Anonymous does not have them 仅在Anonymous没有它们时才需要

Create a new bat file only with these lines. 仅使用这些行创建新的bat文件。

<local path>/SonarQube.Scanner.MSBuild.exe begin /k:"<SomeProjectNameKey>" /n:"<YourProjectName>" /v:"1.0"
<local path>/MSBuild.exe /t:Rebuild /tv:15.0 /p:VisualStudioVersion=15.0
<local path>/SonarQube.Scanner.MSBuild.exe end

The bat file should be in the same folder where the projects are located bat文件应位于项目所在的同一文件夹中

Documentation reference https://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner+for+MSBuild 文档参考https://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner+for+MSBuild

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM