问题
Is there a way to programatically exclude artifacts from Sonar analysis using the MSBuild Scanner?
We have a number of different Visual Studio solutions in our git repository. Almost all of these solutions make use of projects containing shared code. I am wishing to exclude code coverage from these common projects - but only for certain Sonar projects.
Always excluding a project is straightforward and documented via adding a <PropertyGroup>
section in the .csproj
.
Adding the /d:sonar.coverage.exclusions="**/MyCommonCode/**"
argument to the MSBuild scanner appears to do nothing, nor does adding exclusion patterns in Sonar itself.
Rationale
I am wishing to maintain accurate code coverage metrics for code using these common projects. If the common code had 100,000 lines and is well tested but my project only has 10,000 lines and has zero tests, the Sonar code coverage would be artificially favourable.
Instead, I intend to have a separate Sonar project containing just the coverage of this common code.
We are using SonarQube 7.0, and the SonarScanner for MSBuild 4.0.2.
回答1:
You could add condition to the MSBuild snippet you referred to, and use that to control whether the common projects are included in the analysis or not e.g.
<PropertyGroup>
<!-- Exclude the project from analysis -->
<SonarQubeExclude Condition="$(ExcludeCommonCode)=='true'" >true</SonarQubeExclude>
</PropertyGroup>
...and then pass the argument /p:ExcludeCommonCode=true to MSBuild for the builds of the solutions for which you don't want to analyse the common code.
Excluding the common projects in this way does more than just exclude those projects from code coverage: it means the code won't be analysed at all (no issues and no metrics), and those MSBuild projects won't appear under that SonarQube project at all. However, I guess this is what you want if you have a separate SonarQube project for analysing the common projects.
来源:https://stackoverflow.com/questions/50499737/programmatic-coverage-exclusions-in-sonarqube-msbuild-runner