Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven Checkstyle configLocation ignored?

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.

like image 246
HeavyE Avatar asked Jan 23 '12 16:01

HeavyE


People also ask

How do you avoid a Checkstyle violation?

If you want to disable checkstyle from your pom, you can add the checkstyle plugin to the pom and set the skip flag to prevent the check.

How do I check my Checkstyle violations?

It will also generate the violation report for the project which is available as a view in Eclipse. To view to violation report, go to Window -> Show View -> Other, and search for Checkstyle. Options for Violations and Violations Chart should be displayed.


2 Answers

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

like image 56
Blundell Avatar answered Sep 17 '22 12:09

Blundell


This works for me when:

  1. I place my_checks.xml at the root level (parallel to pom.xml), and
  2. Wrap the plugin in the <build> element (as opposed to <reporting>)

With those changes, I see the following:

[DEBUG] Configuring mojo 'org.apache.maven.plugins:maven-checkstyle-plugin:2.8:checkstyle' with basic configurator -->
[DEBUG]   (f) cacheFile = /home/dave/tech/lang/java/web/struts/playground/so/s231_01/target/checkstyle-cachefile
[DEBUG]   (f) configLocation = my_checks.xml
... etc ...
[DEBUG] request.getConfigLocation() my_checks.xml
[DEBUG] The resource 'my_checks.xml' was not found with resourceLoader
... etc ...
[DEBUG] The resource 'my_checks.xml' was not found with resourceLoader org.codehaus.plexus.resource.loader.URLResourceLoader.
[DEBUG] The resource 'my_checks.xml' was not found with resourceLoader org.codehaus.plexus.resource.loader.JarResourceLoader.
[DEBUG] The resource 'my_checks.xml' was found as /home/dave/tech/lang/java/web/struts/playground/so/s231_01/my_checks.xml.

Please reference the usage documentation, specifically:

To specifically configure the Checkstyle Plugin, you need to the add it in the <build> section of your pom.xml as shown in the sample below.

The <reporting> usage is for mvn site.

like image 25
Dave Newton Avatar answered Sep 17 '22 12:09

Dave Newton