Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable Sonar rules for specific files?

Tags:

sonarqube

People also ask

How do I exclude files from sonar coverage?

You can prevent some files from being taken into account for code coverage by unit tests. To do so, go to Project Settings > General Settings > Analysis Scope > Code Coverage and set the Coverage Exclusions property.

How do you exclude rules in SonarQube?

Using @SuppressWarnings In Java, we can exclude Sonar checks using the built-in @SuppressWarnings annotation. This works exactly the same way as suppressing compiler warnings. All we have to do is specify the rule identifier, in this case java:S106.

How do I exclude a folder from code coverage in SonarQube?

Choosing Files To specify which files are are and are not included in an analysis go to Administration > General Settings > Analysis Scope > Files. Use exclusion to analyze everything but the specified files: sonar. exclusions - to exclude source code files.

How do you customize sonar rules?

There are three ways to add coding rules to SonarQube: Writing a SonarQube plugin in Java that uses SonarQube APIs to add new rules. Adding XPath rules directly through the SonarQube web interface. Importing Generic Issue Reports generated by an independently run tool.


Since SonarQube 4.0, you can define issue exclusion patterns based on rule key and file path pattern.

On previous versions, you can rely upon the Switch Off Violations plugin.


You can annotate a class or a method with SuppressWarnings.

Here is an example:

@java.lang.SuppressWarnings("squid:S00111") squid:S00111 in this case is a Sonar issue ID. You can find this issue id from the Sonar web ui.


Building on Mithfindel's answer and dpk's comment, this removes the warning

System.out and System.err should not be used as loggers

from all classes in packages named log (or any subpackage thereof) by adding an ignore pattern for the rule squid:S106:

enter image description here

For a list of the keys to all your rules go to your profile under Quality Profiles and select Download to get a .csv file containing all the rules' keys.

I'm using SonarQube version 4.1.1.


You can set specific files and rules under sonar-project.properties with following contents:

# Ignore Issues
sonar.issue.ignore.multicriteria=e1,e2
# Skip Bold Check
sonar.issue.ignore.multicriteria.e1.ruleKey=Web:BoldCheck
sonar.issue.ignore.multicriteria.e1.resourceKey=**/*.html
# Skip Tag Check
sonar.issue.ignore.multicriteria.e2.ruleKey=Web:S1234
sonar.issue.ignore.multicriteria.e2.resourceKey=**/*.html

Change the ruleKey and resourceKey based on your specific need.


Yes, it is possible.

  • 1.Goto Administration tab->Analysis Scope->Issues

  • 2.There , you will find "Ignore Issues on Multiple Criteria".

  • 3.Provide Rule ID in "Rule Key pattern" textbox [Rule ID can be found by clicking on the particular rule and find it in top right corner]

  • 4.Provide Filepath for which you need to ignore rule in "File Path Pattern" textbox

  • 5.Click on Save Issues settings

  • Image to know where Rule ID will be present in the page
  • Image where Rule key pattern and File Key pattern text boxes are present

Using below annotation we can ignore the rule from the specific files

For one rule

@java.lang.SuppressWarnings("squid:S2696")
@Slf4j
@Service
public class PaymentServiceImpl implements PaymentService {....

For more than one rule

@java.lang.SuppressWarnings({"squid:S2696", "squid:S1172", "squid:CommentedOutCodeLine"})
@Slf4j
@Service
public class PaymentServiceImpl implements PaymentService {...