Amazon holiday deals See deals →ad

Mets Minor League Blog

Music theory, instruments and musicians

Mets Minor League Blog

Music theory, instruments and musicians

Exclude Specific Classes from SonarQube Code Coverage

Ensuring accurate code coverage reports is vital for maintaining code quality. Sometimes, you may want to exclude certain classes—like generated code, legacy classes, or third-party integrations—from SonarQube’s coverage analysis to focus on the parts of your codebase that truly matter. This guide provides a clear, step-by-step process to exclude specific classes from SonarQube coverage, helping you generate more meaningful insights and streamline your quality gate assessments.

What You’ll Need

  • Access to your SonarQube server with permissions to modify project settings
  • Knowledge of your project’s directory structure and class naming conventions
  • A configuration file for your build tool (like Maven, Gradle, or SonarScanner)
  • Optional: Knowledge of your code’s package structure to accurately specify exclusions

Step-by-Step

  1. Identify Classes to Exclude: Determine which classes or packages you want to exclude from coverage. Common candidates include generated classes, legacy code, or third-party integrations. Use your IDE or file explorer to locate these classes and note their package or class names.
  2. Choose Your Exclusion Method: SonarQube supports several ways to exclude classes:
    • Using the ‘sonar.exclusions’ property in your configuration
    • Using the ‘sonar.coverage.exclusions’ property for coverage-specific exclusions
    • Configuring exclusions via the UI (less common for coverage exclusions)
  3. Modify Your Build Configuration: Depending on your build tool, add the appropriate exclusion property.
    • Maven: Edit your ‘pom.xml’ to include the ‘sonar.coverage.exclusions’ property inside the ‘properties’ section or in the command line.
    • Gradle: Use the ‘sonarqube’ plugin configuration to specify exclusions.
    • SonarScanner CLI: Add the property as a command-line argument.
  4. Specify the Classes or Packages to Exclude: Use patterns matching your class names or package paths. For example:
    • com/example/generated/** — excludes all classes in the ‘generated’ package
    • com/example/legacy/MyLegacyClass.java — excludes a specific class
    • /test/ — excludes all classes in test directories
  5. Update Your Configuration: Incorporate the patterns into your configuration file or command line.
    sonar.coverage.exclusions=/generated/, com/example/legacy/MyLegacyClass.java
  6. Run Your SonarQube Analysis: Execute your build or scan command as usual. The exclusions will now be applied during analysis.
  7. Verify Exclusions: Check the SonarQube dashboard to confirm that the specified classes are excluded from coverage metrics. You can also review the detailed report to ensure the exclusions are correctly applied.

Common Mistakes

  • Using incorrect path patterns or wildcards, resulting in exclusions not applying
  • Failing to specify the correct property (e.g., ‘sonar.coverage.exclusions’ vs. ‘sonar.exclusions’)
  • Forgetting to rerun the analysis after updating the configuration
  • Excluding too many classes, which could skew coverage metrics
  • Not verifying the exclusions in the SonarQube UI or reports

Tips

  • Use wildcards like ‘**’ to match multiple directories or classes
  • Combine multiple patterns separated by commas for comprehensive exclusions
  • Document your exclusion patterns for future reference and team clarity
  • Test exclusions incrementally—start with a few classes to ensure correct application before broadening
  • Leverage the SonarQube documentation for your specific scanner or build tool for latest syntax and options

Troubleshooting

  • Exclusions not applying: Double-check your pattern syntax, ensure you are editing the correct configuration file, and verify you are rerunning the analysis.
    • Use SonarQube’s ‘Analysis Logs’ to see if exclusions are recognized
    • Ensure there are no conflicting settings overriding your exclusions
  • Coverage still includes excluded classes: Confirm that your exclusion patterns match the actual class paths and names.
    • Use debug logs or verbose output from your scanner to verify pattern matching
    • Check for typos or incorrect directory separators
  • Exclusions work in local analysis but not in CI/CD pipeline: Make sure your CI environment uses the same configuration files and environment variables.
  • Coverage metrics seem inconsistent: Clear your previous analysis cache and rerun the scan to ensure new settings are applied.

Frequently Asked Questions

1. Can I exclude classes based on annotations?

SonarQube’s exclusion settings typically match class paths or names; excluding based on annotations requires custom configuration or filtering outside of SonarQube, such as in your build tool or code analysis plugin.

2. Is it possible to exclude only certain types of coverage metrics?

No, exclusions generally apply to entire classes or packages. To exclude specific metrics, you may need custom rules or filter reports after analysis.

3. How do I exclude generated code from coverage?

Identify generated classes or packages and add patterns like /generated/ to your ‘sonar.coverage.exclusions’ property. Many tools generate classes in predictable locations, simplifying this process.

4. Will excluding classes affect overall coverage percentage?

Yes, excluding classes reduces the denominator in coverage calculations, potentially increasing or decreasing the overall percentage. Use exclusions judiciously to reflect meaningful coverage.

5. How often should I review my exclusion patterns?

Regularly review exclusions during major refactoring or when adding new modules to ensure they remain relevant and accurate.

6. Can I exclude classes via SonarQube UI?

Exclusions for coverage are primarily configured through properties or configuration files. The UI offers some exclusion options but is less flexible for detailed coverage exclusions.

By following these steps and tips, you can effectively tailor your SonarQube coverage reports to focus on the most critical parts of your codebase, ensuring more accurate metrics and better insights into your project’s health.

Exclude Specific Classes from SonarQube Code Coverage
Scroll to top