Maven

Apache Maven Checkstyle Plugin Tutorial

Maven Checkstyle is a tool to help ensure that the application’s Java code adheres to a set of coding standards.

1. Introduction

The Eclipse Checkstyle Plugin (aka eclipse-cs) integrates the static source code analyzer Checkstyle into the Eclipse IDE. Checkstyle is an Open Source development tool to help developers ensure that their Java code adheres to a set of coding standards and it does this by inspecting the Java source code and pointing out the items that deviate from a defined set of coding rules.
 
 

1.1 Basic Concept

This plugin uses a project builder to check the project files with Checkstyle. Assuming the Eclipse Auto-Build feature is enabled each modification of a project file will immediately get checked by Checkstyle on file save i.e. giving developers an immediate feedback about the changes they made.

To use a simple analogy, the Checkstyle Plug-in works very much like a compiler but instead of producing the .class files it produces warnings where the code violates the Checkstyle rules. The discovered deviations are accessible in the Eclipse Problems View, as the code editor annotations, and via additional Checkstyle violations views.

The set of rules used to check if the code is highly configurable in the Checkstyle plugin. A Checkstyle configuration specifies which check rules are validated against the code and with which the severity violations will be reported. Once a Checkstyle configuration is defined, it can be used across multiple projects. The plugin comes with several pre-defined Checkstyle configurations.

Developers can create custom configurations using the plugin’s Checkstyle configuration editor or even use an existing Checkstyle configuration file from an external location.

1.2 What does it do?

With the Checkstyle Eclipse Plugin, the code is constantly inspected for coding standard deviations. Within the Eclipse workbench, the developers are immediately notified of problems via the Eclipse Problems View and Source Code annotations similar to the compiler errors or warnings.

1.3 Why would I use it?

If the development team consists of more than one person, then obviously a common ground for coding standards (i.e. formatting rules, line lengths etc.) must be agreed upon – even if it is just for practical reasons to avoid superficial, format related merge conflicts. Checkstyle (and the Eclipse Checkstyle Plugin for that matter) helps you define and easily apply these common rules.

1.4 Maven Checkstyle Installation

Checkstyle in Eclipse is installed by two ways i.e. Use the Eclipse Marketplace or via the Install New Software in the Eclipse Help menu.

1.4.1 Install Checkstyle plug-in by Eclipse Marketplace

  • Go To Help -> Eclipse Marketplace and search for Checkstyle. Developers will see the below screen:

    Fig. 1: Eclipse Marketplace
    Fig. 1: Eclipse Marketplace
  • Click on the Install button associated with the title “Check style Plug-in
  • Follow the instructions and the Eclipse Ide will successfully install the Checkstyle plugin. Developers will need to restart the Eclipse for the Checkstyle plugin to start working

1.4.2 Install Checkstyle plug-in Install New Software

  • Go to Help -> Install New Software and paste the http://eclipse-cs.sourceforge.net/update in the Work with text box
  • Click on add button and provide a name. Click OK. Developers will see the below screen:

    Fig. 2: Install New Software
    Fig. 2: Install New Software
  • Follow the instructions and the Eclipse Ide will successfully install the Checkstyle plugin. Developers will need to restart the Eclipse for the Checkstyle plugin to start working

1.4.3 Verify Installation of Checkstyle Plugin in Eclipse

Go to Window -> Preferences and search for Checkstyle. If developers get the below screen, they have successfully installed the Checkstyle plug-in.

Fig. 3: Checkstyle Plugin in Eclipse Ide
Fig. 3: Checkstyle Plugin in Eclipse Ide

1.5 Creating a custom Checkstyle configuration

The built-in configurations that ship with the plugin will only get developers so far. Chances are that developers will require a custom configuration which fulfills their teams own coding standard. Here’s how it works:

  • Open the Eclipse Preferences Window

    Fig. 4: Preference Window
    Fig. 4: Preference Window
  • Navigate to the Checkstyle section within the Eclipse preferences

    Fig. 5: Checkstyle Preference Settings Page
    Fig. 5: Checkstyle Preference Settings Page
  • Click the New button to create a new Checkstyle configuration

    Fig. 6: New Checkstyle Configuration
    Fig. 6: New Checkstyle Configuration
  • Select the newly created configuration and press the Configure button to open the Configuration Editor

    Fig. 7: Configuration Selection
    Fig. 7: Configuration Selection
  • Make this new setting your default one

2. Checkstyle Maven Plugin

Checkstyle can also run during a Maven build. The maven-checkstyle-plugin can generate reports about checkstyle violations or can also be a part of the 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>

3. Using the Checkstyle Maven plugin

3.1 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.2 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 the custom checkstyle rules, a configLocation needs to be specified.

3.3 Validate

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

Fig. 8: Validation
Fig. 8: Validation

That’s all for this post. Happy Learning!!

4. Conclusion

Here, in this example, we understood the implementation of the Maven Checkstyle plugin in Eclipse IDE. I hope this article has served you with whatever developers are looking for.

Yatin

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button