Search code examples
javamavenmaven-2sonarqubepmd

Running Maven PMD locally using Sonar ruleset


I'm attempting to run pmd locally using Maven. In project I run

mvn jxr:jxr pmd:pmd

but receive error :

net.sourceforge.pmd.RuleSetNotFoundException: Can't find resource 'rulesets/basic.xml' for rule 'UnusedNullCheckInEquals'.  Make sure the resource is a valid file or URL and is on the CLASSPATH

Here is a snippet of the rules file which was exported :

<?xml version="1.0" encoding="UTF-8"?>
<ruleset>
  <rule ref="rulesets/basic.xml/UnusedNullCheckInEquals">
    <priority>3</priority>
  </rule>
</ruleset>

I've updated pom file to reference custom ruleset :

          <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-pmd-plugin</artifactId>
        <version>3.2</version>
        <configuration>
            <linkXref>true</linkXref>
            <rulesets>
                <ruleset>c:\\pmd\\export.xml</ruleset>
            </rulesets>

        </configuration>
    </plugin>

To export the ruleset so can run pmd locally using Maven is it required to export this file (basic.xml) & all other xml files which together comprise the "Sonar Way" quality profile ? If so where can these files be accessed on Sonar ?


Solution

  • You can access thru "Quality Profiles > Sonar Way > Permalinks > PMD", although, I think "Sonar Way" pmd rules are empty by default (all rules disabled).

    Also, have in mind that version 3.2 of maven-pmd-plugin looks for rules not in rulesets but in rulesets/java, so you might have to replace that path in the exported file (at least the file that you get with Sonar 4.2).

    Finally, I would recommend not using a hardcoded path to reference the ruleset file, use something like:

    <ruleset>${project.basedir}/rules/pmd-rules.xml</ruleset>
    

    Hope this helps!