gradle checkstyle错误:预期文件集合只包含一个文件,但它包含14个文件

gradle checkstyle错误:预期文件集合只包含一个文件,但它包含14个文件,gradle,build.gradle,checkstyle,Gradle,Build.gradle,Checkstyle,我将Java 8与Gradle一起使用,并试图将Google checkstyle规则添加到构建中,但我得到的是以下错误: “预期文件集合只包含一个文件,但它包含14个文件。” 我的配置是: apply plugin: 'checkstyle' configurations { checkstyleConfig } def versions = [ checkstyle: '8.8', ] dependencies { checkstyleConfig "com.puppycrawl

我将Java 8与Gradle一起使用,并试图将Google checkstyle规则添加到构建中,但我得到的是以下错误:

“预期文件集合只包含一个文件,但它包含14个文件。”

我的配置是:

apply plugin: 'checkstyle'
configurations {
  checkstyleConfig
}
def versions = [
  checkstyle: '8.8',
]
dependencies {
  checkstyleConfig "com.puppycrawl.tools:checkstyle:${versions.checkstyle}"
}
checkstyle {
  toolVersion = "${versions.checkstyle}"
  config = resources.text.fromArchiveEntry(configurations.checkstyleConfig, 'google_checks.xml')
}

这里的问题是,
configurations.checkstyleConfig
包括多个JAR文件:
com.puppycrawl.tools:checkstyle
,以及它的所有可传递依赖项。在本地调试该问题时,我看到包括以下依赖项:

antlr:antlr:2.7.7
com.google.code.findbugs:jsr305:1.3.9
com.google.errorprone:error_prone_annotations:2.1.3
com.google.guava:guava:23.6-jre
com.google.j2objc:j2objc-annotations:1.1
com.puppycrawl.tools:checkstyle:8.8
commons-beanutils:commons-beanutils:1.9.3
commons-cli:commons-cli:1.4
commons-collections:commons-collections:3.2.2
commons-logging:commons-logging:1.2
net.sf.saxon:Saxon-HE:9.8.0-7
org.antlr:antlr4-runtime:4.7.1
org.checkerframework:checker-compat-qual:2.0.0
org.codehaus.mojo:animal-sniffer-annotations:1.14
幸运的是,解决方法非常简单。您只需将可传递依赖项从Checkstyle依赖项中排除,脚本的其余部分将按照您希望的方式工作:

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

顺便说一句,为了将来的参考,不需要添加新的配置来使用它,只需从plgin使用的现有配置中过滤checkstyle依赖项

这是我使用的配置:

checkstyle {
  config = resources.text.fromArchiveEntry(
    configurations.checkstyle.find { it.name.contains('checkstyle') },
    'google_checks.xml'
  )
}