Wed 31 Dec 2008
PMD in Ant Problem – “Could not find class”
Posted by admin under Java , ant , build management , netbeans[2] Comments
(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.
January 30th, 2009 at 11:56 am
Hi,
I’ve found another solution to this problem, that doesn’t require to upgrade PMD version (works on Ant 1.7, PMD 4.2.4): make sure the classpath defined at the taskdef level includes asm-3.1.jar and jaxen-1.1.1.jar. I had them removed because PMD didn’t complain about it… I also did not use the auxclasspath attribute/nested element.
If you get NoSuchMethodError about ClassVisitor, make sure your classpath does not also include and old version of asm.jar (hibernate distribution for example)
March 1st, 2011 at 10:28 pm
Titix,
Thank you for the insight here. I wasted an awful lot of time trying to figure out what to do. I did need the auxclasspath element in addition.