Reputation: 200
In SonarQube runner, it was possible to manage excluded projects through the sonar.exclusions property in the sonar project properties. This way we could manage build configuration separate from the code base.
We're migrating from the runner to scanner for MSBuild to take advantage of running FXCop rules on our code.
Leaving the sonar-project.properties file in the root folder of the project results in an exception in the "MSBuild.SonarQube.Runner.exe end" command. sonar-project.properties files are not understood by the SonarQube Scanner for MSBuild. Remove those files from the following folders: [folder]
The only option I can see now for excluding specific projects (actually all projects which name ends with .Test, .Tests, .Testing, .UnitTests, etc) appears to be adding a project property to a propertyGroup in each affected project: <SonarQubeExclude>true</SonarQubeExclude>
This is hard to maintain, error prone and cumbersome, compared to managing the settings on root level (which we did for jenkins) or in the build settings (which we currently use in TeamCity).
Is there any alternative? Or planned for the future?
When the project folder is called ***.Tests, it is still scanned after adding it to the the exclusion settings.
[14:14:14][Step 12/23] INFO: ------------- Scan MyProject.Tests
[14:14:14][Step 12/23] INFO: Excluded sources for coverage:
[14:14:14][Step 12/23] INFO: **/*.Tests/**/*
[14:14:14][Step 12/23] INFO: **/*.Test/**/*
[14:14:14][Step 12/23] INFO: Base dir: C:\SomeFolder\MyProject\Modules\MyProject.Tests
[14:14:14][Step 12/23] INFO: Working dir: C:\SomeFolder\MyProject\.sonarqube\out\.sonar\{Sonar_Project}_{some guid}
[14:14:14][Step 12/23] INFO: Test paths: [I removed some classes], Utils/SomeTests.cs, Enum/dummy.resx, app.config, Compression/TestData/data1.FRM, Compression/TestData/data1.zip, Compression/TestData/data1.Off.zip, packages.config
[14:14:14][Step 12/23] INFO: Source encoding: UTF-8, default locale: en_US
[14:14:14][Step 12/23] INFO: Index files
[14:14:15][Step 12/23] INFO: 45 files indexed
[14:14:15][Step 12/23] INFO: Quality profile for cs: [some profile]
[14:14:15][Step 12/23] INFO: Quality profile for vb: [some profile]
Upvotes: 4
Views: 3892
Reputation: 62764
Why not do the following:
Directory.Build.props
with <SonarQubeExclude>true</SonarQubeExclude>
Works since MSBuild 15 (i.e. VS 2017)
That's it.
Upvotes: 0
Reputation: 200
For those interested... After more than a year, we haven't found an alternative, so we stick to the following workaround, a small powershell script we added to our TeamCity build steps before the build, which is responsible for adding a property group to exclude the project from Sonar analysis to the project files ending with Tests.csproj:
$dir = "C:\Temp\ExcludeProjectsFromSonar"
Get-ChildItem $dir *Tests.csproj -recurse |
% {
$root = [xml](gc $_.FullName);
$project = $root.Project;
$propertyGroup = $root.CreateElement("PropertyGroup", $project.NamespaceURI);
$comment = $root.CreateComment("Exclude the project from analysis");
$sonarExclude = $root.CreateElement("SonarQubeExclude", $project.NamespaceURI);
$sonarExclude.InnerText = 'true';
$propertyGroup.AppendChild($comment);
$propertyGroup.AppendChild($sonarExclude);
$project.AppendChild($propertyGroup);
$root.Save($_.FullName);
}
Upvotes: 3
Reputation: 22804
There is no documentation on managing these property groups in the analysis parameters because it is very difficult to get right. Instead, you should be using the UI to manage exclusions: Administration > General Settings > Analysis Scope.
For more, see the docs.
Upvotes: 1