PMD 插件附带一个默认规则集,其中启用了各种规则。规则集以/rulesets/java/maven-pmd-plugin-default.xml的形式提供。有关使用默认规则集启用的规则,请参见下文。
PMD 代码分析工具中还有更多可用和捆绑的规则:
要使用其他规则集,您可以在插件中进行配置,如下所示。规则集可能驻留在类路径、文件系统或 URL 中。对于与 PMD 工具捆绑的规则集,您无需指定文件的绝对路径。它将由插件解决。但如果规则集是自定义规则集,则需要指定其绝对路径。
如果您想创建自己的自定义规则集,请参阅制作规则集。
注意:从 PMD 5.0 和 Maven PMD Plugin 3.0 开始,Java 语言捆绑规则集的路径从/rulesets/xyz.xml更改为/rulesets/java/xyz.xml。因此,当升级到 Maven PMD 插件 3.0 时,您可能需要更改插件配置。
注意:从 PMD 6.0.0 和 Maven PMD Plugin 3.9.0 开始,规则被重新组织成类别,例如/category/java/bestpractices.xml。因此,当升级到 Maven PMD 插件 3.9.0 时,您应该检查您的插件配置和/或自定义规则集。
<project> ... <reporting> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-pmd-plugin</artifactId> <version>3.16.0</version> <configuration> <rulesets> <!-- A rule set, that comes bundled with PMD --> <ruleset>/category/java/bestpractices.xml</ruleset> <!-- Custom local file system rule set --> <ruleset>d:\rulesets\strings.xml</ruleset> <!-- Custom remote rule set accessed via a URL --> <ruleset>http://localhost/design.xml</ruleset> </rulesets> </configuration> </plugin> </plugins> </reporting> ... </project>
这是规则集/rulesets/java/maven-pmd-plugin-default.xml的摘录。它仅包含 Java 规则。如果您使用不同的语言,则需要指定自己的自定义规则集。
当前版本的规则集可以在版本控制中找到:https ://gitbox.apache.org/repos/asf?p=maven-pmd-plugin.git;a=blob_plain;f=src/main/resources/rulesets /java/maven-pmd-plugin-default.xml;hb=HEAD
<rule ref="category/java/bestpractices.xml/AvoidUsingHardCodedIP" /> <rule ref="category/java/bestpractices.xml/CheckResultSet" /> <rule ref="category/java/bestpractices.xml/PrimitiveWrapperInstantiation" /> <rule ref="category/java/bestpractices.xml/UnusedFormalParameter" /> <rule ref="category/java/bestpractices.xml/UnusedLocalVariable" /> <rule ref="category/java/bestpractices.xml/UnusedPrivateField" /> <rule ref="category/java/bestpractices.xml/UnusedPrivateMethod" /> <rule ref="category/java/codestyle.xml/ExtendsObject" /> <rule ref="category/java/codestyle.xml/ForLoopShouldBeWhileLoop" /> <rule ref="category/java/codestyle.xml/TooManyStaticImports" /> <rule ref="category/java/codestyle.xml/UnnecessaryFullyQualifiedName" /> <rule ref="category/java/codestyle.xml/UnnecessaryImport" /> <rule ref="category/java/codestyle.xml/UnnecessaryModifier" /> <rule ref="category/java/codestyle.xml/UnnecessaryReturn" /> <rule ref="category/java/codestyle.xml/UselessParentheses" /> <rule ref="category/java/codestyle.xml/UselessQualifiedThis" /> <rule ref="category/java/design.xml/CollapsibleIfStatements" /> <rule ref="category/java/design.xml/SimplifiedTernary" /> <rule ref="category/java/design.xml/UselessOverridingMethod" /> <rule ref="category/java/errorprone.xml/AvoidBranchingStatementAsLastInLoop" /> <rule ref="category/java/errorprone.xml/AvoidDecimalLiteralsInBigDecimalConstructor" /> <rule ref="category/java/errorprone.xml/AvoidMultipleUnaryOperators" /> <rule ref="category/java/errorprone.xml/AvoidUsingOctalValues" /> <rule ref="category/java/errorprone.xml/BrokenNullCheck" /> <rule ref="category/java/errorprone.xml/CheckSkipResult" /> <rule ref="category/java/errorprone.xml/ClassCastExceptionWithToArray" /> <rule ref="category/java/errorprone.xml/DontUseFloatTypeForLoopIndices" /> <rule ref="category/java/errorprone.xml/EmptyCatchBlock" /> <rule ref="category/java/errorprone.xml/EmptyFinallyBlock" /> <rule ref="category/java/errorprone.xml/EmptyIfStmt" /> <rule ref="category/java/errorprone.xml/EmptyInitializer" /> <rule ref="category/java/errorprone.xml/EmptyStatementBlock" /> <rule ref="category/java/errorprone.xml/EmptyStatementNotInLoop" /> <rule ref="category/java/errorprone.xml/EmptySwitchStatements" /> <rule ref="category/java/errorprone.xml/EmptySynchronizedBlock" /> <rule ref="category/java/errorprone.xml/EmptyTryBlock" /> <rule ref="category/java/errorprone.xml/EmptyWhileStmt" /> <rule ref="category/java/errorprone.xml/JumbledIncrementer" /> <rule ref="category/java/errorprone.xml/MisplacedNullCheck" /> <rule ref="category/java/errorprone.xml/OverrideBothEqualsAndHashcode" /> <rule ref="category/java/errorprone.xml/ReturnFromFinallyBlock" /> <rule ref="category/java/errorprone.xml/UnconditionalIfStatement" /> <rule ref="category/java/errorprone.xml/UnnecessaryConversionTemporary" /> <rule ref="category/java/errorprone.xml/UnusedNullCheckInEquals" /> <rule ref="category/java/errorprone.xml/UselessOperationOnImmutable" /> <rule ref="category/java/multithreading.xml/AvoidThreadGroup" /> <rule ref="category/java/multithreading.xml/DontCallThreadRun" /> <rule ref="category/java/multithreading.xml/DoubleCheckedLocking" /> <rule ref="category/java/performance.xml/BigIntegerInstantiation" />