How do you exclude files from SonarQube analysis?

IntermediateConceptSonarQube
Answer

Short answer

Use sonar.exclusions to skip files entirely (generated code, vendored libraries, build output), sonar.test.inclusions to mark test files, sonar.coverage.exclusions to leave files out of the coverage calculation, and sonar.cpd.exclusions to ignore them for duplication. Configure them in sonar-project.properties or the project settings.

Example

sonar.sources=src
sonar.tests=src
sonar.test.inclusions=**/*.test.ts,**/*.spec.ts
sonar.exclusions=**/node_modules/**,**/dist/**,**/generated/**,**/*.min.js
sonar.coverage.exclusions=**/config/**,**/main.ts,**/*.module.ts
sonar.cpd.exclusions=**/migrations/**

Be careful

Excluding files to make the gate pass hides real problems. Exclude only generated, third-party or genuinely untestable code, and review exclusion changes in PRs.

What to say in the interview

"sonar.exclusions for generated and vendored code, coverage exclusions for bootstrap or config classes, CPD exclusions for things like migrations. Exclusions are reviewed so they're never used just to turn the gate green."

All Security Tools questions Roadmap: SonarQube — code quality & SAST

More on SonarQube