Gradle配置依赖项和Gradle配置传入依赖项之间有什么区别?

时间:2017-11-15 22:22:49

标签: gradle

Gradle配置有两个属性称为dependenciesincoming。根据我的理解,我们可以通过Gradle依赖关系块为配置添加依赖关系,如下所示:

dependencies {
    testCompile "junit:junit:4.12"
}

我希望在执行上面的代码块之后,testCompile.dependencies将返回一个包含至少一个表示JUnit依赖项的对象的集合。

我不确定testCompile.incoming代表的是什么。

配置的dependenciesincoming属性之间有什么关系?使用传入属性的示例是什么?

1 个答案:

答案 0 :(得分:0)

我想我通过一些实验来解决这个问题。我开始使用Gradle创建一个虚拟项目。我有以下Gradle脚本来尝试testCompile配置属性的不同属性。

group 'com.example'
version '1.0-SNAPSHOT'

apply plugin: 'java'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

task("dummyFileGenerator") {
    outputs.file '/tmp'
}

// configurations.testCompile.transitive false

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
    testCompile files('/tmp')

}

println "Printing dependencies"
configurations.testCompile.dependencies.all {
    println(it)
}
println "======="

println "Printing all dependencies"
configurations.testCompile.allDependencies.all {
    println it
}
println '======='

println "printing incoming dependencies"
configurations.testCompile.incoming.dependencies.all {
    println(it)
}
println "======="

println "printing files of incoming dependencies"
configurations.testCompile.incoming.files.forEach {
    println it
}
println "======="

println "printing artifacts of incoming dependencies"
configurations.testCompile.incoming.artifacts.forEach {
    println it
}
println "======="

println "printing files of incoming dependencies (again)"
configurations.testCompile.incoming.files.forEach {
    println it
}
println "======="

task("blecgh") {
    doLast {
        println "Hello!"
    }
}

此脚本的示例输出为:

Printing dependencies
DefaultExternalModuleDependency{group='junit', name='junit', version='4.12', configuration='default'}
org.gradle.api.internal.artifacts.dependencies.DefaultSelfResolvingDependency_Decorated@433edba9
=======
Printing all dependencies
DefaultExternalModuleDependency{group='junit', name='junit', version='4.12', configuration='default'}
org.gradle.api.internal.artifacts.dependencies.DefaultSelfResolvingDependency_Decorated@433edba9
=======
printing incoming dependencies
DefaultExternalModuleDependency{group='junit', name='junit', version='4.12', configuration='default'}
org.gradle.api.internal.artifacts.dependencies.DefaultSelfResolvingDependency_Decorated@433edba9
=======
printing files of incoming dependencies
/tmp
/usr/local/google/home/afwang/.gradle/caches/modules-2/files-2.1/junit/junit/4.12/2973d150c0dc1fefe998f834810d68f278ea58ec/junit-4.12.jar
/usr/local/google/home/afwang/.gradle/caches/modules-2/files-2.1/org.hamcrest/hamcrest-core/1.3/42a25dc3219429f0e5d060061f71acb49bf010a0/hamcrest-core-1.3.jar
=======
printing artifacts of incoming dependencies
tmp
junit.jar (junit:junit:4.12)
hamcrest-core.jar (org.hamcrest:hamcrest-core:1.3)
=======
printing files of incoming dependencies (again)
/tmp
/usr/local/google/home/afwang/.gradle/caches/modules-2/files-2.1/junit/junit/4.12/2973d150c0dc1fefe998f834810d68f278ea58ec/junit-4.12.jar
/usr/local/google/home/afwang/.gradle/caches/modules-2/files-2.1/org.hamcrest/hamcrest-core/1.3/42a25dc3219429f0e5d060061f71acb49bf010a0/hamcrest-core-1.3.jar
=======
:blecgh
Hello!

BUILD SUCCESSFUL

Total time: 0.621 secs

值得注意的是,incoming.filesincoming.artifacts包含Hamcrest依赖关系的表示。这表明配置的incoming属性除了配置的直接依赖性的工件之外还包括配置的传递依赖性的工件。这似乎与配置的transitive属性有关。如果transitive属性设置为false,则incoming属性将不包含配置的传递依赖性'文物;只有直接依赖'工件将被包含。

请注意,配置的allDependencies属性不包含传递依赖项。相反,allDependencies属性包括来自超级配置的继承依赖项。

这是帮助解释我认为这些配置属性代表的内容的图表:

configParent -- depParent
|
|
configChild -- depA
            \- depB -- depB1 -- depB2

configParentconfigChild是配置。 configParent直接依赖depParentconfigParentconfigChild的超级配置。 configChild具有直接依赖关系depAdepBdepB依赖depB1,而depB1依赖depB2

configChild.dependencies仅包含depAdepBconfigChild.allDependencies仅包含depParentdepAdepBconfigChild.incoming.artifacts将包含依赖项depParentdepAdepBdepB1depB2的工件。同样,configChild.incoming.files将包含依赖项文件depParentdepAdepBdepB1depB2

我总结一下,配置的传入属性是其所有依赖关系的解析,包括从超级配置继承的依赖关系。