SonarQube MSBuild Scanner文件排除失效及覆盖率排除需求咨询
Alright, let's break this down for you—since you want a file-based, low-maintenance approach that doesn't require touching your TFS build tasks, SonarQube's native configuration files are perfect here. Your Scanner version (4.1.1) fully supports the rules we'll use, so no compatibility issues to worry about.
1. Fully exclude ProjectA/Scripts from all Sonar analysis (quality rules + code coverage)
This directory needs to be completely ignored by Sonar—no code checks, no coverage stats. We'll add a configuration file directly in the ProjectA folder so the Scanner picks it up automatically:
Option 1: Use sonar-project.properties (works for all project types)
Create a file named sonar-project.properties in your ProjectA directory with this content:
# Exclude all files under the Scripts directory from any Sonar analysis sonar.exclusions=Scripts/**/*
Option 2: Use Directory.Build.props (for .NET projects)
If you're working with .NET projects, you can add Sonar-specific settings to your existing Directory.Build.props (or create a new one) in ProjectA:
<PropertyGroup> <!-- Tell SonarQube to exclude the Scripts directory entirely --> <SonarQubeExclusions>Scripts/**/*</SonarQubeExclusions> </PropertyGroup>
2. Exclude ProjectB/Views only from code coverage (keep quality analysis)
Here you want Sonar to still run code quality checks on the Views folder, but skip counting its coverage. We'll target just the coverage exclusion in the ProjectB directory:
Option 1: Use sonar-project.properties
Create a sonar-project.properties file in ProjectB with:
# Keep Views in code analysis, but exclude from coverage statistics sonar.coverage.exclusions=Views/**/*
Option 2: Use Directory.Build.props (for .NET projects)
Add this to ProjectB's Directory.Build.props:
<PropertyGroup> <!-- Exclude Views folder only from coverage, not from code quality checks --> <SonarQubeCoverageExclusions>Views/**/*</SonarQubeCoverageExclusions> </PropertyGroup>
Why this works without modifying TFS build tasks
The SonarQube Scanner for VSTS/TFS automatically scans for sonar-project.properties files in your solution/project directories, and picks up Sonar-related properties from .NET project files (like Directory.Build.props) by default. This means you don't need to edit any build task parameters—just drop these files in place, and the Scanner will apply the rules on its own.
How to verify
After your next build:
- Check the SonarQube project's Administration > Analysis Scope page to confirm the exclusion rules are listed
- Look for lines like
Excluded sources: ...orExcluded from coverage: ...in your TFS build logs to confirm the Scanner applied the rules
内容的提问来源于stack exchange,提问作者Marcell Spies

