Analyzing code with the Maven PMD plugin

PMD is a source code analyzer. It finds common programming flaws such as unused variables, empty catch blocks, and unnecessary object creation. It also includes the Copy/Paste Detector (CPD) that finds duplicated code.

How to do it...

Use the following steps to run PMD on a Maven project:

  1. Open the Maven project for which you want to do a PMD analysis (for instance, project-with-violations).
  2. Run the following command:
    mvn pmd:pmd pmd:cpd
    
  3. Observe the output:
    [INFO] --- maven-pmd-plugin:3.2:pmd (default-cli) @ project-with-violations ---
    [WARNING] Unable to locate Source XRef to link to – DISABLED
    [INFO] --- maven-pmd-plugin:3.2:cpd (default-cli) @ project-with-violations ---
    [WARNING] Unable to locate Source XRef to link to - DISABLED
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    
  4. Check the contents of the target folder:
    How to do it...
  5. Open the pmd.xml report:
    How to do it...
  6. Open the cpd.html file in the site folder:
    How to do it...

How it works...

The pmd or cpd goals of the Maven PMD plugin are not bound to any phase. Also, they analyze the Java source and thus, do not need any other Maven phase or goal to be run.

When the pmd goal is run, it generates a PMD site report using default rulesets and the configuration set in the plugin. It also generates a pmd output file in the XML format.

You can also define your own customized ruleset. To do this, add the following code in the configuration section of the pom file:

<reporting>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-pmd-plugin</artifactId>
        <version>3.4</version>
        <configuration>
          <rulesets>
            <!-- Two rule sets that come bundled with PMD -->
            <ruleset>/rulesets/java/braces.xml</ruleset>
            <ruleset>/rulesets/java/naming.xml</ruleset>
            <!-- Custom local file system rule set -->
            <ruleset>d:
ulesetsstrings.xml</ruleset>
            <!-- Custom remote rule set accessed via a URL -->
            <ruleset>http://localhost/design.xml</ruleset>
          </rulesets>
        </configuration>
      </plugin>
    </plugins>
  </reporting>

Likewise, when the cpd goal is run, it generates a similar report for duplicated code. By default, the minimum token count that it considers to report that code is duplicated is 100 tokens (which is typically 10 lines of code).

There's more...

The plugin can be made to fail the build by using the check goal in the following way:

  1. Add the following code to the pom file of project-with-violations:
    <build>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-pmd-plugin</artifactId>
          <version>3.4</version>
          <executions>
            <execution>
              <goals>
                <goal>check</goal>
                <goal>cpd-check</goal>
              </goals>
            </execution>
          </executions>
        </plugin>
      </plugins>
    </build>
  2. Run the following command:
    mvn verify
    
  3. Observe the output as shown in the following screenshot:
    There's more...
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.183.210