Applying advanced exclusions to your project analysis
In very specific situations, you may have to:
- Exclude files based on the file content.
- Exclude blocks within files.
- Exlude specific files from specific coding rules.
It is strongly advised to configure these features in SonarQube UI. The configuration on the CI/CD host which is very tedious and implies using a set of properties is not documented in this guide.
As a system administrator, you can perform these settings as the default settings for all projects of your SonarQube Community Build. See Using advanced exclusion features at the global level.
Excluding files based on file content
You can exclude from the analysis files that contain a block of code matching a given regular expression. You can enter one or more regular expression patterns. Any file containing at least one of the specified patterns will be ignored.
The parameter to be configured is Ignore Issues on Files. For a configuration example of this parameter, see below.
To define the Ignore Issues on Files parameter:
- Retrieve your project.
- Go to Project Settings > General Settings > Analysis scope.
- In D. Issue Exclusions > Ignore Issues on Files, enter and save a regular expression pattern.
- You can enter a second regular expression pattern and so on.
Example of Ignore Issues on Files parameter configuration
Let's say you have generated class files in your Java project that you wish to exclude. The files look something like this:
@Generated("com.example.generated")
public class GeneratedClass extends AnotherClass {
// Some generated code
}
To exclude all such files, you might set the Ignore Issues on Files parameter to the following regular expression: @Generated\(".*"\)
Excluding blocks within files
You can exclude from the analysis specific blocks contained in any source file (The rest of the file will be analyzed.). The parameter to be configured is Ignore Issues in Blocks.
Principles governing the use of the Ignore Issues in Blocks parameter
Blocks to be ignored are delimited within the file by start and end strings specified by regular expression patterns:
- If the first regular expression is found but not the second one, the end of the file is considered to be the end of the block.
- Regular expressions are not matched across multiple lines.
Any block - within any file - containing at least one of the specified patterns will be ignored.
Defining the Ignore Issues in Blocks parameter
- Retrieve your project.
- Go to Project Settings > General Settings > Analysis scope.
- In D. Issue Exclusions > Ignore Issues in Blocks, enter and save a block definition.
- You can enter a second block definition and so on.
Example of Ignore Issues in Blocks parameter configuration
You can use block delimiters to specify the block to be excluded as illustrated below. In this example, you want to ignore the code in the method doSomethingElse
below delimited by // BEGIN-NOSCAN
and // END-NOSCAN
.
public class MyClass {
public MyClass() {
...
}
public void doSomething() {
...
}
// BEGIN-NOSCAN
public void doSomethingElse()
{
...
}
// END-NOSCAN
}
You could define the block to be excluded with the following regular expressions:
- Start of block:
\s*//\s*START-NOSCAN
- End of block:
\s*//\s*END-NOSCAN
These regular expressions ensure that the start and end block delimiters will be recognized regardless of the number of spaces around the line comment characters (//
).
Excluding specific files from specific coding rules
This section explains how to exclude specific files from specific coding rules in your project analysis.
Introduction to coding rules exclusion
To exclude specific files from specific coding rules, you can:
- Exclude specific files from the check against specific coding rules.
To do so, you define exclusion criteria. An exclusion criterion is a combination of:- A coding rule key pattern: specifies the coding rules to be excluded.
- A file path pattern: specifies the files to which the specified coding rules will not be applied.
- Apply the check against specific coding rules to specific files. It means that the other files are excluded from this check.
To do so, you define inclusion criteria. An inclusion criterion is a combination of:- A coding rule key pattern: specifies the coding rules to be applied.
- A file path pattern: specifies the files to which the specified rules will be applied. The specified rules will not be applied to the other files.
Defining coding rule inclusion or exclusion criteria
- Retrieve your project.
- Go to Project Settings > General Settings > Analysis scope.
- In the D. Issue Exclusions > Ignore Issues on Multiple Criteria parameter (to define an exclusion criterion), or in Restrict Scope of Coding Rules (to define an inclusion criteria), enter and save a pair consisting of (see Defining matching patterns):
- A pattern for coding rule keys.
- A pattern for file paths.
- You can add a second criterion, and so on.
Examples of Ignore Issues on Multiple Criteria parameter configuration (inclusion criterion)
Example | Inclusion criterion |
---|---|
Check:
|
|
Check:
| Two criteria must be used. Criterion 1:
Criterion 2:
|
Examples of Restrict Scope of Coding Rules parameter configuration (exclusion criterion)
Example | Exclusion criterion |
---|---|
Ignore:
|
|
Ignore:
|
|
Ignore:
|
|
Ignore:
|
|
Related pages
- Excluding specific files from your project's code coverage analysis or duplication check
- Excluding files from your project analysis based on path-matching patterns
- Excluding files from your project analysis based on file extension
- Performing other analysis scope adjustments
- Setting the initial scope of your project
- Verifying the analysis scope of your project
- Adjusting the analysis scope at the global level
Was this page helpful?