Checkstyle rules in separate jar file


(Marcin Cylke) #1

I’m trying to force checkstyle plugin to use xml configuration stored in different jar file (as a dependency).

I’ve created a package that contains just that config file.

In build.gradle I have the following config:

apply plugin: 'checkstyle'
  checkstyle {
    checkstyleMain {
        classpath += configurations.checkstyleDep
    }
    checkstyleTest {
        classpath += configurations.checkstyleDep
    }
    configFile = new File(getClass().getResource("/checkstyle.xml").toURI())
    println(configFile)
}
  configurations {
    checkstyleDep
}
  dependencies {
    checkstyleDep "com.example:checkstyle-common:1.0"
}

Unfortunately this does not cause gradle to see the dependency.


(Justin Ryan) #2

Gradle’s checkstyle plugin’s configFile literally takes a File and not a URI. The underlying ant task does take a URI, but that’s not exposed. Maybe a JIRA can be created. To get around this, you have to write out the contents of the resource, to a temp directory, then specify that as the configFile.


(Marcin Cylke) #3

Okey, this may be acceptable solution, but still, I can’t get the resource without the dependency available.

So there still is a problem about resolving dependencies for checkstyle plugin. How to solve this problem?


(Justin Ryan) #4

With guava, this is how I copy out of the classpath:

def resourceUrl = Resources.getResource(resourcePath)
      def supplier = Resources.newInputStreamSupplier(resourceUrl)
      Files.copy(supplier, to)

Concerning getting the file into the classpath, you can do this by putting it in buildSrc or bundling it with a plugin. E.g. I have a netflix-checkstyle plugin who’s sole purpose is to bundle the checkstyle.xml file and then write it out to a temp directory if configFile is not set by the user.


(Marcin Cylke) #5

Hi, the best thing in my situation looks like this one:

http://java.dzone.com/articles/gradle-goodness-init-script

It works fine with that.


(Luke Daley) #6

Justin, I seem to recall you raising a problem report about this earlier. Do you have a link handy?


(Justin Ryan) #7

http://issues.gradle.org/browse/GRADLE-2800