Search code examples
mavenpluginsconfigurationcheckstyle

how to externalise the checkstyle config for maven-checkstyle-plugin


I'm trying to make the maven-checkstyle-plugin use the same config file for all our projects.

I've tried a couple of ways, but non of them was effective.

The only thing that seems to work is when i place the config file at the root of my maven-project and then use the name as configLocation configuration parameter in the pom.xml

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

                <configuration>
                    <configLocation>my-checkstyle-checker.xml</configLocation>
                </configuration>
            </plugin>

I've tried specifying an absolute disk-path, but that doesn't seem to work. (Considering the endgoal is to have jenkins do the checkstyle this seemed a valid option if the file would be on the jenkins server at the specified location)

I've also tried making a seperate jar-file only containing the xml-file and then using this as a dependency. (This would also centralise the config in 1 location and prevent project specific deviations.) Unfortunately this also doesn't work.

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-checkstyle-plugin:2.10:checkstyle (default-cli) on project jenkins-sandbox-project: An error has occurred in Checkstyle report generation. Failed during checkstyle execution: Unable to find configuration file at location my-checkstyle-checker.xml: Could not find resource 'my-checkstyle-checker.xml'. -> [Help 1]

Is there anyone that can tell me what i'm doing wrong here?

It seems it only knows about the files in the same location as where the maven command was started.

  • maven-checkstyle-plugin version : 2.10
  • maven command : mvn checkstyle:checkstyle

Solution

  • Create a separate Maven project, that contains just the Checkstyle configuration. In my case I called this project checkstyle-config and it contains the following:

    checkstyle-config/src/main/resources/checkstyle.config.xml
    checkstyle-config/src/main/resources/checkstyle.suppressions.xml
    checkstyle-config/pom.xml
    

    The POM file for this project is trivial:

    <?xml version="1.0" encoding="UTF-8"?>
    <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/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.totaalsoftware.incidentmanager</groupId>
      <artifactId>checkstyle-config</artifactId>
      <version>2.0.0-SNAPSHOT</version>
    </project>
    

    Build it, so that it gets installed. Then use it as a dependency for your Checkstyle execution, e.g.:

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-checkstyle-plugin</artifactId>
        <executions>
          <execution>
            <goals>
              <goal>check</goal>
            </goals>
          </execution>
        </executions>
        <dependencies>
          <dependency>
            <groupId>com.totaalsoftware.incidentmanager</groupId>
            <artifactId>checkstyle-config</artifactId>
            <version>2.0.0-SNAPSHOT</version>
          </dependency>
        </dependencies>
        <configuration>
          <configLocation>checkstyle.config.xml</configLocation>
          <suppressionsLocation>checkstyle.suppressions.xml</suppressionsLocation>
    
          ... other configuration ...
    
        </configuration>
      </plugin>