Excluding files from your project analysis based on path-matching patterns
You can adjust your project's initial analysis scope by excluding files based on path-matching patterns. To exclude the files, you define file exclusion parameters based on directory and file name patterns.
You can perform the setup in SonarQube UI (this requires that you have the project's Administer permission) or on the CI/CD host. A parameter set on the CI/CD host has precedence over any UI setting of the same parameter.
As a system administrator, you can perform these settings as the default settings for all projects in your SonarQube. See Excluding files from analysis based on path-matching patterns at the global level.
Example of an initial scope adjustment
We consider the following repository example where test files are contained in both test/
directories. Source and test code files are contained in the same ancestor directory: src/
which is chosen as the initial analysis scope for both source and test code. Therefore, a scope adjustment is necessary.

We adjust the initial scope as follows:
- For source files: by defining an exclusion parameter with the pattern
src/**/test/**/*

- For test files: by defining an inclusion parameter with the pattern
src/**/test/**/*

Principles governing the use of file exclusion parameters
A file exclusion parameter:
- Applies either to source code (also called main code) or to test code files.
The SonarScanner must identify the source code as well as the test code since they are processed differently by SonarQube. A code file is either a source or a test code; it cannot be both (If this is the case, the scanner will fail the analysis with an error message.). - Contains:
- Either exclusion patterns: to define files to be excluded from the analysis scope.
- Or inclusion patterns: to define files to be included in the scope.
It means that the rest of the files is excluded from the analysis scope.
For a given code category (source or test), we strongly recommend that you use either exclusion-pattern or inclusion-pattern parameters, depending on what is simpler in your situation (If you do not and there is an overlapping, then exclusion patterns have precedence over inclusion patterns.).
A file path definition is either relative to the sonar.projectBaseDir
property (which is by default the directory from which the analysis was started, for more information see Analysis parameters) or absolute.
Defining a file exclusion parameter in the UI
- Retrieve the project you want to configure.
- Go to Project Settings > General Settings > Analysis Scope.
- In A. File Exclusions, choose the parameter to configure (source or test code; exclusion or inclusion patterns), and enter and save the first pattern. See Defining matching patterns for files.
- Add additional patterns to the parameter if necessary.
- Define other parameters if necessary. Make sure you use either Source File Exclusions or Source File Inclusions, and either Test File Exclusions or Test File Inclusions.
If a parameter is defined at the global level, it will appear at the project level as “(default)”. You can edit it for your project. Click Reset to reset the value to its default value.
Defining a file exclusion parameter on the CI/CD host
The table below lists the properties you can use to define a file exclusion parameter by setting sonar properties on CI/CD host. For more information, see Analysis parameters.
Property key | Description |
---|---|
sonar.exclusions | Defines the source files (non-test files) to be excluded from the analysis. Possible values: Comma-separated list of path-matching patterns. |
sonar.inclusions | Defines the source files (non-test files) to be included in the analysis. The other files will be excluded. Possible values: Comma-separated list of path-matching patterns. |
sonar.test.exclusions | Defines the test files to be excluded from the analysis. Possible values: Comma-separated list of path-matching patterns. Note: In this property key, the |
sonar.test.inclusions | Defines the test files to be included from the analysis. The other files will be excluded. Possible values: Comma-separated list of path-matching patterns. Note: In this property key, the |
Related pages
- Excluding specific files from your project's code coverage analysis or duplication check
- Excluding files from your project analysis based on file extension
- Applying advanced exclusions to your project analysis
- 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?