【问题标题】:Gradle exclude module for Copy task复制任务的 Gradle 排除模块
【发布时间】:2018-01-23 09:55:39
【问题描述】:

我有一个复制任务集如下:

task copyToLib( type: Copy ) {
   into "$buildDir/myapp/lib"
   from configurations.runtime

   // We only want jars files to go in lib folder
   exclude "*.exe"
   exclude "*.bat"
   exclude "*.cmd"
   exclude "*.dll"

   // We exclude some lib
   exclude group: "org.slf4j", name: "slf4j-api", version: "1.6.2"
}

我收到以下错误:

Could not find method exclude() for arguments [{group=org.slf4j, name=slf4j-api, version=1.6.2}] on task ':copyToLib' of type org.gradle.api.tasks.Copy

我觉得这只是一个语法问题,有什么提示吗?

【问题讨论】:

  • Copy 用于复制文件。所以你可以排除文件。你正在传递一张地图。以下是您可以使用的各种排除方法的文档:docs.gradle.org/current/dsl/…
  • 非常感谢。因此,如果我理解得很好,以下应该可以解决问题: exclude {it.file in configurations.runtime.files {it.name.equals("slf4j-api")}} 。我不再收到任何错误,但仍包含资源...
  • 没有。它应该看起来像 exclude "slf4j-api.jar"exclude { it.file.name.contains('slf4j-api') }。运行时配置中没有名为 slf4j-api 的文件。
  • 文件名可能是“slf4j-api-1.6.2.jar”
  • 只有在按组排除或使用闭包时才应排除组。

标签: java gradle copy build.gradle


【解决方案1】:

按组排除:exclude group: org.slf4j

按模块排除:exclude module: slf4j-api

按文件名排除:exclude { it.file.name.contains('slf4j-api') }

排除文件:exclude "slf4j-api.jar"

您可以按组和模块排除,但需要像这样进入配置排除。然后它会在复制之前限制配置。

task copyToLib( type: Copy ) {
    into "$buildDir/myapp/lib"
    from configurations.runtime {
        exclude group: 'org.slf4j'
    }

    // We only want jars files to go in lib folder
    exclude "*.exe"
    exclude "*.bat"
    exclude "*.cmd"
    exclude "*.dll"

}

记得确保目录存在$buildDir/myapp/lib

也许不是排除所有其他文件,而是只包含 jars?

【讨论】:

  • 感谢 LazerBanana,它就像一个魅力!是的,100% 同意,包含而不是排除会更好。干杯!!
【解决方案2】:

也许写一个方法来帮助你

static String generateExcludeJar(Map<String, String> map) {
    "$map.name-${map.version}.jar" 
   // All jar names are like name-version.jar when downloaded by Gradle.
}

exclude generateExcludeJar(group: "org.slf4j", name: "slf4j-api", version: "1.6.2")

【讨论】:

  • 谢谢@aristotll。是的,这会起作用,但对我来说最好的方法是根据其组或 artifatId 排除 jar。但是,正如 Jb Nizet 所提到的,Copy(据我所知)对文件起作用,所以也许你的建议实际上是唯一的可能性。
猜你喜欢
  • 2014-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多