# Excluding 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.

{% hint style="info" %}
As a system administrator, you can perform these settings as the default settings for all projects in your SonarQube. See [excluding-files-based-on-file-paths](https://docs.sonarsource.com/sonarqube-community-build/instance-administration/analysis-functions/analysis-scope/excluding-files-based-on-file-paths "mention") for more information.
{% endhint %}

## Example of an initial scope adjustment <a href="#initial-scope-adjustment-example" id="initial-scope-adjustment-example"></a>

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.

<figure><img src="broken-reference" alt="Both test directories in this example must be excluded from the source code&#x27;s analysis scope"><figcaption></figcaption></figure>

We adjust the initial scope as follows:

* For source files: by defining an exclusion parameter with the pattern `src/**/test/**/*`

<div align="left"><figure><img src="https://1580440648-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FbqrfLGeD0Y9vE5l9Le42%2Fuploads%2FE3Pmj8qaqvLBRaFUusoJ%2Fsonarqube-source-file-exclusions.png?alt=media&#x26;token=6c3dbb52-fb63-4bad-bc72-00af97147adf" alt="Source file exclusions as found in the SonarQube user interface." width="401"><figcaption></figcaption></figure></div>

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

<div align="left"><figure><img src="https://1580440648-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FbqrfLGeD0Y9vE5l9Le42%2Fuploads%2FMtuj5BqtP0Jcq4qPhWfb%2Fsonarqube-test-file-inclusions.png?alt=media&#x26;token=2b4ec220-dc13-4752-ae91-3f3d71c97f8e" alt="Test file inclusions as found in the SonarQube user interface." width="431"><figcaption></figcaption></figure></div>

## Principles governing the use of file exclusion parameters <a href="#principles" id="principles"></a>

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.).

The following applies:

* The parameter defined at the project level will override the same parameter defined at the global level.\
  For example, if the system administrator defines the exclusion pattern for source code `src/**/test1/**/*` at the global level, then, if the project administrator sets the exclusion pattern for source code `src/**/test2/**/*` for their project, the scanner will consider only the pattern `src/**/test2/**/*` to compute source file exclusion.
* If *test file inclusion* patterns are used, the scanner will automatically set these patterns as *source file exclusion* patterns during project analysis. These source file exclusion patterns will apply in addition to the other configured source file exclusion patterns.\
  For example:
  * If the exclusion pattern for source code is `src/**/test5/**/*` and the inclusion pattern for test code is `src/**/test6/**/*`
  * Then the scanner will consider both patterns to compute the *source file exclusion*:\
    `src/**/test5/**/*` and `src/**/test6/**/*`

{% hint style="info" %}
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](https://docs.sonarsource.com/sonarqube-community-build/analyzing-source-code/analysis-parameters "mention")) or absolute.
{% endhint %}

## Defining a file exclusion parameter in the UI <a href="#defining-exclusion-parameter-in-ui" id="defining-exclusion-parameter-in-ui"></a>

1. Retrieve your project. See [retrieving-projects](https://docs.sonarsource.com/sonarqube-community-build/user-guide/viewing-projects/retrieving-projects "mention") for more details.
2. Go to **Project Settings** > **General Settings** > **Analysis Scope**.
3. 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](https://docs.sonarsource.com/sonarqube-community-build/project-administration/adjusting-analysis/setting-analysis-scope/defining-matching-patterns "mention") for more information.
4. Add additional patterns to the parameter if necessary.
5. 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**.

<div align="left"><figure><img src="https://1580440648-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FbqrfLGeD0Y9vE5l9Le42%2Fuploads%2F37CeRu68Hrp5HCB3R4Ul%2Fsonarqube-server-analysis-scope-file-exclusion-parameter.png?alt=media&#x26;token=6ac380b1-616d-4008-b258-592e760e4f5a" alt="Defining a file exclusion parameter in SonarQube user interface." width="563"><figcaption></figcaption></figure></div>

{% hint style="info" %}
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.
{% endhint %}

## Defining a file exclusion parameter on the CI/CD host <a href="#defining-exclusion-parameter-on-ci-cd-host" id="defining-exclusion-parameter-on-ci-cd-host"></a>

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](https://docs.sonarsource.com/sonarqube-community-build/analyzing-source-code/analysis-parameters "mention").

| **Property key**      | **Description**                                                                                                                                                                                                                                                                                                                                                                                                                                         |
| --------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| sonar.exclusions      | <p>Defines the source files (non-test files) to be excluded from the analysis.</p><p><strong>Possible values</strong>: Comma-separated list of path-matching patterns. See <a data-mention href="defining-matching-patterns">defining-matching-patterns</a>.</p>                                                                                                                                                                                        |
| sonar.inclusions      | <p>Defines the source files (non-test files) to be included in the analysis. The other files will be excluded.</p><p><strong>Possible values</strong>: Comma-separated list of path-matching patterns. See <a data-mention href="defining-matching-patterns">defining-matching-patterns</a>.</p>                                                                                                                                                        |
| sonar.test.exclusions | <p>Defines the test files to be excluded from the analysis.</p><p><strong>Possible values</strong>: Comma-separated list of path-matching patters. See <a data-mention href="defining-matching-patterns">defining-matching-patterns</a>.</p><p><strong>Note</strong>: In this property key, the <code>test</code> string is in singular, unlike the <code>sonar.tests</code> property defining the initial scope</p>                                    |
| sonar.test.inclusions | <p>Defines the test files to be included from the analysis. The other files will be excluded.</p><p><strong>Possible values</strong>: Comma-separated list of path-matching patterns. See <a data-mention href="defining-matching-patterns">defining-matching-patterns</a>.</p><p><strong>Note</strong>: In this property key, the <code>test</code> string is in singular, unlike the <code>sonar.tests</code> property defining the initial scope</p> |

## Related pages <a href="#related-pages" id="related-pages"></a>

* [exclude-from-coverage-duplication](https://docs.sonarsource.com/sonarqube-community-build/project-administration/adjusting-analysis/setting-analysis-scope/exclude-from-coverage-duplication "mention")
* [excluding-based-on-file-extension](https://docs.sonarsource.com/sonarqube-community-build/project-administration/adjusting-analysis/setting-analysis-scope/excluding-based-on-file-extension "mention")
* [advanced-exclusions](https://docs.sonarsource.com/sonarqube-community-build/project-administration/adjusting-analysis/setting-analysis-scope/advanced-exclusions "mention")
* [other-adjustments](https://docs.sonarsource.com/sonarqube-community-build/project-administration/adjusting-analysis/setting-analysis-scope/other-adjustments "mention")
* [setting-initial-scope](https://docs.sonarsource.com/sonarqube-community-build/project-administration/adjusting-analysis/setting-analysis-scope/setting-initial-scope "mention")
* [verifying-analysis-scope](https://docs.sonarsource.com/sonarqube-community-build/project-administration/adjusting-analysis/setting-analysis-scope/verifying-analysis-scope "mention")
* [introduction](https://docs.sonarsource.com/sonarqube-community-build/instance-administration/analysis-functions/analysis-scope/introduction "mention") to Analysis scope
