(ant 1.7, pmd 4.2.4)

I’ve recently tried to use PMD – sourcecode inspection tool in an ant powered build. Here is a basic example from a test in the build.xml of a Netbeans project (the pmd jar is located in ant’s home lib directory):

    <target name="pmd" depends="init">
         <!-- see http://pmd.sourceforge.net/ant-task.html -->
        <taskdef name="pmd" classname="net.sourceforge.pmd.ant.PMDTask"/>
        <mkdir dir="${build.test.results.dir}"/>
        <pmd targetjdk="1.6">
            <!-- auxClasspath> 
               <pathelement location="dist/test.jar" /> 
            </auxClasspath -->
            <ruleset>basic</ruleset>
            <!-- ruleset>rulesets/basic.xml</ruleset -->
            <!-- ruleset>rulesets/braces.xml</ruleset>
            <ruleset>rulesets/javabeans.xml</ruleset>
            <ruleset>rulesets/unusedcode.xml</ruleset>
            <ruleset>rulesets/strings.xml</ruleset>
            <ruleset>rulesets/design.xml</ruleset>
            <ruleset>rulesets/coupling.xml</ruleset>
            <ruleset>rulesets/codesize.xml</ruleset>
            <ruleset>rulesets/imports.xml</ruleset>
            <ruleset>rulesets/naming.xml</ruleset -->
            <formatter type="xml" toFile="${build.test.results.dir}/pmd_report.xml" />
            <fileset dir="${src.dir}">
                <include name="**/*.java" />
            </fileset>
        </pmd>
    </target>

More examples you will find at PMD Ant Task.

Running this task from command line ant -v pmd gives a warning

[pmd] Could not find class test.hallo.Main

or running same from Netbeans (using external ant).

Could not find class test.hallo.Main
java.lang.NoClassDefFoundError: org/jaxen/JaxenException
        at net.sourceforge.pmd.rules.XPathRule.getRuleChainVisits(XPathRule.java:87)

After looking around for a while I found this thread Ant could not find class.

Citation from there:

As far as what this will do to the PMD results… these errors come from the Type Resolution code. Only Rules which use the Type resolution capabilities even have a chance to give less than accurate results, and then only in circumstances requiring type information not apparent from the .java file directly. Finally, there only a few Rules in PMD that actively use Type Resolution right now, although I know several folks have written custom Rules using this capability.

Solution?

PMD 5 is not out yet. But I was successful by using the latest snapshot (at time of writing)
pmd-5.0-20081231.082852-77.jar. Also, I had to change the target definition a little:

 <target name="pmd" depends="init">
        <taskdef name="pmd" classname="net.sourceforge.pmd.ant.PMDTask"/>
        <mkdir dir="${build.test.results.dir}"/><!-- wird auch in -pre-test-run aufgerufen -->
        <pmd>
            <auxClasspath> 
               <pathelement location="dist/test.jar" /> 
            </auxClasspath>
<!-- new path for rulesets!! -->
            <ruleset>rulesets/java/basic.xml</ruleset>
            <formatter type="xml" toFile="${build.test.results.dir}/pmd_report.xml" />
            <fileset dir="${src.dir}">
                <include name="**/*.java" />
            </fileset>
        </pmd>
    </target>

Now it’s working.