Checkstyle: How to use an official style configuration?

Is there any way to use the style configurations that are packaged with the checkstyle jar?

For example on the command line,

java -jar checkstyle-6.1.1-all.jar -c sun_checks.xml MyClass.java

In my case I’d like to reference the google_checks.xml instead of copying it locally and missing updates from the official configuration.

A new feature was added in 2.2 that can help with this.

http://gradle.org/docs/2.2/release-notes#sharing-configuration-files-across-builds

Perfect! Except sadly it appears that the checkstyle documentation is wrong, and the jars don’t include the official configurations. Off to bug them. :slight_smile:

Thanks!

$ java -jar checkstyle-6.1.1-all.jar -c sun_checks.xml MyClass.java
Error loading configuration file
com.puppycrawl.tools.checkstyle.api.CheckstyleException: unable to find sun_checks.xml
 at com.puppycrawl.tools.checkstyle.ConfigurationLoader.loadConfiguration(ConfigurationLoader.java:342)
 at com.puppycrawl.tools.checkstyle.ConfigurationLoader.loadConfiguration(ConfigurationLoader.java:286)
 at com.puppycrawl.tools.checkstyle.Main.loadConfig(Main.java:232)
 at com.puppycrawl.tools.checkstyle.Main.main(Main.java:105)
Caused by: java.io.FileNotFoundException: sun_checks.xml
 at com.puppycrawl.tools.checkstyle.ConfigurationLoader.loadConfiguration(ConfigurationLoader.java:328)
 ... 3 more

Somewhat unrelated, but could a ‘TextResource’ be used to distribute script plugins. For example:

apply from: resources.text.fromArchiveEntry(configurations.myConfig, “path/to/plugin.gradle”)

Today you’d have to do…

’’‘
apply from: resources.text.fromArchiveEntry(configurations.myConfig, “path/to/plugin.gradle”).asFile()
’’'

That should work, but you are paying the price of unpacking the file opposed to reading it directly from the stream though (as it probably should)


Checkstyle 6.2 now includes the Google and Sun style checks in the jar. However, when I run with your suggestion I receive the error,

FAILURE: Build failed with an exception.
  * What went wrong:
Expected file collection to contain exactly one file, however, it contains 9 files.
apply plugin: 'checkstyle'
  configurations {
  checkstyleConfig
}
  def versions = [
  checkstyle: '6.2',
]
  dependencies {
  checkstyleConfig "com.puppycrawl.tools:checkstyle:${versions.checkstyle}"
}
  checkstyle {
  toolVersion = "${versions.checkstyle}"
  config = resources.text.fromArchiveEntry(configurations.checkstyleConfig, 'google_checks.xml')
}
1 Like

Oh, I think the problem is because of transitive dependencies. That resolves into 9 files.

checkstyleConfig
\--- com.puppycrawl.tools:checkstyle:6.2
     +--- antlr:antlr:2.7.7
     +--- org.antlr:antlr4-runtime:4.3
     |
  +--- org.abego.treelayout:org.abego.treelayout.core:1.0.1
     |
  \--- org.antlr:antlr4-annotations:4.3
     +--- commons-beanutils:commons-beanutils-core:1.8.3
     |
  \--- commons-logging:commons-logging:1.1.1
     +--- commons-cli:commons-cli:1.2
     \--- com.google.guava:guava:18.0

Setting ‘transitive = false’ on the dependency resolved the issue.