PMD coulnd't find ruleset

2019-04-14 03:27发布

问题:

I'm on creating a maven based java project, which contains the PMD maven plugin. I use my own rule set XML and it works like a charm, except two rule sets: the emptycode and the unnecessary: when I run the build, maven says: "can't find resource". The role definitions look like:

<role ref="rulesets/emptycode" />

and

<role ref="rulesets/unnecessary" />

In every other cases, this kind of definition works. What I found out is that: there is a rule set with the name "unnecessary" under ecmasrcipt category, so maybe this definition needs some suggestion to use java version. I tried multiple thinks, like set language attribute to the ruleset xml node ("JAVA", based on PMD JavaDoc), and some pre-postfix in ref, but it doesn't work and I found no working solution over the web. Does someone has an idea, what I forgot to set, or what I fail? Thanks for any help!

回答1:

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.