Chapter 4. Coding Standards

Table of Contents

4.1. Checkstyle
4.1.1. Maven Plugin
4.1.2. Eclipse Plugin
4.2. PMD
4.2.1. Maven Plugin
4.2.2. Eclipse Plugin
4.3. Code Coverage (Cobertura and Emma)
4.3.1. Maven Plugin (Cobertura)
4.3.2. Eclipse Plugin (Emma)
4.4. Eclipse Code Style
4.4.1. Formatter
4.4.2. Cleanup
4.4.3. Save Actions

Abstract

This chapter describes how we enforce coding standards for sister projects. Follow the steps in this chapter if you intend to contribute back to the sister projects. There's no need to perform these steps if you are just building the sister projects from source.

All the sister projects use Checkstyle and PMD to enforce coding standards, and use Cobertura and Emma to capture code coverage. Each of these tools has integration with both Maven and Eclipse. In addition, a number of Eclipse config files define formatting and clean-up standards that are complementary to the Checkstyle standards.

All of the config files described here are checked into Subversion under trunk/main/standards/src/main/resources. To ease distribution they are released as part of the Star Objects sourceforge website. The config files are not versioned (other than by Subversion itself).

4.1. Checkstyle

Checkstyle is a powerful tool for enforcing coding standards and detecting certain classes of likely errors. The checkstyle definition project is derived from Sun's standards, with a number of modifications, and can be accessed from the Star Objects sourceforge website.

Checkstyle integrates with both Maven and with Eclipse.

4.1.1. Maven Plugin

The corporate POM (see Part IV, “Building and Deploying the Corporate Artifacts”) automatically includes the Maven checkstyle plugin:

<!-- quality checks: checkstyle -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-checkstyle-plugin</artifactId>
    <configuration>
        <configLocation>
          http://starobjects.sourceforge.net/m2-site/main/standards/resources/checkstyle.xml
        </configLocation>
        ...
    </configuration>
</plugin>

This plugin is not bound to any Maven lifecycle phase, and is not intended to be run other than as a report within mvn site. In particular, note that the configuration is only defined in the <reporting> section, so it isn't possible to run using mvn checkstyle:checkstyle. For more immediate feedback, use the Eclipse plugin, below.

4.1.2. Eclipse Plugin

The eclipse-cs plugin allows Checkstyle violations to be flagged as warnings or errors within the Problems view of the Eclipse IDE. To associate eclipse-cs with the Checkstyle config file, use Windows > Preferences, and specify the config file as http://starobjects.sourceforge.net/m2-site/main/standards/resources/checkstyle.xml:

TODO: the screenshot is out of date

According to eclipse-cs' documentation, it is meant to integrate with m2eclipse and transparently pick up any Maven configuration of mvn-checkstyle-plugin. This doesn't seem to work for me, though.

You may then need to enable CheckStyle for each project as required, using the context menu in Package Explorer. CheckStyle violations show up in the Problems view:

These violations are dynamic updated, so fixing any problem should automatically remove the violation from the problems view. A quick fix short cut (ctrl+1) is available for some of these.