Maven Checkstyle configLocation ignored

checkstylemaven

Attempting to set a custom checkstyle configuration in the maven pom according to the guidelines on the apache site isn't working in a very simple case.

I created a project, MyProject, using the directory layout recommended by Maven (i.e. src/main/java/, src/main/resources), a single file MyClass.java:

package com.myproject;

public class MyClass {

   public static void main(String[] args) {
      System.out.println("This line is longer than 80 characters which returns an error in sun_checks.xml, however my_checks.xml allows for much longer lines and will not return a long line error.");
   }
}

an empty checkstyle file, my_checks.xml:

<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
    "-//Puppy Crawl//DTD Check Configuration 1.2//EN"
    "http://www.puppycrawl.com/dtds/configuration_1_2.dtd">

<module name="Checker">
    <property name="severity" value="warning"/>
    <module name="TreeWalker">
    </module>
</module>

and a pom file according to the specifications in the guide:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.myproject</groupId>
    <artifactId>A_Project</artifactId>
    <name>A Project</name>
    <version>1.0.0</version>

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

Running 'mvn -X checkstyle:checkstyle' is using the sun_checks.xml (default) instead of using the configuration in the my_checks.xml file, which can be seen by both the resulting checkstyle errors and the debug output (ex. '[DEBUG] request.getConfigLocation() config/sun_checks.xml').

I know the my_checks.xml is valid because the checkstyle.config.location can be changed in the properties using the strategy outlined by Carboni in a previous stack overflow post, but this causes issues when moving to multi-module projects and differs from the 'official' apache maven checkstyle instructions.

Best Answer

Yeah I've found the guidelines on the apache site out of date. Through trawling the internet and scrapping bits together I've managed to work this out.

Moving to the <build> tag worked for a while but when updated to the latest checkstyle it was being ignored again. I've found I had to set a property:

<project ...>

 ....

  <properties>
    <checkstyle.config.location>properties/checkstyle-configuration.xml</checkstyle.config.location>
  </properties>

  <build>
    ...

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-checkstyle-plugin</artifactId>
        <version>2.9.1</version>
      </plugin>
    </plugins>
  </build>

  ...

</project>

This is part of my example of how to create your own checkstyle custom check found here:
http://blog.blundellapps.com/create-your-own-checkstyle-check/

and taken from the source code here:
https://github.com/blundell/CreateYourOwnCheckStyleCheck

Related Topic