Piotrek Hryciuk
Piotrek Hryciuk

Reputation: 805

How to compile project with Google Checkstyle rules with gradle?

I am trying to use Google checkstyle configuration (https://github.com/checkstyle/checkstyle/blob/master/src/main/resources/google_checks.xml) but I am constantly getting an error on gradle check:

Unable to create a Checker: cannot initialize module TreeWalker - Unable to instantiate EmptyCatchBlock

I used Gradle to build the project. Below is my gradle.build.

apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'checkstyle'

sourceCompatibility = 1.8
version = '1.0'

checkstyle {
    toolVersion = "6.3"
}

task "create-dirs" << {
   sourceSets*.java.srcDirs*.each { it.mkdirs() }
   sourceSets*.resources.srcDirs*.each { it.mkdirs() }
}

jar {
    manifest {
        attributes 'Implementation-Title': 'xyz',
                   'Implementation-Version': 0.01
    }
}

repositories {
    mavenCentral()
}

dependencies {
    compile (
        ['org.apache.logging.log4j:log4j-api:2.2'],
        ['org.apache.logging.log4j:log4j-core:2.2']
    )
    testCompile(
        ['junit:junit:4.11'],
        ['org.mockito:mockito-core:1.+']
    )
}

test {
    systemProperties 'property': 'value'
}

uploadArchives {
    repositories {
       flatDir {
           dirs 'repos'
       }
    }
}

Also, when I try to add XML config file to Checkstyle plugin in IDEA I get similar error but with a stack trace:

org.infernus.idea.checkstyle.exception.CheckStylePluginException: <html><b>The CheckStyle rules file could not be loaded.</b><br>cannot initialize module TreeWalker - Unable to instantiate EmptyCatchBlock</html>
    at org.infernus.idea.checkstyle.checker.CheckerFactory.blacklistAndShowMessage(CheckerFactory.java:234)
    at org.infernus.idea.checkstyle.checker.CheckerFactory.createChecker(CheckerFactory.java:188)
    at org.infernus.idea.checkstyle.checker.CheckerFactory.getOrCreateCachedChecker(CheckerFactory.java:98)
    at org.infernus.idea.checkstyle.checker.CheckerFactory.getChecker(CheckerFactory.java:73)
    at org.infernus.idea.checkstyle.checker.CheckerFactory.getChecker(CheckerFactory.java:41)

I cannot figure out what am I doing wrong. Any help would be appreciated. Gradle version: 2.2

Upvotes: 6

Views: 7237

Answers (3)

M. Justin
M. Justin

Reputation: 21162

Here is an approach that works with the (currently) latest versions of Gradle & Checkstyle (Gradle 6.1.1 & Checkstyle 8.29):

plugins {
    id 'java'
    id 'checkstyle'
}

configurations {
    checkstyleConfig
}

dependencies {
    checkstyleConfig("com.puppycrawl.tools:checkstyle:8.29") { transitive = false }
}

checkstyle {
    toolVersion '8.29'
    config = resources.text.fromArchiveEntry(configurations.checkstyleConfig, 'google_checks.xml')
}

Note that the Checkstyle dependency excludes transitive dependencies, otherwise the resources.text.fromArchiveEntry will fail since multiple JAR files will be present, and it will be unable to select a single one.

Upvotes: 1

sancho21
sancho21

Reputation: 3643

You can add this configuration into your build.gradle file:

configurations {
  checkstyleOverride
}

dependencies {
  checkstyleOverride('com.puppycrawl.tools:checkstyle:6.11.2')
}

tasks.withType(Checkstyle) {
  checkstyleClasspath = project.configurations.checkstyleOverride
}

Enjoy!

Upvotes: 5

Opal
Opal

Reputation: 84784

The problem lies in the fact that com.puppycrawl.tools.checkstyle.checks.blocks.EmptyCatchBlockCheck was indeed added to checkstyle but for version 6.4-SNAPSHOT. As it can be seen in checkstyle repository (pom.xml history) version 6.4-SNAPSHOT was introduced on the 02.02.2015 and EmptyCatchBlockCheck class was created on 18.02.2015.

Gradle still uses version 6.3 as in the following log extract:

:checkstyleMain
Download https://repo1.maven.org/maven2/com/puppycrawl/tools/checkstyle/6.3/checkstyle-6.3.pom

So there's simply no class You'd like to use.

According to the docs checkstyle classpath can be specified with checkstyleClasspath property - you can try to set it up manually.

I've also prepared a demo with 6.4-SNAPSHOT version, it can be found here. Checkstyle jar was built with mvn clean package with source taken from this repo.

Upvotes: 4

Related Questions