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

时间:2019-11-07 07:57:36

标签: gradle build.gradle maven-checkstyle-plugin

我正在将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')

}

可以帮忙吗?

3 个答案:

答案 0 :(得分:1)

这里的问题是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 }
}

答案 1 :(得分:0)

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

这是我使用的配置:

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

答案 2 :(得分:0)

对于任何感兴趣的人,这是来自@thiago 的配置的 Kotlin DSL 变体答案:

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