Maven Checkstyle Plugin — Usage

iStock 000002882014XSmall 150x150 Maven Checkstyle Plugin    Usage photo    technology blogroll  programming plugin mvn maven Java development continuous integration checkstyle build process The title might not be the best, I’ll give you this, however, having just spent some time trying to get Checkstyle to run in the site phase of one of my maven projects I have figured out that a lot of the resources you find out there miss out a few important details. So if you are looking for the complete usage guide of the Maven Checkstyle plugin, this post isn’t what you’re looking for. However, if just like me you found yourself adding the configuration example given on the Maven Checkstyle plugin and then issuing a mvn site only to find out that Checkstyle plugin doesn’t kick in, then this is post is probably for you.

I’m not going to show you here how to configure your Checkstyle rules but rather what you need to include in your pom.xml to actually get the Checkstyle plugin to generate the reports when you issue a mvn site.

I am currently using the following configuration of Java + Maven:

Apache Maven 3.0.3 (r1075438; 2011-02-28 09:31:09-0800)
Maven home: /usr/share/maven
Java version: 1.6.0_29, vendor: Apple Inc.
Java home: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
Default locale: en_US, platform encoding: MacRoman
OS name: "mac os x", version: "10.7.2", arch: "x86_64", family: "mac"

So here’s what you will find on the Maven Checkstyle usage page as a recommendation for what to include in your pom.xml to get Checkstyle to run part of the site goal:

<project>
  ...
  <reporting>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-checkstyle-plugin</artifactId>
        <version>2.8</version>
      </plugin>
    </plugins>
  </reporting>
  ...
</project>

However if at this point you try mvn site there are no Checkstyle reports generated — look in target/site/ directory and you will notice it’s empty! You will quite likely see that if you specifically invoke the plugin (mvn Checkstyle:Checkstyle) it actually works fine and generates the reports correctly — so the problem is not the configuration of the plugin, right?

As it turns out the problem is the maven site plugin! By default, in my case, maven uses maven-site:2.0.1 which seems to not do much apart from generating the site directory. What you need to do is to use maven-site:3.0 — and I swear this doesn’t get mentioned anywhere in the docco’s I’ve seen online so it took me a bit of digging and trial-and-error until I figured out this was the culprit!

Simply include this bit in your <build> section :

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-site-plugin</artifactId>
    <version>3.0</version>
</plugin>

So your pom.xml looks like this:

<project>
...
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-site-plugin</artifactId>
            <version>3.0</version>
        </plugin>
    </plugins>
...
</build>
 
<reporting>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-checkstyle-plugin</artifactId>
        <version>2.8</version>
      </plugin>
    </plugins>
</reporting>
 
...
</project>

Now if you invoke mvn site your Checkstyle reports are there!

Tags: , , , , , , , ,

1 Response to "Maven Checkstyle Plugin — Usage"

Leave a Comment