Home Tutorials Training Consulting Products Books Company Donate Contact us









NOW Hiring

Quick links

Share

Using the Checkstyle plug-in This tutorial will describe the usage of the Checkstyle plug-in for Eclipse.

1. Checkstyle

1.1. Overview

Checkstyle is a tool to help ensuring that your Java code adheres to a set of coding standards

1.2. Installation

http://eclipse-cs.sourceforge.net/update Update site for the Eclipse Checkstyle Plugin.

Please see Eclipse Update Manager for more information on how to use the update manager.

If you developing with Eclipse, make sure to select the Sun Conventions (Eclipse) under

Window ▸ Preferences ▸ Checkstyle. Press the "Set as Default" after selecting the right entry.

checkstyle10

1.3. Configuration

You can turn of certain checks. If you change settings from the standard profile you should always make a copy of the existing profile.

To customizine your check, first make a copy of the checks

checkstyle20

Select your new configuration and press Configure. De-activatefor example the checks for Javadoc Comments.

checkstyle30

Make this new setting your default one.

1.4. Using common Checkstyle rules for teams

For teams it is good to follow the same coding style rules. The Eclipse checkstyle plug-in allows this by proving a remote site for the checkstyle settings.

Press new in the settings. Select "Remote Configuration". Give the rule set a description and then type in the URL you want to use for the settings.

checkstyle50

Make this new setting your default one.

1.5. Using Checkstyle in your projects

Make your new profile the default one. You need to activate the Eclipse Checkstyle Plugin for your project. Right-click on your project and search for Checkstyle. Select the checkbox "Checkstyle active for this project".

checkstyle100

You can use the checkstyle browser view to display the violations.

checkstyle110

2. Checkstyle Maven Plugin

Checkstyle can also be run during a Maven build. The maven-checkstyle-plugin can generate reports about checkstyle violations or can also be a part of the build and cause a build failure when the rules defined in the checkstyle.xml are violated.

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-checkstyle-plugin</artifactId>
            <version>2.17</version>
            <executions>
                <execution>
                    <id>validate</id>
                    <phase>validate</phase>
                    <configuration>
                        <configLocation>checkstyle.xml</configLocation>
                        <encoding>UTF-8</encoding>
                        <consoleOutput>true</consoleOutput>
                        <failsOnError>true</failsOnError>
                        <linkXRef>false</linkXRef>
                    </configuration>
                    <goals>
                        <goal>check</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

More information about the usage can be found here: Maven Checkstyle plugin usage

3. Exercise: Use the Checkstyle Maven plugin

3.1. Target

The target of this exercise is to check code being build with maven and let the build fail in case rules are violated.

3.2. Prerequisites

For a introduction to maven please refer to the Apache Maven tutorial.

3.3. Create a maven project

Create a quick start maven project in an IDE or by using the command line like this:

mvn archetype:generate -DgroupId=com.vogella.build.maven.java \
-DartifactId=com.vogella.build.maven.java  \
-DarchetypeArtifactId=maven-archetype-quickstart \
-DinteractiveMode=false

3.4. Apply the checkstyle plugin

To activate the maven-checkstyle-plugin it has to be applied to the generated pom.xml.

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-checkstyle-plugin</artifactId>
            <version>2.17</version>
            <executions>
                <execution>
                    <id>validate</id>
                    <phase>validate</phase>
                    <configuration>
                        <encoding>UTF-8</encoding>
                        <consoleOutput>true</consoleOutput>
                        <failsOnError>true</failsOnError>
                        <linkXRef>false</linkXRef>
                    </configuration>
                    <goals>
                        <goal>check</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

This configuration uses the sun_checks.xml checkstyle rules by default. In order to specify custom checkstyle rules a configLocation needs to be specified. See Checkstyle Maven Plugin

3.5. Validate

Run mvn clean verify to see all checkstyle validation failures of the generated project.

maven checkstyle execution

4. About this website

5.2. vogella GmbH training and consulting support

TRAINING SERVICE & SUPPORT

The vogella company provides comprehensive training and education services from experts in the areas of Eclipse RCP, Android, Git, Java, Gradle and Spring. We offer both public and inhouse training. Whichever course you decide to take, you are guaranteed to experience what many before you refer to as “The best IT class I have ever attended”.

The vogella company offers expert consulting services, development support and coaching. Our customers range from Fortune 100 corporations to individual developers.

Copyright © 2012-2016 vogella GmbH. Free use of the software examples is granted under the terms of the EPL License. This tutorial is published under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Germany license.

See Licence.