How can we ignore some SonarQube rules in Java?

The right thing to do is to put something like this on sonar-project.properties file per project:

sonar.issue.ignore.multicriteria=e1,e2
# tab characters should not be used
sonar.issue.ignore.multicriteria.e1.ruleKey=squid:S00105
sonar.issue.ignore.multicriteria.e1.resourceKey=**/*.java
# right curly braces should be on a new line
sonar.issue.ignore.multicriteria.e2.ruleKey=squid:RightCurlyBraceStartLineCheck
sonar.issue.ignore.multicriteria.e2.resourceKey=**/*.java

As noted in the comments, all you have to do is remove the rules from your profile or edit them to lower their priority. You need the Global Administer Quality Profiles permission to do that. Once you're logged in with that permission, go to the Rules interface, search for a rule you want to deactivate, select the rule, click on it, and Deactivate it from the relevant profile.


If you have the id of the rule ypu want to ignore, then you can add the SuppressWarnings for that

Example:

@SuppressWarnings("squid:S0016")

I dont like this too much and use to add the comment //NOSONAR that tells SonarQube to ignore all errors for a specific line.

Example2:

If I do this:

System.setErr(System.out);
ConsoleHandler h = new ConsoleHandler(); 
System.setErr(err);

my sonar complains asking me to use logger instead of system.out...

therefore I can silent the warning doing:

System.setErr(System.out);  //NOSONAR 
ConsoleHandler h = new ConsoleHandler(); 
System.setErr(err);