【问题标题】:gradle checkstyle error:Expected file collection to contain exactly one file, however, it contains 14 filesgradle checkstyle 错误:预期的文件集合只包含一个文件,但是,它包含 14 个文件
【发布时间】:2020-03-03 18:27:32
【问题描述】:

我正在使用带有 Gradle 的 Java 8 并尝试将 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')
}

【问题讨论】:

    标签: gradle build.gradle checkstyle


    【解决方案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 }
    }
    

    【讨论】:

      【解决方案2】:

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

      这是我使用的配置:

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

      【讨论】:

        【解决方案3】:

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多