Search code examples
javamavenpmd

How to create custom rule for PMD with Maven?


I am trying to implement a new XPath PMD rule based on the example from the book "Jenkins Continuous Integration Cookbook".

My pom file's relevant section:

<reporting>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jxr-plugin</artifactId>
            <version>2.1</version>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-pmd-plugin</artifactId>
            <configuration>
                <targetJdk>1.6</targetJdk>
                <format>xml</format>
                <rulesets>
                    <ruleset>password_ruleset.xml</ruleset>
                </rulesets>
            </configuration>
        </plugin>
    </plugins>
</reporting>

I have the file 'password_ruleset.xml' sitting in the root of my maven project and it looks as follows:

<?xml version="1.0"?>
<ruleset name="STUPID PASSWORDS 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">
  <description>
  Lets find stupid password examples
  </description>
<rule name="NO_PASSWORD"
  message="If we see a PASSWORD we should flag"
  class="net.sourceforge.pmd.rules.XPathRule">
  <description>
  If we see a PASSWORD we should flag
  </description>
  <properties>
    <property name="xpath">
    <value>
<![CDATA[
//VariableDeclaratorId[@Image='PASSWORD']
]]>

While executing i got the following error:

Failure executing PMD: Couldn't find the class net.sourceforge.pmd.rules.XPathRule

Checking which libraries contains this class i realized it's 'pmd' itself. I tried to add the dependency to the dependencies section without luck.

Where and what should i change to overcome this?

Please see the whole setup in github: https://github.com/dave00/pmdcustomrule


Solution

  • A friend of mine resolved my problem via github: https://github.com/mnyeste/pmdcustomrule/commit/ad2f04e33d2a5a04ef95d059d64a258ebca5b7be

    Summary:

    PMD API change 4.3 -> 5.0 Class net.sourceforge.pmd.rules.XPathRule has been renamed to net.sourceforge.pmd.lang.rule.XPathRule

    Maven PMD plugin version 3.2 is using PMD 5.1.2

    Anyone interested can now pull my example project from github to see this working.