5

I'm attempting to exclude a certain rule from pmd using the maven-pmd-plugin (in a multi-module maven project).

Approach:

Using the excludeFromFailureFile http://maven.apache.org/plugins/maven-pmd-plugin/examples/violation-exclusions.html

Ideally, I want to exclude this rule for the entire product (based on a parent package), however,to being with I tested for a particular class - even that is not working.

Environment

Java 7, Maven 3.0.3

<plugin>
       <groupId>org.apache.maven.plugins</groupId>
       <artifactId>maven-pmd-plugin</artifactId>
       <version>3.1</version>
       <executions>
         <execution>
            <goals>
              <goal>check</goal>
            </goals>
            <configuration>
              <excludeFromFailureFile>exclude-pmd.properties</excludeFromFailureFile>
            </configuration>
          </execution>
          <execution>
             <goals>
                 <goal>cpd-check</goal>
             </goals>
          <!-- Added explicit execution Id to avoid the below problem -->
      <!-- 'build.pluginManagement.plugins.plugin[org.apache.maven.plugins:maven-pmd-plugin].executions.execution.id' must be unique but found duplicate execution with id default @ line 1423, column 36 -->
            <id>cpd-check</id>
          </execution>
   </executions>

Contents of exclude-pmd.properties

mycompany.project.classA=UselessParentheses
Jaga
  • 393
  • 2
  • 5
  • 15

1 Answers1

4

The simplest way to exclude rules, is providing your own ruleset file. You can see where to find the default ruleset file in the answer to this question. If you are using Sonar, you can retrieve the file using the permalink.

Copy the file to your parent module, and then you can customize it, deleting the rules you want to exclude, and use the following configuration:

in the parent pom.xml:

...
<properties>
    <main.basedir>${project.basedir}</main.basedir> 
    <!-- Some child module could set this to true to skip PMD check -->
    <skip.pmd.check>false</skip.pmd.check>
</properties>
...
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-pmd-plugin</artifactId>
    <!-- This is the version I'm using -->
    <version>2.7.1</version>
    <configuration>
        <rulesets>
            <ruleset>${main.basedir}/path/to/pmd-rules.xml</ruleset>
        </rulesets>
        <skip>${skip.pmd.check}</skip>
    </configuration>
    <executions>
        <execution>
            <id>pmd-config</id>
            <goals>
                <goal>check</goal>
            </goals>
        </execution>
    </executions>
</plugin>

in child modules pom.xml:

<properties>
    <main.basedir>${project.parent.basedir}</main.basedir>
</properties>

in some module you want to skip (i.e contains generated code):

<properties>
    <main.basedir>${project.parent.basedir}</main.basedir>
    <skip.pmd.check>true</skip.pmd.check>
</properties>

Hope this help!

Community
  • 1
  • 1
Pablo Lascano
  • 662
  • 6
  • 14
  • 1
    Appreciate your detailed answer with helpful comments, Thanks! Is there a chance that new rules could be added to the default pmd set? In which case I would have to start comparing and maintaining the custom pmd file? Also, i'm thinking it would be intuitive for the pmd community to support exclude rules. – Jaga Mar 31 '14 at 16:28
  • You can, in fact, exclude rules from a ruleset ref. Take a look into "Excluding rules from a ruleset" section [here](http://pmd.sourceforge.net/pmd-5.1.0/howtomakearuleset.html). So, instead of deleting the rules from the "default" file, you can create another rulset, ref the default file, and exclude the rules. About worrying for the custom pmd file, I think your only worry is some for some major change in PMD that breaks compatibility with the rulset file syntax. – Pablo Lascano Mar 31 '14 at 19:58