12

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

4 Answers 4

19

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>
4
  • ah, you need to add the jar as a dependency to the plugin and not to your project, that might explain it. I'll try that and see if it works. Nov 1, 2013 at 10:35
  • This suggestion saved my day. Thanks
    – ssasi
    Jul 24, 2018 at 15:38
  • I don't know why that got changed, I'm trying to change it back. Apr 17, 2020 at 18:15
  • For me, it was as simple as a version mismatch in parent and child pom(s).
    – S S
    Jun 22, 2021 at 12:45
5

I had a similar problem. I solved it with the following configuration.

${project.basedir}/src/main/resources/checkstyle.xml

Note: my checkstyle file is located in "src/main/resources"

0
2

I also had some issues defining the location in my plugin configuration, but was able to get this working by overriding a Maven property that the plugin uses, checkstyle.config.location. See example below which works with a multi-module maven project and requires very little overhead.

<checkstyle.config.location>${project.parent.basedir}/my_checks.xml</checkstyle.config.location>
0

On my case the order of dependencies is the key , this is my pom

<dependency>
    <groupId>org.geotools</groupId>
    <artifactId>gt-epsg-wkt</artifactId>
    <version>${geotools.version}</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.geotools</groupId>
    <artifactId>gt-geometry</artifactId>
    <version>${geotools.version}</version>
</dependency>
<dependency>
    <groupId>org.geotools</groupId>
    <artifactId>gt-swing</artifactId>
    <version>${geotools.version}</version>
</dependency>
<dependency>
    <groupId>org.geotools</groupId>
    <artifactId>gt-shapefile</artifactId>
    <version>${geotools.version}</version>
</dependency>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.