Source Code Quality Checks using PMD Plugin

Ensuring high-quality code is essential for maintaining a healthy and sustainable codebase. One of the tools that can help with this is the PMD plugin for Maven. PMD is a source code analyzer that finds common programming flaws like unused variables, empty catch blocks, unnecessary object creation, and more. In this blog post, we'll explore how to integrate the PMD plugin into your Maven project to perform source code quality checks.

What is PMD?

PMD stands for Programming Mistake Detector. It's an open-source static code analyzer that scans your source code and reports potential issues, such as:

  • Unused variables
  • Empty catch blocks
  • Unnecessary object creation
  • Code complexity

By integrating PMD into your Maven build process, you can automatically check your code for these issues during the build.

Adding PMD Plugin to Maven Project

To integrate PMD into your Maven project, you need to add the PMD plugin to your pom.xml file. Here's how you can do it:

  1. Open your pom.xml file:
<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>my-maven-project</artifactId>
    <version>1.0-SNAPSHOT</version>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-pmd-plugin</artifactId>
                <version>3.15.0</version>
                <executions>
                    <execution>
                        <phase>verify</phase>
                        <goals>
                            <goal>pmd</goal>
                            <goal>cpd-check</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <targetJdk>21</targetJdk>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
  1. Configure the PMD Plugin:

In the above pom.xml configuration:

  • The PMD plugin is configured to run during the verify phase of the Maven build lifecycle.
  • It includes two goals: pmd to run the PMD analysis and cpd-check to check for copy-paste code (duplicate code).
  1. Run PMD Plugin:

To run the PMD analysis, execute the following Maven command:

mvn pmd:check

This will generate a report of potential issues found in your source code.

Analyzing PMD Reports

After running the PMD plugin, you can find the generated reports in the target/site directory of your project. The reports include details about the issues detected by PMD, such as the type of issue, the location in the code, and a description.

To view the reports, navigate to target/site and open the pmd.html file in a web browser. This file contains a comprehensive report of all issues detected by PMD.

Customizing PMD Rules

PMD provides a set of default rules, but you can customize these rules to fit your project's needs. To do this, you can create a custom ruleset file and configure the PMD plugin to use it.

  1. Create a custom ruleset file:

Create a file named pmd-ruleset.xml in your project's root directory with the following content:

<?xml version="1.0"?>
<ruleset name="Custom Ruleset"
         xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd">
    <description>Custom ruleset for my project</description>

    <!-- Add or remove rules as needed -->
    <rule ref="category/java/bestpractices.xml/UnusedImports"/>
    <rule ref="category/java/errorprone.xml/UnusedLocalVariable"/>
    <rule ref="category/java/design.xml/ExcessiveMethodLength"/>
</ruleset>
  1. Configure PMD Plugin to use the custom ruleset:

Update the PMD plugin configuration in your pom.xml to use the custom ruleset file:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-pmd-plugin</artifactId>
            <version>3.15.0</version>
            <executions>
                <execution>
                    <phase>verify</phase>
                    <goals>
                        <goal>pmd</goal>
                        <goal>cpd-check</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <targetJdk>21</targetJdk>
                <rulesets>
                    <ruleset>pmd-ruleset.xml</ruleset>
                </rulesets>
            </configuration>
        </plugin>
    </plugins>
</build>

Conclusion

Integrating PMD into your Maven project is a great way to automate source code quality checks and maintain a high-quality codebase. By following the steps outlined in this blog post, you can easily add the PMD plugin to your project, customize the ruleset to fit your needs, and analyze the generated reports to identify and fix potential issues in your code. Regularly running PMD as part of your build process will help you catch issues early and ensure that your code remains clean, efficient, and maintainable.

Comments