首页 > 解决方案 > SonarScanner for MsBuild 可以扫描 TSQL

问题描述

我安装了 SonarQube,我们正在尝试在包含以下代码类型的产品上运行它

现在我们运行它来扫描除 T/SQL 代码之外的所有代码。

此 TSQL 代码与所有其他代码位于同一目录下,但没有特定的 Visual Studio 项目。

我们能够在 SQL 上运行扫描的唯一方法是使用标准运行程序sonarqube,但这会导致在我们的仪表板上创建一个新产品。

任何想法或建议。

标签: sonarqubesonarqube-scansonar-runner

解决方案


目前,如果您希望分析 TSQL 文件并将其显示在与其他代码相同的 SonarQube 项目下,则需要从 MSBuild 项目中引用它。

有几种方法可以做到这一点:

1) 使用如下代码段在现有项目之一中包含 TSQL 文件:

<ItemGroup>
  <!-- Include additional files that should be analyzed by the SonarScanner for MSBuild -->
  <None Include="*.tsql" >
    <!-- Don't show the items in the Solution Explorer -->
    <Visible>False</Visible>
  </None>
</ItemGroup>

2) 创建一个单独的虚拟 MSBuild 项目,其唯一目的是指定要分析的其他文件。这稍微复杂一些,因为虚拟项目需要一些额外的内容才能使其与 SonarScanner for MSBuild 目标一起使用。以下模板适用于 v4.3 的扫描仪,也应该适用于最近的早期版本。

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

  <!-- The only purpose of this project is to specify additional files to be analyzed by the SonarScanner for MSBuild -->

  <!-- 1. Set a unique GUID id for the project -->
  <PropertyGroup>
    <ProjectGuid>{EA2BAA27-D799-4FBE-9430-7499ACF3E431}</ProjectGuid>
  </PropertyGroup>

  <!-- 2. Specify the files to be analysed --> 
  <ItemGroup>
    <SonarQubeAnalysisFiles Include="**\*.js" />
  </ItemGroup>


  <!-- ******************************************************** -->
  <!-- Boilerplate - no need to change anything below this line -->
  <!-- ******************************************************** -->
  <!-- Import the SQ targets (will only exist if the scanner "begin" step has been executed) -->
  <PropertyGroup>
    <SQImportBeforeTargets>$(localappdata)\Microsoft\MSBuild\$(MSBuildToolsVersion)\Microsoft.Common.targets\ImportBefore\SonarQube.Integration.ImportBefore.targets</SQImportBeforeTargets>
  </PropertyGroup>
  <Import Condition="Exists('$(SQImportBeforeTargets)')" Project="$(SQImportBeforeTargets)" />

  <!-- Re-define the standard step of targets used in builds -->
  <Target Name="Build" />
  <Target Name="Clean" />
  <Target Name="CoreCompile" />
  <Target Name="Rebuild" DependsOnTargets="Clean;Build" />

  <!-- Re-define one of the standard SQ targets as we have already set the list of files to analyze above -->
  <Target Name="CalculateSonarQubeFilesToAnalyze" >
    <PropertyGroup>
      <!-- Set a property indicating whether there are any files to analyze -->
      <AnalysisFilesExist Condition=" @(SonarQubeAnalysisFiles) != '' ">true</AnalysisFilesExist>
    </PropertyGroup>
  </Target>
</Project>

推荐阅读