Search code examples
javajenkinssonarqubefindbugs

Turning Sonar off for certain code


Is it possible to turn off sonar (www.sonarsource.org) measurements for specific blocks of code, which one doesn't want to be measured?

An example is the "Preserve Stack Trace" warning which Findbugs outputs. When leaving the server, I might well want to only pass the message back to the client, not including the actual exception which I just caught, if that exception is unknown to the client (because the client doesn't have the JAR in which that exception was contained for example).


Solution

  • This is a FAQ. You can put //NOSONAR at the end of the line triggering the warning.

    //NOSONAR

    For most languages, SonarQube supports the use of the generic mechanism: //NOSONAR at the end of the line of the issue. This will suppress all issues - now and in the future - that might be raised on the line.

    I prefer using the FindBugs mechanism though, which consists in adding the @SuppressFBWarnings annotation:

    @edu.umd.cs.findbugs.annotations.SuppressFBWarnings(
        value = "NAME_OF_THE_FINDBUGS_RULE_TO_IGNORE",
        justification = "Why you choose to ignore it")