2017-12-22 63 views
2

Sonarqubeを使用すると、sonar.coverage.exclusionsキーにパターンを追加することで、個々のファイルをコードカバレッジから除外することができます。これは、プロジェクトレベルで、UIで、さらにはSonarQubeSetting要素を指定することによって、.csprojファイルに追加することによって行うことができます。私。Sonarqubeを.NET(C#)プロジェクトから除外する方法

<SonarQubeSetting Include="sonar.coverage.exclusions"> <Value>**/*.cs</Value> </SonarQubeSetting>

しかし、これらのアプローチの両方が動作していないようです。 SonarQube documentationで指定されているパターンで演奏しても、結果は得られません。私はまた、SonarQubeExclude MSBuildプロパティの存在を認識していますが、私のプロジェクトを他のすべての分析から除外するので、そのパスを下りたくないです。

別の可能性がありますか?あるいは、プロジェクト内のすべてのクラスをコードカバレッジから除外することはできませんか?

答えて

0

私は同じ問題を抱えていたし、それはそれを把握するために私にしばらく時間がかかった:

<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0"> 

    <!-- This target customises the SonarQube MSBuild runner targets to limit the projects that are analysed. 

     Projects whose full path and file name match the specified filter will be marked as "excluded". 

     Note that this targets file does not ever set $(SonarQubeExclude) to "false". This is to allow other 
     targets to exclude projects for other reasons (e.g. a Fakes projects should always be excluded, regardless 
     of whether or not they match the project filter). 

     Also, this project will do nothing if $(SonarQubeExclude) has already been set. 

     The regular expression uses the normal .NET regular expression syntax. 

     Usage: 
     (1) include the target in the projects being built, either by directly importing it or by 
      dropping it in the one of the standard "ImportBefore" folders. 

     (2) set $(SQExclusionFilter) to the desired regular expression 
      e.g. the following example matches all projects with "CodeSense\Services" in the path: .*\\CodeSense\\Services\\.* 

    --> 
    <PropertyGroup Condition=" $(SonarQubeExclude) == '' AND $(SQExclusionFilter) != '' "> 

     <MatchesSQExclusionFilter Condition="$([System.Text.RegularExpressions.Regex]::IsMatch($(MSBuildProjectFullPath), $(SQExclusionFilter), System.Text.RegularExpressions.RegexOptions.IgnoreCase)) ">true</MatchesSQExclusionFilter> 
     <SonarQubeExclude Condition="$(MatchesSQExclusionFilter) == 'true' " >true</SonarQubeExclude> 

    </PropertyGroup> 

    <!-- This target is not required: it simply writes out additional information to simplify debugging --> 
    <Target Name="AnalysisProjectInfoExclude_DEBUG" BeforeTargets="CoreCompile" 
      Condition="$(SQExclusionFilter) != '' "> 

    <Message Importance="high" Text="ExclusionFilter: filter has been set. Filter= $(SQExclusionFilter)" /> 
    <Message Importance="high" Text="ExclusionFilter: current project = $(MSBuildProjectFullPath)" /> 
    <Message Importance="high" Text="ExclusionFilter: match result = $(MatchesSQExclusionFilter)" /> 

    <Message Importance="high" Condition="$(MatchesSQExclusionFilter) == 'true' " 
     Text="ExclusionFilter: project is excluded" /> 

    <Message Importance="high" Condition="$(MatchesSQExclusionFilter) != 'true' " 
     Text="ExclusionFilter: project has not been excluded by the exclusion filter" /> 

    </Target> 

</Project> 

はこのコンテンツとソリューションのディレクトリにDirectory.Build.propsファイルを設定します

ここでは、環境変数SQExclusionFilter交流で正規表現を設定しhttps://docs.microsoft.com/en-us/visualstudio/msbuild/customize-your-build

を説明したように、このフラグメントは、すべてのプロジェクトファイルに含まれます言い換えると、パスの区切りには二重バックスラッシュを使用します。 Windowsシェルのエスケープパイプ文字には、キャレットが付きます。

set SQExclusionFilter=(path1\\project)^|(path2\\project) 

SQ/MSからのDuncan Pocklingtonへのクレジット!

+0

私はあなたのソリューションを見てきましたが、私はこれらのMSBuild変数とSonarQubeとの関係を見るために失敗しています。これらの変数はSonarQube内であらかじめ定義されていますか?私はコードカバレッジを除外したいと思っている間に、これによってプロジェクト全体を除外しようとしていることもありますか? – mvandevy

+0

@mvandevy、そうです、ソリューションはすべての分析からプロジェクトを除外します。 フィルタがプロジェクトにヒットした場合、XMLフラグメント true がプロジェクトに追加され、それによって、そのプロジェクトのSonarQubeが抑制されます。 –