Maven Checkstyle error trying to running custom rules in Java project

142 views Asked by At

I am currently trying to integrate checkstyle into many of the pipeline of our services. The end goal is to have some centralized configuration we can run in the pipelines and our services can all depend on the same ruleset in this central location.

For now I am just running the commands in PowerShell locally and we can ignore the gitlab pipeline steps.

I have created a new repository (NCS) which is a fairly standard Maven module and the structure is as follows.

|-- NCS
|   |-- src
|       |-- main
|           |-- java
|               |-- com
|                   |-- example
|                       |-- check
|                           |-- CustomCheck.java
|   |-- src
|       |-- main
|           |-- resources
|               |-- checkstyle.xml
|   |-- .gitlab-ci.yml
|   |-- pom.xml

My CustomCheck is very straightforward and I just wish to run this rule for now I can implement logic later.

My pom.xml is as follows (simplified a couple of other dependencies and a parent)

<?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.example</groupId>
    <artifactId>ncs</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>com.puppycrawl.tools</groupId>
            <artifactId>checkstyle</artifactId>
            <version>10.12.6</version>
        </dependency>
    </dependencies>


    <build>
        <plugins>
            <!-- Configure Checkstyle Plugin -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-checkstyle-plugin</artifactId>
                <version>3.3.1</version>
                <executions>
                    <!-- Disable Checkstyle during default build phases -->
                    <execution>
                        <id>checkstyle</id>
                        <goals>
                            <goal>check</goal>
                        </goals>
                        <configuration>
                            <skip>true</skip>
                        </configuration>
                    </execution>
                </executions>
                <configuration>
                    <failOnViolation>true</failOnViolation>
                    <configLocation>src/main/resources/checkstyle.xml</configLocation>
                </configuration>
            </plugin>
        </plugins>
    </build>



</project>

I can run mvn clean install successfully and can see a JAR in my local .m2 called ncs-1.0.0-SNAPSHOT.jar

However when I run the command mvn checkstyle:check I receive the following error.

[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.047 s
[INFO] Finished at: 2023-12-22T10:21:29Z
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-checkstyle-plugin:3.3.1:check (default-cli) on project ncs: Failed during checkstyle configuration: cannot initialize module TreeWalker - cannot initialize module com.example.check.CustomCheck - Unable to instantiate 'com.example.check.CustomCheck' class, it is also not possible to instantiate it as null. Please recheck that class name is specified as canonical name or read how to configure short name usage https://checkstyle.org/config.html#Packages. Please also recheck that provided ClassLoader to Checker is configured correctly. -> [Help 1]

I would expect that this rule should run and I should see 1 violation with my custom error.

I can't seem to figure this out any advice would be much appreciated.

My checkstyle.xml is a list of rules I have configured (I won't post the full thing here but it is successful when I am not running my custom rule as I can see a violation I have added for testing, no new line at end of a file)

<!DOCTYPE module PUBLIC
        "-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
        "https://checkstyle.org/dtds/configuration_1_3.dtd">

<module name="Checker">
    .......
    <module name="TreeWalker">
        <module name="com.example.package.CustomCheck"/>
         .......
    </module>
</module>

I originally followed the online documentation and was unable to get their rule to run- https://checkstyle.org/writingchecks.html#Writing_Checks

So I created in my eyes a simpler rule and simplified my project to have a rule that just always fails with a clear message.

I have tried refactoring this project so checks are now in a separate module, and still receive the same error. When I comment out my custom check in the xml the mvn checkstyle:check runs successfully

I've also tried tweaking the versions of the puppy crawl dependency and the maven plug in but no luck.

1

There are 1 answers

0
Rob Spoor On

Unless I'm mistaken, you're trying to use a check from the project itself when calling Checkstyle on the project. I don't think that's possible. For one, the class may not even be compiled yet (my projects run Checkstyle all the way at the start in the initialize stage). Also, it's not on the class/module path when Checkstyle is executed.

Splitting the checks to a separate module can work, but you need to make some changes to your POM.

First, the Checkstyle module must use a different Checkstyle configuration that doesn't include your custom check (or even omit running Checkstyle).

Secondly, you need to tell the Checkstyle plugin that you want to use your module as a dependency. For instance:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-checkstyle-plugin</artifactId>
  <version>...</version>
  <executions>
    ...
  </executions>
  <configuration>
    ...
  </configuration>
  <dependencies>
    <!-- Optional, to use a version different from the default, which is currently 9.3 -->
    <dependency>
      <groupId>com.puppycrawl.tools</groupId>
      <artifactId>checkstyle</artifactId>
      <version>10.12.6</version>
    </dependency>
    <dependency>
      <!-- Assuming the Checkstyle module uses the same groupId and version -->
      <groupId>${project.groupId}</groupId>
      <artifactId>checkstyle-module</artifactId>
      <version>${project.version}</version>
    </dependency>
  </dependencies>
</plugin>