How to add PMD reporting to Maven
Tech-Today

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 Create A Modularized Ear Project In Maven
This post is one way of creating a typical javaee6 maven project that contains ear, web, ejb and api. The output of course is an ear file that contains (web, ejb and api). How it looks like (assuming our top project is named ipiel): +ipiel  +ipiel-ear...

- How To Generate Jax-rs Documentation Using Maven
To generate the jax-rs documentation automatically using maven, we need to add some plugins in the build section of the project's pom file. To avoid running this plugin every time you invoke mvn install, you can create a separate profile for it. Here's...

- How To Get Git Build Id Using Maven
There are times when git's build number is important for a release. Specially in development mode, when there are frequent releases. So if we want to append the build number on our page, how do we automate it? For us to achieve this we will need 2...

- How To Ignore Maven's Plugin Execution Not Covered By Lifecycle Configuration Warning
There are times when you want to ignore some of maven warnings or errors. To do so, normally we add the ff code in the parent project's pom.xml. For example to ignore liferay's theme-merge and build-thumbnail goal warning. <pluginManagement>...

- How To Create A Modularized Maven Project In Eclipse
I've just noticed that it's easier to do this now on eclipse-jee-indigo, no need to use the console :-) I'll try to make this write up short, but I guess it will not be. So first, you should have the ff installed (if you don't know how...



Tech-Today








.