java PMD/CPD:使用注释忽略代码位
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5767747/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PMD/CPD: Ignore bits of code using comments
提问by digiarnie
Is there a way to tell PMD to ignore checking parts of code for duplication?
有没有办法告诉 PMD 忽略检查部分代码的重复?
For example, can I do something like this:
例如,我可以做这样的事情:
// CPD-Ignore-On
...
// CPD-Ignore-Off
Currently I have PMD set up like this using Maven, but don't see any arguments that would like me do what I want unless I am missing something.
目前我已经使用 Maven 像这样设置了 PMD,但是除非我遗漏了什么,否则看不到任何让我做我想做的事情的论点。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>2.5</version>
<configuration>
<minimumTokens>40</minimumTokens>
<targetJdk>1.5</targetJdk>
<ignoreIdentifiers>true</ignoreIdentifiers>
<ignoreLiterals>true</ignoreLiterals>
</configuration>
</plugin>
回答by Avik
After digging around enough, I finally came across it.
经过足够的挖掘,我终于遇到了它。
By adding the annotations @SuppressWarnings("CPD-START")
and @SuppressWarnings("CPD-END")
all code within will be ignored by CPD - thus you can avoid false positives.
通过添加注释@SuppressWarnings("CPD-START")
,@SuppressWarnings("CPD-END")
CPD 将忽略其中的所有代码 - 因此您可以避免误报。
Source - http://pmd.sourceforge.net/pmd-5.0.5/cpd-usage.html.
回答by kamuflage661
I found it possible only to disable a whole class check in maven-pmd-plugin configuration in project pom. It is performed by adding <excludes>
tag. If you would like to do it, your conf should be like this:
我发现只能在项目 pom 中的 maven-pmd-plugin 配置中禁用整个类检查。它是通过添加<excludes>
标签来执行的。如果你想这样做,你的 conf 应该是这样的:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>2.5</version>
<configuration>
<minimumTokens>40</minimumTokens>
<targetJdk>1.5</targetJdk>
<ignoreIdentifiers>true</ignoreIdentifiers>
<ignoreLiterals>true</ignoreLiterals>
<excludes>
<exclude>**/YourClassName.java</exclude>
........
<exclude>....</exclude>
</excludes>
</configuration>
</plugin>
回答by Johnco
I know this is a 8 years old question, but for completeness, CPD does support this since PMD 5.6.0 (April 2017).
我知道这是一个 8 年前的问题,但为了完整起见,自 PMD 5.6.0(2017 年 4 月)以来,CPD 确实支持这一点。
The complete (current) docs for comment-based suppression are available at https://pmd.github.io/pmd-6.13.0/pmd_userdocs_cpd.html#suppression
基于评论的抑制的完整(当前)文档可在https://pmd.github.io/pmd-6.13.0/pmd_userdocs_cpd.html#suppression 获得
It's worth noting, if a file has a // CPD-OFF
comment, but no matching // CPD-ON
, everything will be ignored until the end of file.
值得注意的是,如果文件有// CPD-OFF
注释,但没有匹配// CPD-ON
,则所有内容都将被忽略,直到文件结束。