Gradle中的configuration.files和configuration.copy方法有什么区别?

What is the difference between the configuration.files and configuration.copy methods in Gradle?

在 Gradle 用户指南 Chapter 23. Dependency Management 中,一个示例显示了 Configuration.copy 和 Configuration.files 方法之间的区别:

build.gradle

task copyVsFiles << {
    configurations.sealife.copyRecursive { dep -> dep.name == 'orca' }
        .each { file -> println file.name }
    println()
    configurations.sealife.files { dep -> dep.name == 'orca' }
        .each { file -> println file.name }
}

Output of gradle -q copyVsFiles
> gradle -q copyVsFiles
orca-1.0.jar
seal-1.0.jar

orca-1.0.jar
seal-2.0.jar

下面的解释让我很困惑。我仍然不知道有什么区别。谁能帮我解决这个问题?

您问题的答案在 Gradle API 文档 Chapter 23 Section 24.

中示例 23.24 之后的段落中

请注意 configurations.files 方法 returns 版本 2 的密封罐。

In the example above, orca has a dependency on seal-1.0 whereas shark has a dependency onseal-2.0. The original configuration has therefore a version conflict which is resolved to the newer seal-2.0 version. The files method therefore returns seal-2.0 as a transitive dependency of orca. The copied configuration only has orca as a dependency and therefore there is no version conflict and seal-1.0 is returned as a transitive dependency.

要真正理解为什么会发生这种情况,请花一些时间查看 Gradle docs 中的配置方法。

Files 执行以下操作:

Resolves this configuration. This locates and downloads the files which make up this configuration. But only the resulting set of files belonging to the subset of dependencies specified by the dependencySpec is returned.

复制执行以下操作:

Creates a copy of this configuration that only contains the dependencies directly in this configuration (without contributions from superconfigurations). The new configuration will be in the UNRESOLVED state, but will retain all other attributes of this configuration except superconfigurations. Configuration.getHierarchy() for the copy will not include any superconfigurations.

这如何应用于文档示例? Configuration.files 解析配置,如示例中的输出所示,它处理由 shark 引入的依赖于 seal-2.0.jar 的版本冲突。 Configuration.copy 创建配置的副本并且尚未解决,这意味着它还没有版本冲突。