Checkstyle checker cannot be created with Gradle 3.5

plugins

#1

I just changed my Gradle distribution from 3.3 to 3.5 and I am now experiencing errors with the Checkstyle plugin which worked without errors before the upgrade.

The error when running ./gradlew clean checkstyleMain:

:clean
:compileJava
:processResources NO-SOURCE
:classes
:checkstyleMain FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':checkstyleMain'.
> Unable to create a Checker: configLocation {/home/me/repo/repo01/config/checkstyle/checkstyle.xml}, classpath {/home/me/repo/repo01/build/classes/main:/home/me/repo/repo01/build/resources/main}.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

The Checkstyle part of my build.gradle file:

apply plugin: 'checkstyle'
tasks.withType(Checkstyle) {
    reports {
        xml.enabled false
        html.enabled true
        html.stylesheet resources.text.fromFile('config/checkstyle/checkstyle-custom.xsl')
    }
}

The files config/checkstyle/checkstyle.xml and config/checkstyle/checkstyle-custom.xml exist of course.


(Benjamin Muschko) #2

Could you please provide a sample project that reproduces the issue? I am assuming the version of Checkstyle we upgraded to changed some behavior internally: https://docs.gradle.org/3.5/release-notes.html#version-of-checkstyle-has-been-upgraded.


#3

Tried to break it down as far as possible.

build.gradle:

apply plugin: 'java'
apply plugin: 'application'
repositories {
    jcenter()
}
mainClassName = 'Test'
apply plugin: 'checkstyle'

src/main/java/Test.java:

public class Test {
    public static void main(String[] args) {
        System.out.println("Hello world.");
    }
}

config/checkstyle/checkstyle.xml:

<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
    "-//Puppy Crawl//DTD Check Configuration 1.3//EN"
    "http://www.puppycrawl.com/dtds/configuration_1_3.dtd">

<module name = "Checker">
    <module name="TreeWalker">        
        <module name="OperatorWrap">
            <property name="option" value="nl"/>
            <property name="tokens" value="METHOD_REF"/>
        </module>
    </module>
</module>

(James Justinic) #4

The specific problem here is that OperatorWrap did not support the METHOD_REF token until Checkstyle 7.2. Technically this configuration file is invalid unless you specify Checkstyle version 7.2 or higher.

With Gradle 3.3 (default Checkstyle 5.9), that configuration was still not correct, but you did not receive an error due to looser validation (in Checkstyle, not Gradle). That version only failed on tokens that were completely unknown, not those that were only valid for other rules.


(René Groeschke) #5

to unblock you from upgrading, you can specify the checkstyle version explicitly:

apply plugin:"checkstyle"
...
...
checkstyle {
    toolVersion = "5.9"
}```

#6

Ok, after adding the current release of Checkstyle to the buildfile, everything runs without errors now.

checkstyle {
    toolVersion "7.6.1"
}

(Erisan) #7

I’m having a similar issue but was not solved by

    toolVersion "7.6.1"
}```

#8

Just tried your Gradle build file (but removed the Springframework) with my XML shown in my other post. There had been a missing quotation mark before the value of the tokens property (fixed now), after this everything runs fine. Does this resolve your problem with my XML config?


#9

Just had a look on the Google Checks file you want to use. The error seems to occur in line 98, means that SeparatorWrap seems to have a problem with the tokens property set to METHOD_REF.

While the docs say that the METHOD_REF token is a valid one, the API docs do not mention it.


(Erisan) #10

Thanks, it works now with

checkstyle {
	toolVersion "7.6.1"
}

and your XML. I also tried a different XML. Nice.