5

I am trying to run PMD from Ant in Eclipse when I build the project.

This is my build.xml file:

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

<target name="check_pmd">
    <pmd rulesetfiles="C:\Users\Nikolay\ProjectName\lib\rulesets\java\basic.xml">
        <formatter type="html" toFile="pmd_report.html" toConsole="true"/>
        <fileset dir="C:\Users\Nikolay\ProjectName\src">
            <include name="**/*.java"/>
        </fileset>
    </pmd>
</target>

It works well for basic.xml, but I want to run for all rulesets in java folder (It has around 20 rulesets) So I have tried:

<pmd rulesetfiles="C:\Users\Nikolay\ProjectName\lib\rulesets\java\*.xml">
<pmd rulesetfiles="C:\Users\Nikolay\ProjectName\lib\rulesets\java\*">

But both of them fail when I try to run. Is there a way to specify folder, not a single file without specifying list of files manually?

For future readers to configure Ant PMD under Eclipse:

  • Download pmd-bin.zip from official website
  • Unpack pmd.jar, jaxen.jar and asm.jar
  • Add jars above to Window - Preferences - Ant - Runtime - Ant Home Entries - Add External JARs
  • Unpack rulesets folder
  • Reference location of ruleset from <pmd rulesetfiles=...>
Nikolay Kuznetsov
  • 9,467
  • 12
  • 55
  • 101

3 Answers3

4

(expanding answer from coolfan for ant task)

The documentation of PMD rulesetfiles says it is comma separated list of files.

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

Ant provides a way to convert fileset into such a format. The task is pathconvert

here is an example from website

<fileset dir="${src.dir}" id="src.files">
      <include name="**/*.java"/>
 </fileset>

 <pathconvert pathsep="," property="javafiles" refid="src.files"/>
Jayan
  • 18,003
  • 15
  • 89
  • 143
3

Maybe the param doesn't support wildcard, as the document suggests.

A quick look over its source code also supports my guess, see RuleSetReferenceId.java, line 194.

So, it takes a property which contains a "list" using , as delimiter, like:

"rule1,rule2,rule3,path-to-rule-file4"

The workaround could be scanning the directory, list all the rule-xml files, and build a property in the comma-delimited format and then pass it to <pmd> task.

Unfortunately, I don't know any ant task which can do this. So you may have to write some code.

I can come up with two ways:

  1. write a ant task; there are many Q&As about this for Java, like this.
  2. write groovy inside a <groovy> task; also many Q&As.

EDIT:

Jayan suggests <pathconvert> task, which should be the right answer.

Community
  • 1
  • 1
Dante WWWW
  • 2,729
  • 1
  • 17
  • 33
1

In the pmd library jar there is an all-java.xml where all the rule sets have been included. Try to use the following:

<pmd rulesetfiles="rulesets/internal/all-java.xml">
Manuel
  • 3,828
  • 6
  • 33
  • 48