【问题标题】:Can't use custom ruleset in maven-pmd-plugin 5.0.2无法在 maven-pmd-plugin 5.0.2 中使用自定义规则集
【发布时间】:2026-01-31 22:15:01
【问题描述】:

我希望 maven-pmd-plugin 包含我指定的规则集并排除一些规则(特别是 UselessParentheses)

就像documentation 中描述的一样,我将以下内容放在所有模块的父级 pmd.xml 中:

  <reporting>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-pmd-plugin</artifactId>
        <version>3.0</version>
        <configuration>
          <rulesets>
            <ruleset>/home/ubuntu/ruleset.xml</ruleset>
          </rulesets>
        </configuration>
      </plugin>
    </plugins>
  </reporting>

并准备了一个这样的自定义规则集:

  <!-- We'll use the entire rulesets -->
  <rule ref="rulesets/java/basic.xml"/>
  <rule ref="rulesets/java/imports.xml"/>
  <rule ref="rulesets/java/codesize.xml"/>
  <rule ref="rulesets/java/design.xml"/>
  <rule ref="rulesets/java/strings.xml"/>
  <rule ref="rulesets/java/unusedcode.xml"/>

  <!-- We want everything from this except some -->
  <rule ref="rulesets/java/unnecessary.xml">
    <exclude name="UselessParentheses"/>
  </rule>

作为主要部分。

尽管如此,当我运行mvn clean jxr:jxr pmd:check 时,我在报告中有“UselessParentheses”。此外,使用-X 运行它会显示

[DEBUG] Preparing ruleset: java-basic
[DEBUG] Before: java-basic After: java-basic.xml
[DEBUG] The resource 'rulesets/java/basic.xml' was found as jar:file:/home/ubuntu/.m2/repository/net/sourceforge/pmd/pmd/5.0.2/pmd-5.0.2.jar!/rulesets/java/basic.xml.
[DEBUG] Preparing ruleset: java-unusedcode
[DEBUG] Before: java-unusedcode After: java-unusedcode.xml
[DEBUG] The resource 'rulesets/java/unusedcode.xml' was found as jar:file:/home/ubuntu/.m2/repository/net/sourceforge/pmd/pmd/5.0.2/pmd-5.0.2.jar!/rulesets/java/unusedcode.xml.
[DEBUG] Preparing ruleset: java-imports
[DEBUG] Before: java-imports After: java-imports.xml
[DEBUG] The resource 'rulesets/java/imports.xml' was found as jar:file:/home/ubuntu/.m2/repository/net/sourceforge/pmd/pmd/5.0.2/pmd-5.0.2.jar!/rulesets/java/imports.xml.

所以看起来 pmd 忽略了我的自定义规则集。

我希望自定义规则集起作用。我做错了什么?

【问题讨论】:

    标签: maven customization maven-plugin pmd


    【解决方案1】:

    您已将 maven-pmd-plugin 配置为 reporting,即

    报告包含专门对应于网站生成阶段的元素。某些 Maven 插件可以生成在报告元素下定义和配置的报告。

    这意味着您应该使用以下命令执行:-

    mvn clean site
    

    如果你想按照你说的执行,请将你的配置复制到builds,例如

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-pmd-plugin</artifactId>
                <version>3.0</version>
                <configuration>
                    <rulesets>
                        <ruleset>/home/ubuntu/ruleset.xml</ruleset>
                    </rulesets>
                </configuration>
            </plugin>
        </plugins>
    </build>
    

    然后当你执行

    mvn clean jxr:jxr pmd:check
    

    结果应该如您所愿。您可以进一步了解 Maven Pom,here

    我希望这会有所帮助。

    问候,

    查理·Ch.

    【讨论】:

    • 谢谢。有用。我只是做了他们在docs 中所说的一切,但没有得到结果。没有一个关于将配置放在“构建”部分中的词。
    • 但是如果您查看maven.apache.org/plugins/maven-pmd-plugin/usage.html,您会看到所有可以放置插件声明的地方...
    • 与许多 maven 插件一样,文档缺乏详细信息。我觉得这很令人困惑,考虑到 maven 应该是这样的。