Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PMD ruleset file

Tags:

java

maven

pmd

People also ask

What is PMD ruleset Java?

A ruleset is an XML configuration file, which describes a collection of rules to be executed in a PMD run. PMD includes built-in rulesets to run quick analyses with a default configuration, but users are encouraged to make their own rulesets from the start, because they allow for so much configurability.

What is PMD XML?

Simply put, PMD is a source code analyzer to find common programming flaws like unused variables, empty catch blocks, unnecessary object creation, and so forth. It supports Java, JavaScript, Salesforce.com Apex, PLSQL, Apache Velocity, XML, XSL.


The standard ruleset file is *.xml inside pmd-bin-x.x.x.zip/.../lib/pmd-x.x.x.jar/rulesets/, refer to http://pmd.sourceforge.net/rules/index.html.

The default ruleset file of PMD Eclipse Plugin is inside pmd___.jar in your {IDE}/plugins/..., but you should not make any changes in that file. Add/edit the rules in Eclipse Preferences, any changes will take precedence over the default ruleset.


After messing with Ant and PMD for a good long while, this is the complete solution I have come up with. Modify to your own taste.


This sets the initial directories I use.

<property name="doc" location="doc" />               <!-- Root for all documentation: -->
<property name="pmddoc" location="${doc}/pmddoc" />  <!-- PMD results -->

This is my task definition, which points to the latest version of PMD at this time where I have it stored. It includes the PMD Jar itself (where all the rules are stored) and all PMD's dependencies as well.

<taskdef name="pmd" classname="net.sourceforge.pmd.ant.PMDTask">
    <classpath>
        <fileset dir="C:\development\pmd-bin-5.0-alpha">
            <include name="lib/*.jar"/>    <!-- also includes pmd's file, which has all the rulesets I need. -->
        </fileset>
    </classpath>
</taskdef>

In initialization, I create the documentation folder if needed:

<target name="init">
    <mkdir dir="${pmddoc}" />
</target>

...And finally, I created a target specifically for creating a PMD report in HTML form. Here it is.

<target name="pmd" depends="init">
    <pmd>
        <formatter type="html" toFile="${pmddoc}/pmd_src_report.html" toConsole="true"/>

        <ruleset>rulesets/java/basic.xml</ruleset> <!-- references file in PMD's .jar -->

        <!-- Files PMD will test. -->       
        <fileset dir="${src}">
            <include name="**/*.java"/>     <!-- required to avoid firing off .aj errors. This ruleset doesn't support AspectJ. -->
        </fileset>
    </pmd>
</target>