Checkstyle custom check does not work on gradle checkstyle plugin

3.3k views Asked by At

Question

I have created own custom check for checkstyle and it works on commandline and via maven checkstyle plugin. However via gradle checkstyle plugin, it occurs below error.

* What went wrong:
Execution failed for task ':my-project:checkstyleMain'.
> Unable to create Root Module: config {C:\Users\[path to my project]\build\tmp\resource\string8421659201972573805.txt}, classpath { ...many of classpathes. not "null" }.

Whenever exclude custom check from checkstyle.xml, the task works.

How to make custom check works on gradle?

Versions

  • checkstyle: 8.37
  • maven checkstyle plugin version: 3.1.2
  • gradle version: 5.6.2

Implementation

  • custom check
public class MyCheck extends AbstractCheck {
...
  • checkstyle.xml
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
          "-//Puppy Crawl//DTD Check Configuration 1.3//EN"
          "http://checkstyle.sourceforge.net/dtds/configuration_1_3.dtd">

<module name = "Checker">
    ...
    <module name="TreeWalker">
        ...
        <module name="package.to.my.check.MyCheck"/>
    </module>
</module>

Custom check class and checkstyle.xml are packaged into an artifact named "mycheck-module".

  • pom.xml (It works)
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-checkstyle-plugin</artifactId>
          <version>3.1.2</version>
          <dependencies>
            <dependency>
              <groupId>com.puppycrawl.tools</groupId>
              <artifactId>checkstyle</artifactId>
              <version>8.37</version>
            </dependency>
            <dependency>
              <groupId>package.to.my.check.module</groupId>
              <artifactId>mycheck-module</artifactId>
              <version>[version]</version>
            </dependency>
          </dependencies>
          <executions>
            <execution>
              <phase>test</phase>
              <goals>
                <goal>check</goal>
              </goals>
            </execution>
          </executions>
          <configuration>
            <sourceDirectories>
              <sourceDirectory>${project.build.sourceDirectory}</sourceDirectory>
            </sourceDirectories>
            <configLocation>checkstyle.xml</configLocation>
            <enableFilesSummary>true</enableFilesSummary>
            <maxAllowedViolations>0</maxAllowedViolations>
            <violationSeverity>warning</violationSeverity>
            <consoleOutput>true</consoleOutput>
            <failOnViolation>
              false
            </failOnViolation>

            <propertyExpansion>
              checkstyleSuppressionConfigDir=${project.basedir}
            </propertyExpansion>
          </configuration>
        </plugin>
  • build.gradle (It does not work)
buildscript {
    ...
    dependencies {
        ...
        classpath 'package.to.my.check.module:mycheck-module:[version]'
    }
}

...

// Set up checkstyle
apply plugin: 'checkstyle'

def checkstyleSuppressionConfigDir = file("${rootDir}/suppressCheckstyle")

checkstyle {
    toolVersion = '8.37'
    sourceSets = [it.sourceSets.main]

    config = resources.text.fromString(getClass().getResourceAsStream('checkstyle.xml').text)
    ignoreFailures = false
    maxWarnings = 0
    maxErrors = 0

    configProperties.checkstyleSuppressionConfigDir = checkstyleSuppressionConfigDir
}
1

There are 1 answers

0
Koji Saiki On BEST ANSWER

I have resolved myself. You can add your dependency into checkstyle with [Append] in below:

  • build.gradle
buildscript {
    ...
    dependencies {
        ...
        classpath 'package.to.my.check.module:mycheck-module:[version]'
    }
}

...

// Set up checkstyle
apply plugin: 'checkstyle'

def checkstyleSuppressionConfigDir = file("${rootDir}/suppressCheckstyle")

// ================ [Append] ================
dependencies {
    checkstyle 'package.to.my.check.module:mycheck-module:[version]'
}
// ================ [Append] ================

checkstyle {
    toolVersion = '8.37'
    sourceSets = [it.sourceSets.main]

    config = resources.text.fromString(getClass().getResourceAsStream('checkstyle.xml').text)
    ignoreFailures = false
    maxWarnings = 0
    maxErrors = 0

    configProperties.checkstyleSuppressionConfigDir = checkstyleSuppressionConfigDir
}