在 PMD 中导入规则

Importing rules in PMD

我们正在对我们公司的静态分析代码进行实施,并且正在尝试 PMD,我已经阅读了关于 PMD 的文档,但发现它不清楚。

我按照文档中的描述将以下内容添加到我的 pom.xml 以下插件中。

<reporting>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-pmd-plugin</artifactId>
                <version>3.7</version>
                <configuration>
                    <rulesets>
                        <ruleset>rulesets/java/braces.xml</ruleset>
                        <ruleset>rulesets/java/naming.xml</ruleset>
                    </rulesets>
                </configuration>
            </plugin>
        </plugins>
</reporting>

我不明白的是我在哪里知道规则集的位置以及如何使用我想使用的规则集创建自己的 XML 文件?

...the locations of the rules sets...

  • 您从 pom.xml 中摘录的内容表明您使用的是 Apache 的 maven-pmd-plugin 3.7 版。
  • 因此,默认使用的规则集将在本地存储库中的 Apache jar 文件 maven-pmd-plugin-3.7.jar 中指定。
  • 默认使用的唯一规则集名为 Maven Ruleset,并在文件 {Repository location}/maven-pmd-plugin- 中的 jar 中定义3.7/rulesets/maven.xml.
  • 这是该文件的相关内容:
<ruleset name="Maven Ruleset"
  xmlns="http://pmd.sf.net/ruleset/1.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd"
  xsi:noNamespaceSchemaLocation="http://pmd.sf.net/ruleset_xml_schema.xsd">
  <!--
    Customized PMD ruleset for Maven, see [0] for more information
    [0] https://pmd.github.io/latest/customizing/howtomakearuleset.html
  -->
  <description>
    This ruleset checks the code for discouraged programming constructs.
  </description>
  <rule ref="rulesets/java/basic.xml"/>
  <rule ref="rulesets/java/empty.xml">
    <exclude name="EmptyCatchBlock"/>
  </rule>
  <rule ref="rulesets/java/empty.xml/EmptyCatchBlock">
    <properties>
      <property name="allowCommentedBlocks" value="true"/>
    </properties>
  </rule>
  <rule ref="rulesets/java/unnecessary.xml"/>
  <rule ref="rulesets/java/unusedcode.xml"/>
  <rule ref="rulesets/java/imports.xml"/>
</ruleset>

...how do I create my own xml file with the rules sets I want to use?

The plugin documentation by Apache describes that,还详细说明了如何在项目之间共享一组通用规则集。