You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Gradle Kotlin DSL 按项目groupId筛选复制项目依赖的正确实现方法

正确实现方式

你之前的写法不生效的核心原因是include闭包的过滤对象是文件路径,而非依赖的元数据,你需要先对runtimeClasspath的解析结果做依赖维度的过滤,再取对应文件传入Sync任务。

方案1:按groupId过滤(适配你的原始需求)

可以匹配所有groupId和当前项目一致的依赖,包含同groupId的外部发布组件:

tasks.register<Sync>("copyResources") {
    val targetGroup = project.group.toString()
    from(configurations.runtimeClasspath.get().filter { file ->
        val artifact = configurations.runtimeClasspath.get().resolvedConfiguration.resolvedArtifacts.find { it.file == file }
        artifact?.moduleVersion?.id?.group == targetGroup
    })
    into(layout.buildDirectory.dir("extraResources"))
}

方案2:仅过滤项目内子模块依赖(更精准)

如果只需要当前项目内的其他子模块依赖,不需要匹配groupId,可以直接通过组件类型判断,完全排除外部依赖:

import org.gradle.api.artifacts.component.ProjectComponentIdentifier

tasks.register<Sync>("copyResources") {
    from(configurations.runtimeClasspath.get().resolvedConfiguration.resolvedArtifacts
        .filter { it.id.componentIdentifier is ProjectComponentIdentifier }
        .map { it.file }
    )
    into(layout.buildDirectory.dir("extraResources"))
}

内容的提问来源于stack exchange,提问作者Kiran Mohan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 10:15:03