Skip to content
Tutorials Center
My learning

Topics

Lesson 18 of 37 ยท Scanning and Integrations

Exclusions and Test Sources

Control what SonarQube analyzes: sonar.exclusions, inclusions, test sources, coverage and duplication exclusions, ant-style patterns, and an interactive pattern matcher.

Not every file deserves analysis. Generated code, vendored libraries, minified bundles and database migrations produce noise: issues nobody should fix, duplication nobody wrote, and coverage nobody can reach. SonarQube lets you keep them out of specific measures with a family of exclusion properties. This lesson explains each, the pattern syntax and the common traps.

The properties

All of these are comma-separated lists of patterns, relative to the project base directory:

  • sonar.exclusions: files that are not analyzed at all (no issues, no measures, not counted as lines of code).
  • sonar.inclusions: restrict analysis to only the matching files within the sources.
  • sonar.test.exclusions and sonar.test.inclusions: the same, for test files.
  • sonar.coverage.exclusions: files that are analyzed (issues, size) but ignored for coverage. Use for code you do not unit test, such as thin configuration or generated adapters.
  • sonar.cpd.exclusions: files ignored by duplication detection only.
  • sonar.issue.ignore.multicriteria: ignore specific rules in specific files (for example a rule on a legacy package).

Think of them as layers: exclusion removes a file entirely; the others remove it from one measure.

Ant-style patterns

Patterns use the Ant syntax:

  • * matches any characters within one path segment (not /)
  • ? matches one character
  • ** matches any number of directories, including none

Examples:

  • **/*.min.js all minified scripts anywhere
  • src/generated/** everything under that folder
  • **/migrations/* files directly in any migrations folder
  • **/*Dto.java files whose names end in Dto.java
  • app/legacy/**/* the contents of that tree

Patterns are matched against the path relative to the project base directory, with forward slashes on every operating system.

An interactive matcher

The stand-in below converts Ant patterns to regular expressions so you can try them on file lists before running a scan. The conversion follows the three rules above. Edit the lists.

Python Test exclusion patterns (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Test sources

Tell the scanner which directories hold tests with sonar.tests, for example sonar.tests=tests,src/test. Test files are analyzed with a test-specific set of rules, are not included in the "lines of code" measure and are not counted for coverage. If you leave tests inside sonar.sources, they are treated as production code and may produce misleading findings or low coverage numbers.

When tests live next to the source (Go, JavaScript), use naming patterns instead:

PROPERTIES
sonar.sources=src
sonar.tests=src
sonar.test.inclusions=**/*.test.ts,**/*.spec.ts
sonar.exclusions=**/*.test.ts,**/*.spec.ts

Here the same directory is both sources and tests; the inclusion and exclusion patterns decide which file goes where. A file must not be in both lists at once.

Python Split a tree into production and test files (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Excluding by rule and file

Sometimes one rule is wrong for one area, but other rules are valuable there. Use multi-criteria ignore in the properties file:

PROPERTIES
sonar.issue.ignore.multicriteria=e1,e2
sonar.issue.ignore.multicriteria.e1.ruleKey=python:S1192
sonar.issue.ignore.multicriteria.e1.resourceKey=**/tests/**
sonar.issue.ignore.multicriteria.e2.ruleKey=*
sonar.issue.ignore.multicriteria.e2.resourceKey=**/legacy/**

The first ignores one rule in all test folders; the second ignores every rule in a legacy folder (a large hammer: prefer fixing). You can also set these in the UI under Project Settings > General Settings > Analysis Scope, which stores them on the server so they apply to every scanner.

Guidelines

  • Exclude generated and vendored code. You cannot fix it, and its issues drown real ones.
  • Do not exclude to pass the gate. Excluding the worst files raises scores and hides debt. Review exclusion changes in code review.
  • Prefer narrow patterns. src/generated/** beats **/*gen*, which can accidentally match real code.
  • Check the result. Run with -X and look at the list of indexed files, or compare the "lines of code" measure before and after.
  • Keep the list in version control. A properties file reviewed with your code beats UI settings nobody can see in history.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does sonar.exclusions do?

    Excluded files are not analyzed: no issues, no size, no coverage from them.
  2. 2. Which property ignores files only for coverage?

    sonar.coverage.exclusions keeps files analyzed for issues but ignores them in coverage.
  3. 3. What does ** match in an Ant pattern?

    ** crosses directory boundaries and may match zero or more path segments.
  4. 4. Why is excluding failing files to pass the gate a bad idea?

    Exclusions that exist to improve scores hide real problems and erode trust in the gate.

Summary

  • sonar.exclusions removes files from analysis; coverage and duplication have their own exclusion properties.
  • Ant-style patterns: *, ?, **, relative to the project base directory.
  • Put tests in sonar.tests, or split a shared tree with inclusion and exclusion patterns.
  • Use multi-criteria ignores for rule-and-path combinations, and keep exclusions narrow and reviewed.

Next: Pull Request Analysis and IDEs