In Sonar, how to prevent checking some rules in some packages?

If you separate the common part to an own project you can add a new Quality Profile in Sonar (where you deactivated these rules) and assign it to your common project.

Apart from that you can use the

// NOSONAR

comment to supress a warning on a single line (see FAQ).


This is an older question and I think meanwhile there is a better solution:

Adding // NOSONAR to the relevant classes solves the issue somehow. But I think this is problematic since no rules will be applied in this case at all. There is another way to solve this though: Ignore Issues on Multiple Criteria

Example:

I want to ignore all issues against coding rule MagicNumbers in the package com.foobar.domain in all my java files.

In Sonar navigate to your project, go to Configuration\Settings\Issues and in Ignore Issues on Multiple Criteria add:

  • Rule Key Pattern: com.puppycrawl.tools.checkstyle.checks.coding.MagicNumberCheck
  • File Path Pattern: com/foobar/domain/*.java

Sonar ignore issues

This is all also explained very nicely in the linked documentation.

Tags:

Java

Sonarqube