Search code examples
testingrulespmd

Working with PMD rules


How to work with PMD rules. Where to place that xml file and run against my application?


Solution

  • If you are using Eclipse look at oneofthelions answer.

    Here an Ant task to generate a PMD report (you will need to substitute the properties according to your project structure):

    <target name="create-pmd-report" description="Generates a PMD report">
    
        <fail unless="target.reports.pmd" message="target.reports.pmd not set"/>
    
        <delete dir="${target.reports.pmd}"/>
        <mkdir  dir="${target.reports.pmd}"/>
    
        <pmd rulesetfiles="src/main/resources/pmd/ruleset.xml">
    
            <formatter type="xml" toFile="${target.reports.pmd}/pmd-report.xml" linkPrefix="${src.main}/"/>
    
            <fileset dir="${src.main}">
                <include name="**/*.java"/>
            </fileset>
    
        </pmd>
    
        <xslt
            in="${target.reports.pmd}/pmd-report.xml"
            style="${pmd.home}/etc/xslt/pmd-report.xslt"
            out="${target.reports.pmd}/pmd-report.html"
        />
    
    </target>