How do I generate a Checkstyle code style report for a site?
Author: Deron Eriksson
Description: This maven tutorial describes how to generate a code style report for a site using the Maven Checkstyle Plugin.
Tutorial created using: Windows Vista || JDK 1.6.0_04 || Eclipse Web Tools Platform 2.0.1 (Eclipse 3.3.1)


Page: < 1 2

(Continued from page 1)

After deploying the site, let's look at the reports that were generated. We can see that the Checkstyle plugin generated the Checkstyle report, and the JXR plugin generated the Source Xref and Test Source Xref reports.

Generated Reports

Let's look at the Checkstyle report. We can see that it provides a summary of the code style issues that it found in the project. According to the summary, when we use Checkstyle with the Sun code configuration, the project had a total of 23 errors.

Checkstyle Results

If we scroll down a little to the Files section, we can see that the Howdy.java file had 12 style errors. Below that, in the Details section, we can see a listing of the 12 violations.

Checkstyle Files and Details sections

Let's see if we can fix a few of those violations. First off, here is the original Howdy.java file:

Original Howdy.java

package com.maventest;

public class Howdy {

	public static void main(String[] args) {
		sayHello();
	}

	public static void sayHello() {
		System.out.println("Howdy says hello!");
	}

}

I'll replace each tab in the file with 4 spaces. I'll also add some lame Javadoc comments.

Updated Howdy.java

package com.maventest;

/**
 * This class does stuff.
 * @author Me
 */
public class Howdy {

    /**
     * The main method does stuff.
     * @param args this is an array of strings that you can pass in
     */
    public static void main(String[] args) {
        sayHello();
    }

    /**
     * This method says hi.
     */
    public static void sayHello() {
        System.out.println("Howdy says hello!");
    }

}

I'll regenerate the site and deploy it. Now, let's look at the Checkstyle report. We can see that the number of violations in the Howdy.java file has been reduced from 12 to 3.

Number of violations reduced

Since we generated the JXR Xref reports, we can click on the Line number of the violation to be taken to the source code location of the violation.

Clicking violation to be taken to xref

Here, we can see that clicking on the link brought me to line 7 of the Howdy class Xref.

Xref for Howdy class.

The Checkstyle report is a handy tool that can be used to enforce a consistent code style across multiple developers in a project.

Page: < 1 2


Related Tutorials: