Getting Started

Here’s how to use Checkstyle Addons with your standard software.

Checkstyle Addons is distributed via Maven Central and Bintray jcenter, so major build tools can automatically access it.

Checkstyle Addons is compatible with all Checkstyle versions starting from 6.0. That is because Checkstyle 5.9 and earlier were based on Java 5, which is no longer available on our continuous integration platform, Travis CI.

Checkstyle Addons uses semantic versioning, so you can always use the latest within a major version, but you should try things out before moving to a higher major version.

Gradle

Checkstyle Addons is available on Maven Central and jcenter, so you can just use it in your Gradle build. In Gradle, Checkstyle Addons runs as part of the Gradle Checkstyle Plugin. The classpath is extended to include Checkstyle Addons. Note that the Checkstyle tool version must be specified.

plugins {
    id 'checkstyle'  // regular Checkstyle plugin
}
dependencies {
    checkstyle group: 'com.thomasjensen.checkstyle.addons', name: 'checkstyle-addons', version: '4.1.1'
}
checkstyle {
    configFile file('your-checkstyle.xml')
    toolVersion '7.0'   // set Checkstyle version here
}

For the checkstyle-addons dependency, you may also use the version notation version: '4.+' in order to always get the latest compatible version.

The example above has been tested with Checkstyle toolVersion set to 6.2 - 7.6. The older Checkstyle versions 6.0 - 6.1.1 are also supported, but if you set toolVersion to these, you must depend on checkstyle-addons-java6 instead of checkstyle-addons (even if you use Java 7 or higher).

Maven

Checkstyle Addons is available on Maven Central, so you can just use it in your Maven build. In Maven, Checkstyle Addons runs as part of the Maven Checkstyle Plugin. The classpath is extended to include Checkstyle Addons. Note that the explicit dependency on Checkstyle itself must also be specified in order to set the correct Checkstyle version.

<project>
  <!-- snip -->
  <properties>
    <!-- path to your checkstyle.xml, relative to the project root -->
    <checkstyle.config.location>your-checkstyle.xml</checkstyle.config.location>
  </properties>
  <!-- snip -->
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-checkstyle-plugin</artifactId>
    <version>2.16</version>
    <dependencies>
      <dependency>
        <groupId>com.puppycrawl.tools</groupId>
        <artifactId>checkstyle</artifactId>
        <version>7.0</version>    <!-- set Checkstyle version here -->
      </dependency>
      <dependency>
        <groupId>com.thomasjensen.checkstyle.addons</groupId>
        <artifactId>checkstyle-addons</artifactId>
        <version>4.1.1</version>
      </dependency>
    </dependencies>
  </plugin>
</project>

The example above has been tested with the Checkstyle dependency set to 6.2 - 7.6. The older Checkstyle versions 6.0 - 6.1.1 are also supported, but if you use these, you must depend on checkstyle-addons-java6 instead of checkstyle-addons (even if you use Java 7 or higher).

Ant

In Ant, we must add Checkstyle Addons to the classpath of the Checkstyle Ant Task. The Ant task is part of the standard Checkstyle distribution. We need at least Checkstyle 6.2 (currently tested up to 7.6), and the Checkstyle Addons FatJAR:

Download FatJAR

The FatJAR (some people say “UberJAR”) is simply a JAR which includes Checkstyle Addons along with all its dependencies except Checkstyle itself.

Then, in our Ant build.xml, we declare the Checkstyle task as follows:

<taskdef resource="checkstyletask.properties">
    <classpath>
        <pathelement location="lib/checkstyle-7.0-all.jar"/>
        <pathelement location="lib/checkstyle-addons-4.1.1-all.jar"/>
    </classpath>
</taskdef>

The checkstyletask.properties is read from checkstyle-7.0-all.jar. The call to the Checkstyle task is standard Ant:

<checkstyle config="/path/to/your-checkstyle.xml">
    <fileset dir="src" includes="**/*.java"/>

    <!-- Where in your build folder to store the cache file used during build for faster analysis -->
    <property key="checkstyle.cache.file" file="target/cachefile"/>
</checkstyle>

For more information on Checkstyle Ant task configuration, please refer to its website.

If you use Ivy for dependency management in Ant, you can take advantage of the fact that Checkstyle Addons is on Maven Central.

IntelliJ IDEA / Android Studio

In IntelliJ IDEA or Android Studio, Checkstyle Addons runs as a third-party addon to the Checkstyle-IDEA plugin. So first make sure you’ve got Checkstyle-IDEA. If not, install via File → Settings… → Plugins → Browse Repostories.

Download Plugin

After that, you can add Checkstyle Addons to the third-party checks:

Checkstyle Addons in IntelliJ IDEA

Checkstyle-IDEA does not offer a visual editor, so you’ll have to activate the Checkstyle Addons checks by modifying your checkstyle.xml directly.

The Checkstyle Addons version offered by the above download button is compatible with Checkstyle 6.2 - 7.6 (which means Checkstyle-IDEA 4.14.0 and up). If you are using the older Checkstyle versions 6.0 - 6.1.1, you must use a special, compatible version of Checkstyle Addons instead:

Download Legacy Plugin

Eclipse

In Eclipse, Checkstyle Addons runs as an addon to Eclipse-CS. So first make sure you’ve got Eclipse-CS installed at version 6.2.0 or higher. (Eclipse-CS 6.1.x is also supported with the legacy plugin, see below.) Then:

Download Eclipse Plugin

Installation: Drop the downloaded plugin JAR into the dropins folder of your Eclipse installation and restart Eclipse.

In order to verify that Checkstyle Addons was installed correctly, open the Checkstyle configuration dialog and you should see a new module category named ‘Checkstyle Addons’. Checkstyle Addons fully supports the visual Checkstyle configuration editor of Eclipse-CS:

Checkstyle Addons in Eclipse

The Checkstyle Addons version offered by the above download button is compatible with Checkstyle 6.2 - 7.6 (which means Eclipse-CS 6.2.0 and up). If you are using the older Checkstyle versions 6.0 - 6.1.1, you must use a special, compatible version of Checkstyle Addons instead:

Download Legacy Plugin

SonarQube

In SonarQube, Checkstyle Addons runs as an extension of the SonarQube Checkstyle Plugin (download, legacy download). Checkstyle Addons currently supports the SonarQube Checkstyle Plugin versions 2.2 (orange column) or later (blue columns). Make sure your SonarQube installation is configured accordingly:

SonarQube Checkstyle Plugin 2.0 2.1 2.1.1 2.2 2.3 2.4 3.1 - 3.3 ≥ 3.4
SonarQube Platform 3.6 - 4.1.2 4.2 - 4.4 ≥ 4.5.2 ≥ 4.5.2 ≥ 4.5.2 ≥ 5.6.4
Checkstyle (by plugin) 5.6 5.6 5.6 6.1 6.4.1 6.12.1 7.1 - 7.3 ≥ 7.4
Java (analysis process) ≥ 6 ≥ 6 ≥ 6 ≥ 6 ≥ 7 ≥ 7 ≥ 8 ≥ 8
Java (code analyzed) ≤ 7 ≤ 7 ≤ 7 ≤ 8 ≤ 8 ≤ 8 ≤ 8 ≤ 8

The Checkstyle version is directly determined by the version of the SonarQube Checkstyle plugin, so you don’t need to check that explicitly. Starting with Checkstyle 7.1, the Checkstyle team release a matching SonarQube plugin every time. SonarQube 3.6 is the earliest version on which Checkstyle Addons could possibly run, even if you built from source.

The SonarQube rules provided by Checkstyle Addons are tagged with checkstyle-addons, which is useful to filter them from the overall list of Checkstyle rules.

Download SonarQube Plugin       Download SonarQube Plugin for 2.2

Installation: Drop the downloaded plugin into the extensions/plugins folder of your SonarQube installation (where all the other plugins are). Double-check the version of the SonarQube Checkstyle plugin according to the above table. Upgrade that if necessary. Restart SonarQube.

Command Line

Checkstyle can also be run from the command line. Checkstyle Addons is included by simply adding its FatJAR to the classpath. If you are using at least Java 7, and at least Checkstyle 6.2 (tested up to 7.6), you may use the regular JAR. If you are using Checkstyle 6.0 - 6.1.1, you must use the legacy JAR. The code you’re analyzing can be anything up to Java 8.

Download regular JAR       Download legacy JAR

Now you can run Checkstyle with Checkstyle Addons like this (example for regular JAR):

java -cp checkstyle-addons-4.1.1-all.jar;checkstyle-7.0-all.jar com.puppycrawl.tools.checkstyle.Main -c your-checkstyle.xml src

The above assumes that your sources are in a subdirectory src, and that the required JARs and your configuration file are in the current directory.