【问题标题】:gradle kotlin dsl: copy project dependenciesgradle kotlin dsl:复制项目依赖项
【发布时间】:2021-10-14 17:10:42
【问题描述】:

我可以复制多模块 gradle 项目中模块的所有依赖项,任务如下:

tasks.register<Sync>("copyResources") {
    from(configurations.runtimeClasspath)
    into(layout.buildDirectory.dir("extraResources"))
}

但实际上,我只需要通过应用具有项目组 ID 的过滤器来复制 project dependencies类似

tasks.register<Sync>("copyResources") {
    from(configurations.runtimeClasspath) {
        include {
            group "this.project.group"   // NOT WORKING.
        }
    }
    into(layout.buildDirectory.dir("extraResources"))
}

在 Gradle 中使用 Kotlin DSL 执行此操作的正确方法是什么?

【问题讨论】:

  • 该组函数应该来自哪里,我认为这在 groovy dsl 中甚至无效?我从未使用过同步任务,但文档中的所有示例都使用包含中的文件路径,您可能会查找所需文件的路径,添加一个过滤器 a la 'this/project/group/*'并完成它。另外include 需要一个参数,而不是 lambda,所以不要使用花括号。
  • @somethingsomething 我的要求是复制“项目依赖项”,不包括第三方依赖项。问题中的第二个代码 sn-p 仅提示我想要实现的目标,并且它不是那里已经记录的代码。

标签: kotlin gradle gradle-kotlin-dsl


【解决方案1】:

以下作品。 但我觉得必须有一个更简单的解决方案。

此解决方案使用正则表达式来过滤依赖关系。如果依赖文件的绝对路径中包含多模块项目根,则正则表达式匹配为真。

val copyImplemtations by configurations.creating {
    extendsFrom(configurations.implementation.get())
}

tasks.register<Sync>("copyResources") {
    val regex = Regex(".*[\\\\/]kotlin-spring-demo[\\\\/].*")
    from(copyImplemtations.filter { regex.matches(it.absolutePath) })
    into(layout.buildDirectory.dir("extraResources"))
}

【讨论】: