Looking for a JavaEE Architect or Potential Tech Co-Founder?
Please don't hesitate to contact me.

How to add PMD reporting to Maven

Before proceeding to this exercise, you may want to try my tutorial on how to setup a maven project to eclipse:
http://czetsuya-tech.blogspot.com/2012/04/how-to-create-modularized-maven-project.html.

Assuming you have follow the tutorial above, you should have 1 maven project (ipiel) that contains 2 modules (ipiel-models, ipiel-portal), where portal is dependent to models.

Basically we would just add pmd configurations on ipiel-portal's pom.xml. Better take a look at this link:
http://maven.apache.org/plugins/maven-pmd-plugin/index.html

4 Goals from the plugin:
  pmd:pmd
  pmd:cpd
  pmd:check
  pmd:cpd-check

Here's my ipiel-portal's new pom.xml, it has comments and it's straightforward so you shouldn't be confuse.
<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>
 <parent>
  <artifactId>ipiel</artifactId>
  <groupId>com.ipiel</groupId>
  <version>0.0.1-SNAPSHOT</version>
 </parent>
 <groupId>com.ipiel.portal</groupId>
 <artifactId>ipiel-portal</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <name>Ipiel Portal</name>
 <description>Ipiel Portal</description>
 <dependencies>
  <dependency>
   <groupId>com.ipiel.model</groupId>
   <artifactId>ipiel-models</artifactId>
   <version>0.0.1-SNAPSHOT</version>
  </dependency>
  <dependency>
   <groupId>pmd</groupId>
   <artifactId>pmd</artifactId>
   <version>4.3</version>
  </dependency>
 </dependencies>
 <build>
  <plugins>
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-pmd-plugin</artifactId>
    <version>2.7.1</version>
    <executions>
     <execution>
      <goals>
       <!-- build will fail if error found on pmd -->
       <goal>check</goal>
       <goal>cpd-check</goal>
      </goals>
     </execution>
    </executions>
    <configuration>
     <sourceEncoding>utf-8</sourceEncoding>
     <minimumTokens>100</minimumTokens>
     <targetJdk>1.5</targetJdk>
    </configuration>
   </plugin>
  </plugins>
 </build>
 <reporting>
  <plugins>
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-pmd-plugin</artifactId>
    <version>2.7.1</version>
    <configuration>
     <rulesets>
      <!-- Two rule sets that come bundled with PMD -->
      <ruleset>/rulesets/braces.xml</ruleset>
      <ruleset>/rulesets/naming.xml</ruleset>
     </rulesets>
    </configuration>
    <reportSets>
     <reportSet>
      <reports>
       <!-- enable only 1 report -->
       <report>pmd</report>
       <!-- <report>cpd</report> -->
      </reports>
     </reportSet>
    </reportSets>
   </plugin>
  </plugins>
 </reporting>
</project>

After that, in eclipse create a new Run Configuration->Maven Build as follow,
You might want to add an empty try/catch block in your Runner.java to test if PMD works then run pmd:check. It should generate several files inside target folder.

In my case it gives several error messages, and the build won't continue because I added a build fail statement in pom.xml's build section.
Here's the message:
com/ipiel/portal/Runner.java
Violation Line
  Avoid empty try blocks 13 - 15
  Avoid empty catch blocks 15 - 17
How to add PMD reporting to Maven How to add PMD reporting to Maven Reviewed by Edward Legaspi on Monday, April 09, 2012 Rating: 5

No comments:

Powered by Blogger.