stefaan dutry
stefaan dutry

Reputation: 1106

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.

Upvotes: 12

Views: 19731

Answers (4)

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>

Upvotes: 0

Sander Verhagen
Sander Verhagen

Reputation: 9118

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>

Upvotes: 19

Naxos84
Naxos84

Reputation: 2028

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"

Upvotes: 5

The Gilbert Arenas Dagger
The Gilbert Arenas Dagger

Reputation: 12731

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>

Upvotes: 2

Related Questions