【问题标题】:How do I run SonarQube on a C#.NET project?如何在 C#.NET 项目上运行 SonarQube?
【发布时间】:2018-02-10 19:21:09
【问题描述】:

我有一个 C#/.NET 项目,我想使用 SonarQube(C# 语言)进行扫描。

我正在让扫描通过,但有很多错误(超过 200 个重复错误)

Provide an 'AssemblyVersion' attribute for this assembly.

上述消息重复了很多次,是我在报告中看到的大多数 SonarQube 错误的原因。

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

我什至在我的项目中运行了这个 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

下载 SonarQube 6.5 并运行以下命令启动服务器

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

我在项目中运行的命令

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

我仍然无法解决项目中的许多 .NET 错误。

如何解决在我的项目上运行 SonarQube 后出现的这些明显的 C# 错误,我是否需要安装 Visual Studio,我是否需要在 Visual Studio 中构建我的项目?我需要遵循哪些正确步骤来扫描我的项目?

编辑

新的三班轮允许分析工作,最后不会出现任何权限不足错误。我在我的 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>"

尽管使用了更新的命令序列,但我的报告中仍然出现程序集版本错误。

我是否需要删除项目并重新分析。此外,即使我的 3 班轮显示使用 MSBuild 15,也会出现黄色通知,说我正在使用 MSBuild 12。我不知道为什么。

更新:代码异味分析中重复文件/文件夹的屏幕截图。

【问题讨论】:

  • this 有帮助吗? Source docs
  • 我之前发现了这个,并在上面的代码中使用了三个 liner 来执行 msbuild 的 sonarscanner。运行 3 班轮后,我是否只是在我的项目上重新运行 sonar-scanner.bat?
  • 我希望在您运行 3 行之后,您的分析就完成了。您不需要运行 .bat 文件。
  • 您的 SonarC# 插件是什么版本?您是否在解决方案中使用共享项目?您能否上传构建中的完整日志并在此处发布链接,以便我们更好地诊断?
  • 感谢您的回复。 SonarQube 是 6.5 版,Sonar Scanner 是 3.0.3.778 版,sonar-csharp-plugin-5.10.1.1411。我收到一条警告说使用 MSBuild 14 以获得最佳分析结果。我在我的项目目录中运行 sonar-scanner.bat。我在帖子中将 MSBuild 更新为 14。如何在 sonar-scanner.bat 中指向它?

标签: c# asp.net msbuild sonarqube


【解决方案1】:

我不确定 SonarQube.Properties 文件在哪里使用。我在我的设置中找不到它。

确保您正在扫描的所有项目都有“Properties”文件夹,并且在包含程序集版本的 AssemblyInfo.cs 下。该文件必须包含在项目中。您可以从 Visual Studio 中看到。

确保您在 SonarQube.Analysis.xml 中提供了 SonarQube 地址和身份验证详细信息

sonar.host.url - 您的 SonarQube 服务器的 URL sonar.login - 具有执行分析权限的用户的分析令牌。仅当匿名者没有它们时才需要

仅使用这些行创建一个新的 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

bat文件应该在项目所在的同一个文件夹中

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

【讨论】:

  • 我知道这是另一个问题,但我正在努力解决这个问题。如何删除重复的代码异味报告行项目(我已编辑我的帖子)?
  • 您可以防止某些文件被检查重复。为此,请转至管理 > 常规设置 > 分析范围 > 重复并设置重复排除属性。有关语法的更多详细信息,请参阅Patterns 部分。
  • 更改后,重新启动Sonar服务并再次运行扫描。
  • 使用您提供的答案(Visual Studio 15 版本)没有重复的代码异味问题。谢谢。
  • 如果有人想知道如何使用 SonarQube 为 ASp.NET 项目运行代码分析,请参阅此详细视频。 youtu.be/2x0fyBOuMB8。在此之前,您必须在本地计算机上安装 SonarQube。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-04-10
  • 2013-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-04
  • 1970-01-01
相关资源
最近更新 更多