sonar 配置sonarqube分析unity中的C# 代码

参考文档:使用SonarQube对Unity项目进行代码分析的问题记录 - zerozabuu - 博客园

一文搞定 SonarQube 接入 C#(.NET) 代码质量分析 - 狂师 - 博客园

需要分析的代码是unity下的C# 代码,配置流程主要参考上面两个文档,但还是遇到不少坑。

1、官网下载解压最新版的SonarQube(我用的是8.9.1)。

2、下载安装jdk并配置环境变量,这里我原来用的是jdk16,但是报错,换成了jdk11。

3、数据库配置,我也用的postgresql。

4、启动sonar服务。

双击startsonar

运行成功如图

此时打开 http://localhost:9000/ ,可以进入sonarqube网站,默认账号密码都是admin,

登录后会提示修改一下密码,这个密码要记住,后面会用到。

 

 

5、下载 sonar-scanner-msbuild

要分析C# 代码,需要下载 sonar-scanner-msbuild并配置环境变量。

我本来也下载的sonar-scanner-msbuild-4.3.1.1372-net466这个版本,但是后续出错了,提交报告的时候,提示只支持Msbuild14和15,又重新下载了最新的版本sonar-scanner-msbuild,需要配置到path路径。

 

6、MSBuild安装和配置。

需要用到Msbuild,这个是Visual studio带的工具,有下载了下VS2019,找到Msbuild的路径

C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\bin

,添加到环境变量path中。

7、运行预处理编译和提交报告命令。

切换到项目根目录,运行cmd。

1)用命令SonarScanner.MSBuild.exe begin /k:"xxxxxx项目名" /n:"项目名" /v:"1.0"    进行预处理。

执行后会生成一个.sonarqube文件

 

 

2)用命令MSBuild.exe xxxx.sln /t:Rebuild 对解决方案进行编译。

 

 提示0个错误,这样是编译通过。

执行后会生成一个obj文件

 

3)SonarScanner.MSBuild.exe end 命令,将分析结果上传到sonarqube。

成功结果如图。

在sonarqube可以看到上传的项目报告。 

 遇到的坑:

1)执行 SonarScanner.MSBuild.exe提示远程服务器返回错误:(401)未经授权。

 解决方案:找同事复制了一份sonarQube.analysis文件,可能我原来的文件改错了。

登录sonarqube的账号密码要配置好。

2、 用VS2019带的MSbuild编译通过,但是生成报告的时候报错。

SonarScanner.MSBuild.exe end
SonarScanner for MSBuild 4.3.1
Using the .NET Framework version of the Scanner for MSBuild
Default properties file was found at E:\Software\sonar-scanner-msbuild-4.3.1.1372-net46\SonarQube.Analysis.xml
Loading analysis properties from E:\Software\sonar-scanner-msbuild-4.3.1.1372-net46\SonarQube.Analysis.xml
Post-processing started.
The SonarQube MSBuild integration failed: SonarQube was unable to collect the required information about your projects.
Possible causes:
  1. The project has not been built - the project must be built in between the begin and end steps
  2. An unsupported version of MSBuild has been used to build the project. Currently MSBuild 14.0 and 15.0 are supported
  3. The begin, build and end steps have not all been launched from the same folder
  4. None of the analyzed projects have a valid ProjectGuid and you have not used a solution (.sln)
Generation of the sonar-properties file failed. Unable to complete SonarQube analysis.
15:17:57.51  Creating a summary markdown file...
15:17:57.512  Post-processing failed. Exit code: 1

 原因是VS2019自带的MSBuild为16版本,而用的sonarscanner for msbuild 4.3.1只支持msbuild14或者15。

所以换了msbuild14编译,又提示编译不通过。

 报错信息为:CSC : error CS1617: Invalid option 'latest' for /langversion; must be ISO-1, ISO-2, Default or an integer in range 1 to 6. 

原因是代码用了新的语言,而MSbuild14不支持,所以还是要用msbuild16。

解决方案:重新下载最新版本的sonarscanner for msbuild(用的是5.3.1)

 又操作预处理,编译,上传报告成功。

3、关闭sonar服务,不仅需要关闭sonar的命令行窗口,还要在任务管理器,结束掉所有的java任务。

如果不关闭java服务,再启动sonar,会提示被占用。

 

 

 

Guess you like

Origin blog.csdn.net/seanyang_/article/details/120354400