(Solved) Gradle checkstyle plugin with sventu checkstyle extensions

Hi all, Im having some trouble activating checkstyle with the additional sventu checks in my gradle build process. It looks like the build process fails to see my added checks library. Below is the relevant extract of my build script:

checkstyle {
    repositories {
      mavenCentral()
      maven {
          url "http://sevntu-checkstyle.github.com/sevntu.checkstyle/maven2"
      }
   }
    
    configFile = new File("etc/config/dev.xml");
    toolVersion = "6.8"
    
    configurations {
      checkstyle
   }

    dependencies {
        checkstyle "com.github.sevntu.checkstyle:sevntu-checks:1.13.4"
    }
}

When I build, I get the following error:

:checkstyleMain FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':checkstyleMain'.
> Unable to create a Checker: cannot initialize module TreeWalker - Unable to instantiate     AvoidConstantAsFirstOperandInConditionCheck

So it looks like my dependecy is not picked up. It is however in my local repository and the jar does contain the AvoidConstantAsFirstOperandInConditionCheck class.

Im using Gradle 2.4. Any ideas what the problem might be please?

Your repositories, configurations, dependencies blocks should be outside the checkstyle block.

Thanks you for your response. This is what I have tried first, but then the process fails as well. Changing the script to:

repositories {
      mavenCentral()
      maven {
          url "http://sevntu-checkstyle.github.com/sevntu.checkstyle/maven2"
      }
   }


checkstyle {
    configFile = new File("etc/config/dev.xml");
    toolVersion = "6.8"
}

configurations {
    checkstyle
}

dependencies {  
    checkstyle "com.github.sevntu.checkstyle:sevntu-checks:1.13.4"
}

generates the same error:

  • What went wrong:
    Execution failed for task ‘:checkstyleMain’.
    Unable to create a Checker: cannot initialize module TreeWalker - Unable to instantiate AvoidConstantAsFirstOperandInConditionCheck

So I finally figured it out:

Turns out the example at https://github.com/sevntu-checkstyle/checkstyle-samples/blob/master/gradle-project/build.gradle only works if you put the full classpath of the custom checks as the name for each custom check.

This is due to checkstyle not knowing where the custom checks are located in the package. Turns out checkstyle can find this out if you include a checkstyle_packages.xml file in the jar that describes the packages that contains the checks.

Unfortunately there is no such file in com.github.sevntu.checkstyle:sevntu-checks:1.13.4. To get this information, you also need to include “com.github.sevntu.checkstyle:sevntu-checkstyle-maven-plugin:1.13.4”, which basically contains nothing but the checkstyle_packages.xml file.

So I’ve added this to my dependencies and the checkstyle rules finally parses:

dependencies {
    checkstyle "com.github.sevntu.checkstyle:sevntu-checks:1.13.4",
               "com.github.sevntu.checkstyle:sevntu-checkstyle-maven-plugin:1.13.4"
}

Hoping this will save someone some pain in future :smile: