PMD coulnd't find ruleset

有些话、适合烂在心里 提交于 2019-12-04 16:59:20
David Carboni

PMD seems to be a fiddly beastie to use from Maven. I've just figured this out with version 3.0 of the plugin - there are two solutions:

  • The quick-and-dirty solution: put rulesets in your project:

    • download the PMD jar (http://sourceforge.net/projects/pmd/files/latest/download)
    • extract lib/pmd-x.x.x.jar
    • extract from that PMD jar file the rulesets/<type>/<ruleset>.xml files you want to use
    • place them in a folder under your project - something like ${basedir}/pmd/...
    • reference them as follows:

      <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-pmd-plugin</artifactId>
          <configuration>
              <rulesets>
                  <ruleset>${basedir}/pmd/<ruleset>.xml</ruleset>
              </rulesets>
          </configuration>
      </plugin>
      

      The advantage is this is easy, the disadvantage is if you update the PMD version in future you'll need to remember to update these files.

  • The nice solution: reference rulesets in pmd-x.x.x.jar.

    • create a custom ruleset such as: ${basedir}/pmd/custom.xml (see http://pmd.sourceforge.net/pmd-5.0.2/howtomakearuleset.html)
    • reference the PMD rulesets in the following way: <rule ref="rulesets/java/imports.xml"/>
    • NB: the path is the path inside pmd-x.x.x.jar (see quick-and-dirty above) with no leading slash
    • reference your custom ruleset as follows:

      <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-pmd-plugin</artifactId>
          <configuration>
              <rulesets>
                  <ruleset>${basedir}/pmd/custom.xml</ruleset>
              </rulesets>
          </configuration>
      </plugin>
      

      The advantage is this will always reference the current PMD rulesets from the PMD jar, the disadvantage is it's a bit fiddly to get right.

To experiment with this until it was working (maven-pmd-plugin version 3.0) I kept running mvn pmd:pmd (<linkXref>false</linkXref> in pom.xml) and tweaked the paths until I stopped getting errors.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!