当前位置:   article > 正文

Asp.Net&.Net Core 使用 SonarQube 踩坑记 (使用 MSBuild扫描器篇)_sonarscanner.msbuild.exe 扫描器下载

sonarscanner.msbuild.exe 扫描器下载

使用dotnet 需要 搭建 ner core的运行环境。

1.首先安装配置java运行环境。 且javaJDK 必须是11以上(jdk版本必须大于11 等于11不行)
2.java和java JDK后 记得配置java 和jdk建立连接和配置。
3.下载SonarQube安装包 (切记要下载社区版,不然分析后还需要申请许可证)

添加 SonarQube 解压后的 根目录路径到path环境变量

4.在这里插入图片描述
5.判断是否安装成功.,(浏览器访问:http://localhost:9000/,点击Login,默认管理员账号和密码都是 admin,进入到 Sonar 的项目管理界面)
在这里插入图片描述
6.下载 Sonar-Scanner for MSBuild安装与配置
下载地址:sonar-scanner-msbuild-4.3.1.1372
下载并解压之后,设置SonarQube Scanner for MSBuild的环境变量。
解压路径根目录路径。

例如我的解压路径是:C:\Users\Administrator\Downloads\sonar-scanner-msbuild-4.3.1.1372-net466,则把该路径添加到Path 
  • 1

7.修改SonarQube.Analysis.xml文件,要修改的地方只是关于SonarQube服务器的一些配置,如服务器URL、USER、PASSWORD等,详细配置修改如下:

<?xml version="1.0" encoding="utf-8" ?>
<!--
  This file defines properties which would be understood by the SonarQube Scanner for MSBuild, if not overridden (see below)
  By default the SonarScanner.MSBuild.exe picks-up a file named SonarQube.Analysis.xml in the folder it
  is located (if it exists). It is possible to use another properties file by using the /s:filePath.xml flag
  The overriding strategy of property values is the following:
  - A project-specific property defined in the MSBuild *.*proj file (corresponding to a SonarQube module) can override:
  - A property defined in the command line (/d:propertyName=value) has which can override:
  - A property defined in the SonarQube.Analysis.xml configuration file [this file] which can override:
  - A property defined in the SonarQube User Interface at project level which can override:
  - A property defined in the SonarQube User Interface at global level which can't override anything.
  Note that the following properties cannot be set through an MSBuild project file or an SonarQube.Analysis.xml file:
  sonar.projectName, sonar.projectKey, sonar.projectVersion
  The following flags need to be used to set their value: /n:[SonarQube Project Name] /k:[SonarQube Project Key] /v:[SonarQube Project Version]
-->
<SonarQubeAnalysisProperties  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.sonarsource.com/msbuild/integration/2015/1">
    
  <Property Name="sonar.host.url">http://sonar_ip:sonar_port</Property>
  <Property Name="sonar.login">admin</Property>
  <Property Name="sonar.password">admin</Property>
   
  <!-- Required only for versions of SonarQube prior to 5.2 -->
    
  <Property Name="sonar.jdbc.url">jdbc:mysql://db_ip:db_port/sonar?useUnicode=true;characterEncoding=utf8;rewriteBatchedStatements=true;useConfigs=maxPerformance;useSSL=false</Property>
  <Property Name="sonar.jdbc.username">项目名称</Property>
  <Property Name="sonar.jdbc.password">网站上生成的项目token</Property>
   
</SonarQubeAnalysisProperties>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

8.MSBuild安装与配置
Visual Studio IDE在编译*.sln解决方案时默认是调用msbuild.exe来实现的。如果你的机器上没有装有Visual Studio,那么也可以单独使用MSBuild来编译.sln(工程解决方案)或.csproj(项目)。MSBuild可以直接通过.NETFramework来安装获得。通过下载MSBuild来使用它

X86: C:\Program Files (x86)\MSBuild\14.0\Bin\MSBuild.exe
X64: C:\Program Files (x86)\MSBuild\14.0\Bin\amd64\MSBuild.exe
  • 1
  • 2

将MSBuild.exe添加到Path环境变量,便于后面在命令行中调用MSBuild。

MSBuild MyApp.sln /t:Rebuild /p:Configuration=Release
MSBuild MyApp.csproj /t:Clean /p:Configuration=Debug;/p:Platform=x86;TargetFrameworkVersion=v3.5
 
编译为 Release 代码 -p:configuration="release"
清理项目 -t:clean
重新编译 -t:rebuild
编译项目 -t:build 默认可以忽略这个参数
发布 -t:Publish
 
注意:这里的 -t 和 /t 作用是相同的。
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

9.分析项目
跳转到项目根目录 ,必须和.sln或者.csproj同级目录。

dotnet sonarscanner begin /k:"网上上定义的项目名称" /d:sonar.host.url="http://localhost:9000" /d:sonar.login="网站上生产的项目token"
  • 1

10 生成编译项目
可以通过dotnet build编译项目 或者 通过MSBuild编译器 编译项目(微软的编译器),在CMD或者终端命令行下执行:

MSBuild.exe /t:Rebuild   (默认为Debug模式 ,编译项目) 
或者
MSBuild.exe /t:Rebuild /p:Configuration=Release  (指定编译模式)
或者
MSBuild.exe D:\hcloud\Common\Common.sln /t:Rebuild  (指定具体的.sln解决方案)

------
或者使用 dotnet 来编译项目 
dotnet build 项目文件名或者解决方案名(若是分层开发,则依次编译各层项目文件)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

在这里插入图片描述

**

编译项目时 如果 出现 找不到文件 Import Project的错误 说明 项目配置文件 有问题, 在当前编译项目的 目录下的csproj文件 找到 类似的配置xml参数 并且更改为以下代码即可 解决问题。**

<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
  <Import Project="$(VSToolsPath)\WebApplications\Microsoft.WebApplication.targets" Condition="'$(VSToolsPath)' != ''" />
  <Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" Condition="false" />
  • 1
  • 2
  • 3

11 执行分析,并提交到远程结果。
切记 这两套不同的命令 不要混合使用。

--使用dotnet命令 执行分析
dotnet sonarscanner end /d:sonar.login="项目token"
或者 这个工具
SonarScanner.MSBuild.exe end
  • 1
  • 2
  • 3
  • 4

在这里插入图片描述

12 最后执行分析提交远程 容易出现 如下错误:
在这里插入图片描述

错误信息: WARNING: The following projects do not have a valid ProjectGuid and were not built using a valid solution (.sln) thus will be skipped from analysis…
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.
16:09:26.217 Post-processing failed. Exit code: 1

需要 使用dotnet命令新建解决方案文件 且添加到该文件:dotnet new sln
具体dotnet命令可上网查看相关命令

new.sln:新创建的解决方案文件
dotnet sln new.sln add 项目路径.csproj

多层开发的项目,需要依次添加各层的csproj文件

13 添加完 解决方案文件后 重新执行 分析命令

dotnet sonarscanner end /d:sonar.login="ee3602c8cfb60b2841bcf62fac9d7645cddf0507"
  • 1

成功后

在这里插入图片描述

第一次写博客 写的不清楚的地方 ,希望大家多多指教!!!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/正经夜光杯/article/detail/973103
推荐阅读
  

闽ICP备14008679号