Apache Maven PMD Plugin

The Apache Maven PMD Plugin allows you to integrate PMD (Project Mess Detector) into your Maven project. PMD is a static code analysis tool that finds common programming flaws like unused variables, empty catch blocks, unnecessary object creation, and more. This guide will walk you through configuring and using the Maven PMD Plugin with a complete example.

Introduction to Maven PMD Plugin

The Maven PMD Plugin helps in detecting and reporting code problems in your source code. It can be configured to run during the build process and generate reports that highlight potential issues.

Latest Version

As of this writing, the latest version of the Maven PMD Plugin is 3.17.0. Using the latest version ensures access to the newest features and bug fixes.

Setting Up the Maven PMD Plugin

To use the Maven PMD Plugin, you need to configure it in your project's pom.xml file. Let's go through the steps to set up a complete Maven project with the PMD Plugin.

Step 1: Create a Maven Project

Run the following command to create a new Maven project:

mvn archetype:generate -DgroupId=com.example -DartifactId=pmd-plugin-demo -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

This command will generate a simple Maven project with the following structure:

pmd-plugin-demo
|-- src
|   |-- main
|   |   |-- java
|   |   |   `-- com
|   |   |       `-- example
|   |   |           `-- App.java
|   `-- test
|       |-- java
|       |   `-- com
|       |       `-- example
|       |           `-- AppTest.java
|-- pom.xml
`-- target

Step 2: Add Maven PMD Plugin Configuration

Navigate to the project directory and open the pom.xml file. Add the Maven PMD Plugin configuration inside the <build> section:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>pmd-plugin-demo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-pmd-plugin</artifactId>
                <version>3.17.0</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>check</goal>
                        </goals>
                        <configuration>
                            <targetJdk>21</targetJdk>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

Explanation

  • <groupId>: Specifies the group ID for the Maven PMD Plugin.
  • <artifactId>: Specifies the Maven PMD Plugin.
  • <version>: The version of the plugin you are using. Ensure you use the latest version.
  • <executions>: Defines the plugin executions.
    • <execution>: Specifies an execution of the plugin.
      • <goals>: The goals to be executed during this phase.
        • <goal>: Specifies the goal of the plugin to be executed. In this case, it's the check goal.
      • <configuration>: Allows you to specify plugin configurations.
        • <targetJdk>: Specifies the target JDK version. Here, it is set to 21.

Step 3: Modify the App.java to Introduce an Issue

Open src/main/java/com/example/App.java and introduce a simple issue that PMD can detect. For example, an unused variable:

package com.example;

public class App {
    public static void main(String[] args) {
        int unusedVariable = 0; // This is an unused variable
        System.out.println("Hello World!");
    }
}

Step 4: Run the PMD Check

Run the following command to execute the PMD check:

mvn pmd:check

Output

If there are issues in your code, PMD will report them. In this case, PMD should report the unused variable in App.java.

[INFO] --- maven-pmd-plugin:3.17.0:check (default-cli) @ pmd-plugin-demo ---
[INFO] PMD Failure: com.example.App:7 Rule:UnusedLocalVariable Priority:3 Unused variable 'unusedVariable'.

Step 5: Generate the PMD Report

You can also generate a PMD report to view all the issues. Run the following command:

mvn pmd:pmd

The report will be generated in the target/site directory. Open target/site/pmd.html in your browser to view the report.

Complete Example

Here is the complete pom.xml file for the example project:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>pmd-plugin-demo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-pmd-plugin</artifactId>
                <version>3.17.0</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>check</goal>
                        </goals>
                        <configuration>
                            <targetJdk>21</targetJdk>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

Conclusion

The Maven PMD Plugin is a powerful tool for improving the quality of your code by detecting potential issues early in the development process. This guide provided a complete example of setting up and using the Maven PMD Plugin, helping you integrate static code analysis into your Maven projects. By following these steps, you can easily enhance the quality and maintainability of your code.

Comments