Asp.Net&.Net Core 使用 SonarQube 踩坑记 (使用 MSBuild扫描器篇)

使用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 

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>

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

将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 作用是相同的。

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

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

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 项目文件名或者解决方案名(若是分层开发,则依次编译各层项目文件)

在这里插入图片描述

**

编译项目时 如果 出现 找不到文件 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" />

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

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

在这里插入图片描述

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"

成功后

在这里插入图片描述

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

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
1. 安装.NET Core SDK 在Jenkins中使用.NET Core编译项目之前,需要先安装.NET Core SDK。可以在官方网站下载适合您的操作系统的SDK。 2. 安装Jenkins插件 使用Jenkins进行.NET Core编译需要安装MSBuild插件。可以在Jenkins插件管理器中搜索并安装该插件。 3. 配置工具路径 在Jenkins中配置.NET Core SDK的路径非常重要。在Jenkins管理界面中,选择“系统设置”,找到“.NET Core SDK”并配置SDK的路径。确保SDK路径指向正确的目录。 4. 配置构建步骤 在Jenkins中配置构建步骤时,需要使用MSBuild”构建步骤。将项目的.csproj文件作为参数传递给MSBuild构建步骤,如下所示: ``` msbuild.exe /t:restore msbuild.exe /t:build /p:Configuration=Release /p:OutputPath=<path to publish output> /p:PublishProfile=<path to publish profile> <path to project file> ``` 其中,“/t:restore”用于还原项目依赖项,“/t:build”用于编译项目,“/p:Configuration=Release”指定编译配置为Release,“/p:OutputPath=<path to publish output>”指定发布输出路径,“/p:PublishProfile=<path to publish profile>”指定发布配置文件路径,“<path to project file>”指定项目文件路径。 5. 配置发布 在Jenkins中配置自动发布非常简单。只需使用“Publish over SSH”插件将构建输出文件上传到目标服务器即可。 6. 配置IIS 在将应用程序部署到IIS时,需要做一些额外的配置。确保在IIS中创建应用程序池,并将.NET Core的版本设置为与安装的SDK版本相同。还需要安装.NET Core Hosting Bundle,以便IIS能够正确地运行.NET Core应用程序。 总结 .NET Core是一个非常好的开发框架,但在使用Jenkins进行部署时,需要注意一些细节。通过正确地安装和配置.NET Core SDK、Jenkins插件和IIS,可以轻松地使用Jenkins部署.NET Core应用程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

NET安梓晨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值