How to use PMD with eclipse

This tutorial will explain how to use PMD ( with eclipse.

What you need:
1.) eclipse-java-helios
2.) PMD (download the latest version here: Extract in c:\java
a.) From here you can download a zipped update site, install that in eclipse by:
Help->Install New Software->Add->Archive->Then just follow the steps.
3.) After eclipse update, you can immediately run PMD by: right click project->PMD->Generate reports. You can refer on the pmd link for more explanations.
4.) For example this code that I copied from PMD website:
package com.kbs.testng;

public class TestPMDDemo {
	public static void main(String args[]) {
		try {
			// do something
		} catch (Throwable th) { // Should not catch throwable
Will output:

5.) If you want to run PMD via ant task, here's the script (add this to your build.xml):

<?xml version="1.0" encoding="UTF-8"?>
<project name="JavaHelpers" default="" basedir=".">
	<!-- set global properties for this build -->
	<property name="program_name" value="JavaHelpers" />
	<property name="package_name" value="JavaHelpers" />
	<property name="src" value="src" />
	<property name="build" value="build" />
	<property name="pmd.home" value="C:/java/pmd-4.2.5" />
	<property name="pmd.jar" value="pmd-4.2.5.jar" />

	<path id="compile.boot.path">
		<fileset dir="${testng.home}">
			<include name="${testng.jar}" />
		<fileset dir="${java.home}/lib">
			<include name="rt.jar" />

	<target name="pmd">
		<taskdef name="pmd" classname="net.sourceforge.pmd.ant.PMDTask">
			<classpath location="${pmd.home}/lib/${pmd.jar}" />
		<pmd shortFilenames="true">
			<formatter type="html" toFile="pmd_report.html" linkPrefix="" />
			<fileset dir="${src}">
				<include name="**/*.java" />

	<!-- Compile Source -->
	<target name="compile">
		<mkdir dir="${build}/classes" />
		<javac destdir="build/classes" target="1.6" source="1.6">
			<bootclasspath refid="compile.boot.path" />
			<src path="${src}" />
			<include name="**/*.java" />
It will create a file: pmd_report.html in the project's root directory.
How to use PMD with eclipse How to use PMD with eclipse Reviewed by Edward Legaspi on Monday, September 05, 2011 Rating: 5

1 comment:

John Ortiz said...

Thanks for this how-to about PMD in Eclipse. It was useful and easy-to-follow.

Can you recommend me books about PMD? Thanks in advance.

Powered by Blogger.