sonarQube扫描.NetCore项目和.netframework

上一篇 / 下一篇  2019-09-18 17:10:26 / 个人分类:过程管理


 安装JAVA SDK

Sonar是一款基于JAVA开发的工具,安装JAVA SDK的过程在此不再叙述,建议安装好之后配置好JAVA_HOME的环境变量,以下是下载地址。

http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

 

安装SonarQube

首先到官网下载安装包,值得注意的是,该安装包是不分平台的,下载下来之后,选择Windows的文件夹中StartSonar.bat文件运行即可。

https://www.sonarqube.org/#downloads

配置Sonar

我们需要对Sonar进行简单配置,使其能连接上MySQL数据库

Sonar-Scanner for MSBuild安装与配置

下载并解压SonarQube Scanner for MSBuild,它是C# Framework的分析插件。

https://github.com/SonarSource/sonar-scanner-msbuild/releases/download/4.3.1.1372/sonar-scanner-msbuild-4.3.1.1372-net46.zip

解压之后,设置SonarQube Scanner for MSBuild的环境变量,如我的解压路径是:C:\MyWorkSpace\Tools\sonar-scanner-msbuild-4.3.1.1372-net46,则把该路径添加到path下:

 修改SonarQube.Analysis.xml文件

要修改的地方只是关于sonarQube服务器的一些配置,关于服务器URLUSERPASSWORD等,修改如下:

<?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://localhost:9000</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://localhost:3306/sonar?useUnicode=true;characterEncoding=utf8;rewriteBatchedStatements=true;useConfigs=maxPerformance;useSSL=false</Property>

  <Property Name="sonar.jdbc.username">sonar</Property>

  <Property Name="sonar.jdbc.password">sonar</Property>

 </SonarQubeAnalysisProperties>

接下来,重要的一步,找到你电脑中的MSBuild.exe并添加到path环境变量,便于后面在命令行中调用MSBuild,我的是在vs 2017的安装目录下

C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\amd64

 C#项目分析

CMD进入C#项目所在的根目录,依次执行以下三条命令。

MSBuild.SonarQube.Runner.exe begin /k:"xxh.xzc.api" /n:"xhh.xzc.api" /v:"1.0"  --分析的前期准备

MSBuild.exe /t:Rebuild     --MSBuild编译,MSBuild是系统安装过VS后会存在的一个编译器,以Visual Studio 2013为例,地址是:C:\Program Files (x86)\MSBuild\12.0\Bin

MSBuild.SonarQube.Runner.exe end   --告诉sonarQube运行完成了,将报告上传SonarQube

参数说明:

SonarScanner.MSBuild.exe begin /k:"yj-Middleware" /d:sonar.host.url="http://localhost:9000" /d:sonar.login="744c1a457e6c36a9b72296bc0a425ccfeb64f51e"

MsBuild.exe /t:Rebuild

SonarScanner.MSBuild.exe end /d:sonar.login="744c1a457e6c36a9b72296bc0a425ccfeb64f51e"

/name(简写n):对应projectName即项目的名称,为项目的一个显示的名称,建议使用完整的项目名称

/version(简写v):对应projectVersion即项目的版本,项目在不同的时期版本也是不一样的,如果方便,可以在sonarQube的服务器中查看到不同的版本代码其中问题的变化

三条命令分别是分析的前期准备,MSBuild编译,将报告上传给SonarQube

 查看分析结果

最后,进入http://localhost:9000/projects 查看分析结果,界面中功能强大,很多认为绝对发现不了的Bug都展现出来了,还可以查看单元测试的覆盖率。

 特别注意:1.Netcore框架前边配置相同,需在path环境变量中配置.Netcore的安装路径;

以上的步骤对。netframework框架的c#代码是没问题的,但对。Netcore是不行的,需要下载安装.netcore并配置paht环境变量,安装。netcore的编译环境。

1Installation of the SonarScanner for MSBuild .NET Core Global Tool在命令行中安装.net core,命令如下

dotnet tool install --global dotnet-sonarscanner --version 4.3.1

 

2.执行的命令与.netframwork不同,.netcore的扫描命令如下

dotnet <path to SonarScanner.MSBuild.dll> begin /k:"project-key"

dotnet build <path to solution.sln>

dotnet <path to SonarScanner.MSBuild.dll> end

dotnet sonarscanner begin /k:"LittleTree.Management" /d:sonar.host.url="http://localhost:9000" /d:sonar.login="project-key"

 

dotnet build D:\sonarProject\LittleTree.Management

 

dotnet sonarscanner end /d:sonar.login="project-key"

 

.netframwork命令如下:

SonarScanner.MSBuild.exe begin /k:"project-key"

MSBuild.exe <path to solution.sln> /t:Rebuild


TAG:

 

评分:0

我来说两句

日历

« 2024-03-15  
     12
3456789
10111213141516
17181920212223
24252627282930
31      

数据统计

  • 访问量: 10574
  • 日志数: 7
  • 图片数: 1
  • 建立时间: 2015-03-05
  • 更新时间: 2019-09-18

RSS订阅

Open Toolbar