0
votes

I want to use a custom rule in PMD. I installed the PMD-plugin in jenkins, I also created a jar with :

  1. A ruleset file
  2. The java code for the custom rule (the custom rule extends from AbstractJavaRule).

I added this jar in the lib directory of the PMD plugin (jenkins/plugins/pmd/WEB-INF/lib).

I also added this in the pom.xml of a project (to test PMD):

        <reporting>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                     <artifactId>maven-pmd-plugin</artifactId>
                    <version>2.7.1</version>
                    <configuration>
                        <linkXRef>false</linkXRef>
                        <targetJdk>1.6</targetJdk>
                        <rulesets>
                                 <ruleset>/rulesets/basic.xml</ruleset>
                        </rulesets>                  
                    </configuration>
                </plugin>
            </plugins>
    </reporting> 

How can I tell to the jenkins PMD-plugin to use my new JAVA rule ?

1

1 Answers

2
votes

I think, the first step is, to get the custom PMD rule working without Jenkins. Jenkins will trigger the maven build, which will trigger PMD to create the report file pmd.xml and then the Jenkins PMD plugin will parse this pmd.xml file.

  1. Maven - you've added the maven-pmd-plugin in the reporting section. This means, you'll need to generate the site, in order to get the PMD reports (run mvn site. It is also possible to run PMD during the build, e.g. run the pmd:pmd in phase verify. See also PMD Mojo Documentation.

  2. In order to have your custom rule and custom ruleset available, you'll need to add your jar as an additional dependency to the maven-pmd-plugin section, e.g.

    ...
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-pmd-plugin</artifactId>
        <version>2.7.1</version>
        ...
        <dependencies>
            <dependency>
                <groupId>com.your.custom.rule</groupId>
                <artifactId>custom-rule</artifactId>
                <version>1.0.0-SNAPSHOT</version>
            </dependency>
        </dependencies>
    </plugin>
    
  3. Verify, that a mvn site will generate the pmd report, usually under target/pmd.xml.

  4. It might be, that the Jenkins PMD plugin now just shows your rule, but without the documentation like code examples and description. This is implemented in the PMDMessages class and uses "RegisteredRuleSets" feature of PMD, which is not good documented, but the sourcecode is here (for PMD 4.3).

  5. Add a properties file named rulesets.properties in the directory rulesets in your jar. You would place this file in the source tree usually under src/main/resources/rulesets/rulesets.properties. The file needs to have the following content

    rulesets.filenames=rulesets/custom-ruleset-1.xml,rulesets/custom-ruleset-2.xml
    

It contains a comma-separated list of ruleset files. If you add this file in your jar, then the Jenkins PMD Report should contain the info you want.

Please note, that there is already a newer version of the Maven PMD Plugin available, currently the latest version is 3.5. This version is based on PMD 5 which introduced multiple languages and the path to the rulesets.properties file changes to rulesets/java/rulesets.properties in case of java.