如何禁用特定文件的Sonar规则?

20 sonarqube

我有一个我正在研究的项目,有些文件违反了一些规则,但这些方法并不是真正的问题,因此会分散噪音.但是,我不想在全球范围内禁用这些规则,我宁愿不必逐个将它们标记为误报.

有没有办法禁用特定文件的声纳规则,如果是,如何?

Jer*_*ong 11

您可以在sonar-project.properties下设置特定文件和规则,内容如下:

# 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
Run Code Online (Sandbox Code Playgroud)

根据您的具体需要更改ruleKey和resourceKey 。


Mit*_*del 7

从SonarQube 4.0开始,您可以根据规则键和文件路径模式定义问题排除模式.

在以前的版本中,您可以依赖Switch Off Violations插件.

  • @mith 链接已损坏。请让您的答案自成一体。 (4认同)
  • 十分感谢.落在此页面上的未来人员的注释:规则键模式是<repository>:<key> (3认同)

小智 7

使用下面的注释我们可以忽略特定文件中的规则

对于一条规则

@java.lang.SuppressWarnings("squid:S2696")
@Slf4j
@Service
public class PaymentServiceImpl implements PaymentService {....
Run Code Online (Sandbox Code Playgroud)

对于多个规则

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


Mat*_*aun 6

在Mithfindel的回答和dpk的评论的基础上,这消除了警告

System.out和System.err不应该用作记录器

log通过为规则添加忽略模式,从名为(或其任何子包)的包中的所有类squid:S106:

在此输入图像描述

有关所有规则的键列表,请转到您的配置文件,Quality Profiles然后选择Download获取包含所有规则键的.csv文件.

我正在使用SonarQube 4.1.1版.


小智 6

您可以使用SuppressWarnings注释类或方法。

这是一个例子:

在这种情况下,@ java.lang.SuppressWarnings(“ squid:S00111”)squid:S00111是一个Sonar问题ID。您可以从Sonar网站ui中找到此问题ID。