##### Overview

- Download PMD 3.9
- What's new in PMD 3.9
- PMD in the news
- PMD-related products and books
- Best practices
- Future directions
- Similar projects
- Credits
- License
- What does 'PMD' mean?

##### Usage

- Installation
- Command line usage
- Ant task usage
- Maven plugin usage
- IDE plugin usage
- Suppressing warnings
- Finding duplicated code
- JSP support

##### Customizing PMD

- Compiling PMD
- How to write a rule
- Writing XPath rules
- How to make a rule set
- How it works

##### For example

- Run PMD on a Sourceforge project

##### Rule Sets

- Index
- Basic
- Braces
- Code Size
- Clone
- Controversial
- Coupling
- Design
- Finalizers
- Import Statements
- J2EE
- Javabeans
- JUnit Tests
- Logging (Java)
- Logging (Jakarta)
- Migrating
- Naming
- Optimizations
- Strict Exceptions
- Strings
- Sun Security
- Unused Code
- Java Server Pages
- Java Server Faces

##### Project Documentation

- About PMD
- Project Info
- Project Reports
- Development Process

## PMD

### Description

Runs a set of static code analysis rules on some Java source code files and generates a list of problems found.

### Parameters

Attribute | Description | Required
rulesetfiles | A comma delimited list of ruleset files ('rulesets/basic.xml,rulesets/design.xml'). If you write your own ruleset files, you can put them on the classpath and plug them in here. | Yes, unless the ruleset nested element is used
failonerror | Whether or not to fail the build if any errors occur while processing the files | No
failOnRuleViolation | Whether or not to fail the build if PMD finds any problems | No
minimumPriority | The rule priority threshold; rules with lower priority than they will not be used | No
shortFilenames | Places truncated filenames in the report. This can reduce your report file size by 15%-20%. | No
targetjdk | Target JDK 1.3, 1.4, 1.5, 1.6, or jsp. "1.4" is the default. | No
failuresPropertyName | A property name to plug the number of rule violations into when the task finishes | No
encoding | The character set encoding (i.e., UTF-8) to use when reading the source code files | No
excludemarker | The series of characters to use to tell PMD to skip lines - the default is NOPMD. | No

`formatter` nested element - specifies the format of and the files to which the report is written.

Name | Values
--- | ---
format | xml,html,text,csv,papari,summaryhtml
showSuppressed | Whether to show suppressed warnings; "false" is the default.
filename | A filename to which to write the report
linkPrefix | Used for linking to online HTMLized source (like this). See example below.
linePrefix | Used for linking to online HTMLized source (like this). See example below.

`classpath` nested element - useful for specifying custom rule. More details on the `classpath` element are in the Ant documentation here and there's an example below.

`ruleset` nested element - another way to specify rulesets. Here's an example:

    <target name="pmd">
     <taskdef name="pmd" classname="net.sourceforge.pmd.ant.PMDTask"/>
     <pmd shortFilenames="true">
      <ruleset>rulesets/favorites.xml</ruleset>
      <ruleset>basic</ruleset>
      <formatter type="html" toFile="pmd_report.html" linkPrefix="http://pmd.sourceforge.net/xref/"/>
      <fileset dir="/usr/local/j2sdk1.4.1_01/src/">
       <include name="java/lang/*.java"/>
      </fileset>
     </pmd>
    </target>

### Postprocessing the report file with XSLT

Several folks (most recently, Wouter Zelle) have written XSLT scripts which you can use to transform the XML report into nifty HTML. To do this, make sure you use the XML formatter in the PMD task invocation, i.e.:

    <formatter type="xml" toFile="${tempbuild}/$report_pmd.xml"/>

Then, after the end of the PMD task, do this:

    <xslt in="${tempbuild}/$report_pmd.xml" style="${pmdConfig}/wz-pmd-report.xslt" out="${pmdOutput}/$report_pmd.html" />

### Examples

Running one ruleset to produce a HTML report (and printing the report to the console as well)

    <taskdef name="pmd" classname="net.sourceforge.pmd.ant.PMDTask"/>

    <target name="pmd">
        <taskdef name="pmd" classname="net.sourceforge.pmd.ant.PMDTask"/>
        <pmd rulesetfiles="imports">
            <formatter type="html" toFile="pmd_report.html" toConsole="true"/>
            <fileset dir="C:\j2sdk1.4.1_01\src\java\lang\">
                <include name="**/*.java"/>
            </fileset>
        </pmd>
    </target>

Running multiple rulesets to produce an XML report

    <taskdef name="pmd" classname="net.sourceforge.pmd.ant.PMDTask"/>

    <target name="pmd">
        <taskdef name="pmd" classname="net.sourceforge.pmd.ant.PMDTask"/>
        <pmd rulesetfiles="imports.xml,unusedcode">
            <formatter type="xml" toFile="c:\pmd_report.xml"/>
            <fileset dir="C:\j2sdk1.4.1_01\src\java\lang\">
                <include name="**/*.java"/>
            </fileset>
        </pmd>
    </target>

Using a custom renderer

    <taskdef name="pmd" classname="net.sourceforge.pmd.ant.PMDTask"/>

    <target name="pmd">
        <pmd rulesetfiles="rulesets/favorites.xml">
            <formatter type="com.mycompany.MyRenderer" toFile="foo.html"/>
            <fileset dir="/path/to/java/src">
                <include name="**/*.java"/>
            </fileset>
        </pmd>
    </target>

Using a classpath reference in the taskdef

    <path id="pmd.classpath">
        <pathelement location="${build}"/>
        <fileset dir="/path/to/my/pmd/lib/">
            <include name="*.jar"/>
        </fileset>
    </path>

    <taskdef name="pmd" classname="net.sourceforge.pmd.ant.PMDTask" classpathref="pmd.classpath"/>

    <target name="pmd">
          <pmd rulesetfiles="rulesets/favorites.xml">
              <formatter type="net.sourceforge.pmd.renderers.HTMLRenderer" toFile="foo.html"/>
              <fileset dir="/path/to/java/src">
                  <include name="**/*.java"/>
              </fileset>
          </pmd>
      </target>

Getting verbose output

    [tom@hal bin]$ ant -v pmd
    Apache Ant version 1.6.2 compiled on July 16 2004
    Buildfile: build.xml
    Detected Java version: 1.4 in: /usr/local/j2sdk1.4.2_03/jre
    Detected OS: Linux
    parsing buildfile build.xml with URI = file:/home/tom/data/pmd/pmd/bin/build.xml
    Project base dir set to: /home/tom/data/pmd/pmd
    Build sequence for target `pmd' is [pmd]
    Complete build sequence is [pmd, copy, cppjavacc, cpd, delete,
     compile, clean, jar, dist, cpdjnlp, jjtree, javadoc, test, tomserver]

    pmd:
          [pmd] Using the normal ClassLoader
          [pmd] Using these rulesets: rulesets/imports.xml
          [pmd] Using rule DontImportJavaLang
          [pmd] Using rule UnusedImports
          [pmd] Using rule ImportFromSamePackage
          [pmd] Using rule DuplicateImports
          [pmd] Processing file /usr/local/java/src/java/lang/ref/Finalizer.java
          [pmd] Processing file /usr/local/java/src/java/lang/ref/FinalReference.java
          [pmd] Processing file /usr/local/java/src/java/lang/ref/PhantomReference.java
          [pmd] Processing file /usr/local/java/src/java/lang/ref/Reference.java
          [pmd] Processing file /usr/local/java/src/java/lang/ref/ReferenceQueue.java
          [pmd] Processing file /usr/local/java/src/java/lang/ref/SoftReference.java
          [pmd] Processing file /usr/local/java/src/java/lang/ref/WeakReference.java
          [pmd] 0 problems found

    BUILD SUCCESSFUL
    Total time: 2 seconds
    [tom@hal bin]$

An HTML report with the "linkPrefix" gizmo

    <target name="pmd">
     <taskdef name="pmd" classname="net.sourceforge.pmd.ant.PMDTask"/>
     <pmd rulesetfiles="basic" shortFilenames="true">
      <formatter type="html" toFile="pmd_report.html" linkPrefix="http://pmd.sourceforge.net/xref/"/>
      <fileset dir="/usr/local/j2sdk1.4.1_01/src/">
       <include name="java/lang/*.java"/>
      </fileset>
     </pmd>
    </target>

An HTML report with the "linePrefix" gizmo

    <target name="pmd">
     <taskdef name="pmd" classname="net.sourceforge.pmd.ant.PMDTask"/>
     <pmd rulesetfiles="basic" shortFilenames="true">
      <formatter type="html" toFile="pmd_report.html" linePrefix=".line"/>
      <fileset dir="/usr/local/j2sdk1.4.1_01/src/">
       <include name="java/lang/*.java"/>
      </fileset>
     </pmd>
    </target>