【问题标题】:Get list (not tree) of dependencies with Gradle使用 Gradle 获取依赖项列表(不是树)
【发布时间】:2020-06-30 21:10:15
【问题描述】:

我有一个具有复杂依赖关系的 Gradle 项目,我可以使用 ./gradlew module-name:dependencies 看到它:

[...]
testRuntimeClasspath - Runtime classpath of source set 'test'.
+--- org.apache.logging.log4j:log4j-core -> 2.11.0
|    \--- org.apache.logging.log4j:log4j-api:2.11.0
+--- com.google.guava:guava -> 20.0
+--- project :xxx
|    +--- org.springframework:spring-tx -> 5.1.7.RELEASE
|    |    +--- org.springframework:spring-beans:5.1.7.RELEASE
|    |    |    \--- org.springframework:spring-core:5.1.7.RELEASE
|    |    |         \--- org.springframework:spring-jcl:5.1.7.RELEASE
|    |    \--- org.springframework:spring-core:5.1.7.RELEASE (*)
|    +--- org.springframework:spring-context -> 5.1.7.RELEASE
|    |    +--- org.springframework:spring-aop:5.1.7.RELEASE
|    |    |    +--- org.springframework:spring-beans:5.1.7.RELEASE (*)
|    |    |    \--- org.springframework:spring-core:5.1.7.RELEASE (*)
|    |    +--- org.springframework:spring-beans:5.1.7.RELEASE (*)
|    |    +--- org.springframework:spring-core:5.1.7.RELEASE (*)
|    |    \--- org.springframework:spring-expression:5.1.7.RELEASE
|    |         \--- org.springframework:spring-core:5.1.7.RELEASE (*)
[...]

当我排除嵌套依赖并明确包含它们时,结果可能如下所示:

[...]
testRuntimeClasspath - Runtime classpath of source set 'test'.
+--- org.apache.logging.log4j:log4j-core -> 2.11.0
|    \--- org.apache.logging.log4j:log4j-api:2.11.0
+--- com.google.guava:guava -> 20.0
+--- project :xxx
|    +--- org.springframework:spring-context -> 5.1.7.RELEASE
|    |    +--- org.springframework:spring-aop:5.1.7.RELEASE
|    |    |    +--- org.springframework:spring-beans:5.1.7.RELEASE (*)
|    |    |    \--- org.springframework:spring-core:5.1.7.RELEASE (*)
|    |    +--- org.springframework:spring-beans:5.1.7.RELEASE (*)
|    |    +--- org.springframework:spring-core:5.1.7.RELEASE (*)
|    |    \--- org.springframework:spring-expression:5.1.7.RELEASE
|    |         \--- org.springframework:spring-core:5.1.7.RELEASE (*)
+--- org.springframework:spring-tx -> 5.1.7.RELEASE
|    +--- org.springframework:spring-beans:5.1.7.RELEASE
|    |    \--- org.springframework:spring-core:5.1.7.RELEASE
|    |         \--- org.springframework:spring-jcl:5.1.7.RELEASE
|    \--- org.springframework:spring-core:5.1.7.RELEASE (*)
[...]

为了查看 set 的依赖关系是否发生了变化,我想比较 lists 的依赖关系。换句话说,我想展平上面显示的树视图,以便我可以专注于与传递包含无关的差异。

有了 Maven,我可以做到 mvn dependency:treemvn dependency:list。我如何使用 Gradle 做到这一点?

【问题讨论】:

    标签: gradle


    【解决方案1】:

    我认为您不会找到一些 Gradle 核心任务来实现这一点(可能存在一些社区插件),但您可以编写自己的自定义 task 以您想要的任何格式(文本、csv、. ..)

    task dependencyList() {
        doLast() {
            configurations.each { configuration ->
                if (configuration.isCanBeResolved()){
                    println("Configuration $configuration.name ================================")
                    def files = configuration.resolvedConfiguration.getFiles().sort()
                    files.forEach{ f -> println("  dep: $f.name")}
                }
            }
        }
    }
    
    

    【讨论】:

    • 我看到的唯一问题是我们没有组 ID。我们可以将最后两行修改为:def resolvedArtifacts = configuration.resolvedConfiguration.getResolvedArtifacts()resolvedArtifacts.forEach{ f -> println("$f.moduleVersion.id.group:$f.moduleVersion.id.name:$f.moduleVersion.id.version")}
    • println("$f.moduleVersion.id.group:$f.moduleVersion.id.name:$f.moduleVersion.id.version")}可以进一步简化为println(f.id.componentIdentifier)
    猜你喜欢
    • 2021-07-27
    • 2016-10-08
    • 1970-01-01
    • 2018-06-19
    • 2015-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-30
    相关资源
    最近更新 更多