Apache

Docs Plugins

PMD Plugin 3.28.0 · Other versions

A Maven plugin for the PMD toolkit, that produces a report on both code rule violations and detected copy and paste fragments, as well as being able to fail the build based on these metrics.

Source
Edit this page
Wrong?
Report a problem

Violation Exclusions

It is possible to exclude some sources from PMD/CPD check to prevent failures.

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-pmd-plugin</artifactId>
        <version>3.28.0</version>
        <executions>
          <execution>
            <goals>
              <goal>check</goal>
            </goals>
            <configuration>
              <excludeFromFailureFile>exclude-pmd.properties</excludeFromFailureFile>
            </configuration>
          </execution>
          <execution>
            <goals>
              <goal>cpd-check</goal>
            </goals>
            <configuration>
              <excludeFromFailureFile>exclude-cpd.properties</excludeFromFailureFile>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  ...
</project>

For cpd check, you can exclude classes to not verify. Classes can be specified in two ways:

  1. Comma-separated: org.apache.maven.ClassA and org.apache.maven.ClassB contain duplicated code across classes.
  2. Single class per line: org.apache.maven.ClassC and org.apache.maven.ClassD do not share duplicate code, but have duplicated code internally.

The properties file for cpd must have the following format:

org.apache.maven.ClassA,org.apache.maven.ClassB
org.apache.maven.ClassC
org.apache.maven.ClassD

For pmd check, you can exclude rules per classes. The properties file must have the following format:

org.apache.maven.ClassA=UnusedPrivateField,EmptyCatchBlock
org.apache.maven.ClassB=UnusedPrivateField,UnusedFormalParameter,UnusedPrivateMethod

You can only exclude (checks for) classes through the exclusion files. If you want more flexibility and exclude classes with a certain name, or whole packages, for example because of generated code, you have to do this in the <configuration> section of the plugin, using the <excludes> or <excludeRoots> elements. To avoid confusion, don't write those to the plugin's configuration as a <build> and <reporting> plugin. Instead, configure the plugin in the <pluginManagement> section of your POM, so executions of the build and site lifecycle pick it up.

<project>
  ...
  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-pmd-plugin</artifactId>
          <version>3.28.0</version>
          <configuration>
            <excludes>
              <exclude>**/*Bean.java</exclude>
              <exclude>**/generated/*.java</exclude>
            </excludes>
            <excludeRoots>
              <excludeRoot>target/generated-sources/stubs</excludeRoot>
            </excludeRoots>
          </configuration>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
  ...
</project>