Maven PMD plug-in not generating a report with 'mvn site' command or 'pmd:pmd'

user3870315 picture user3870315 · Feb 28, 2015 · Viewed 9k times · Source

I am reading an interesting tutorial here: http://www.avajava.com/tutorials/lessons/how-do-i-generate-pmd-and-cpd-reports-for-a-site.html?page=1

This tutorial shows how to use Maven to run the open-source static-analysis tool, PMD, and to see the generated output on a Maven created website. Maven can easily create websites with mvn site command, but this tutorial shows how to use PMD for more helpful metrics on the source code.

The instructions were followed to the best of my ability. Here is my pom.xml file that came from reading the tutorial:

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.name.bookstore</groupId>
  <artifactId>bookstore</artifactId>
  <packaging>jar</packaging>
  <version>1</version>
  <name>bookstore</name>
  <url>http://maven.apache.org</url>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <extensions>
      <!-- start for deploying using webdav -->
      <extension>
        <groupId>org.apache.maven.wagon</groupId>
        <artifactId>wagon-webdav</artifactId>
        <version>1.0-beta-2</version>
      </extension>
    </extensions>
  </build>

  <distributionManagement>
    <!-- start -location where site is deployed -->
    <site>
      <id>site.deployments</id>
      <name>Site deployments</name>
      <url>dav:localhost/${basedir}</url>
    </site>
  </distributionManagement>

  <reporting>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-pmd-plugin</artifactId>
        <version>3.4</version>

      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jxr-plugin</artifactId>
        <version>2.5</version>
      </plugin>

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-javadoc-plugin</artifactId>
        <version>2.10.1</version>
      </plugin>
    </plugins>
  </reporting>

</project>

When I run the command: mvn clean site I get a website built by Maven with a bunch of different pages but none of them show anything in regards to PMD. What am I missing here? Why am I not seeing anything in regards to PMD in the generated website?

Also, when I run mvn pmd:pmd there is a successful build but I do not obtain any helpful PMD metrics. I even coded in some unused variables and methods in one of my Java source files as illustrated in the above linked tutorial and there is no helpful output.

The mvn pmd:pmd command does appear to create some files though. A couple are files of rules for the engine it looks like and the others are empty. Please see the screen-shots of this below:

Files created with mvn pmd:pmd command Figure 1: Files created by pmd:pmd command

empty file for pmd

Figure 2: Empty pmd file - even though there are obvious errors in Java source file

Does anyone out there know what is up? Why is PMD not working with Maven for me?

Thanks for reading this.

Also, from what I have read around the Internet on PMD's website and Maven's website there should be some information in the "Project Reports" section. There is no data here though from PMD. Please see the below screen-shot.

No PMD Data in Project Reports

Figure 3: No PMD Data found in Project Reports

Regards

UPDATE

When I change the PMD section of the pom.xml file to the below snippet I obtain some CPD results via PMD but still nothing from PMD on code bugs. I even coded in an NullPointerException and PMD said nothing even when issuing the mvn pmd:check command.

 <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-pmd-plugin</artifactId>
    <version>3.4</version>
    <configuration>

      <linkXref>true</linkXref>
      <sourceEncoding>utf-8</sourceEncoding>
      <minimumTokens>1</minimumTokens>
      <targetJdk>1.7</targetJdk>
    </configuration>
  </plugin>

In the snippet I changed the sourceEncoding tag to be utf-8 because everything I see in regards to this is utf-8. I also changed the minimumTokens value to 1 to try to get more output from this plug-in. I also put this snippet in the <build> section to try and get results but still nothing... :/

Thanks for studying this...

Answer

adangel picture adangel · Mar 1, 2015

The maven-pmd-plugin by default skips nowadays empty reports (property skipEmptyReport). You'll need to set this to false, to get in your site always a PMD/CPD report:

<reporting>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-pmd-plugin</artifactId>
      <version>3.4</version>
      <configuration>
        <skipEmptyReport>false</skipEmptyReport>
      </configuration>
    </plugin>
  </plugins>
</reporting>

This applies for both PMD and CPD. I assume, this is your problem, as in Figure 2, you show, there are no PMD violations detected (pmd.xml file is empty).

The property minimumTokens configures CPD and defines how long a code snipped at a minimum must be to be declared as a duplicate. The lower the number, the more duplicates are detected, but the duplicates can also be much shorter and therefore maybe more often false positives.

Without further configuring maven-pmd-plugin it uses by default these three PMD rulesets: java-basic, java-imports, java-unusedcode. See also property rulesets. If you want to detect specific problems, then you'll need to enable these rules. See also How to make a ruleset.