Gradle 覆盖特定依赖项的一般传递=false

Gradle overwriting general transitive=false for specific dependency

在尝试 gradle 之后,我偶然发现了以下行为,我不确定这是否符合预期,或者我是否做错了什么。

默认情况下,我会通过告诉编译配置 transitive false 来排除所有传递依赖项。但是对于特定的依赖项,我只想包含传递依赖项,所以我在依赖项声明中添加了 transitive true

但碰巧 gradle 忽略了我的覆盖。

这是预期的吗?我做错了什么吗?

示例如下:

build.gradle

apply plugin: 'java'

repositories {
    jcenter()
}

configurations {
    compile {
            transitive false
    }
    testCompile {
            transitive false
    }
}

dependencies {
    testCompile('junit:junit:4.12') { transitive true }
}

产出

默认包括传递依赖

:dependencies

------------------------------------------------------------
Root project
------------------------------------------------------------

testCompile - Compile classpath for source set 'test'.
\--- junit:junit:4.12
     \--- org.hamcrest:hamcrest-core:1.3

BUILD SUCCESSFUL

Total time: 3.404 secs

默认情况下不包括传递依赖项,但包括特定依赖项

:dependencies

------------------------------------------------------------
Root project
------------------------------------------------------------

testCompile - Compile classpath for source set 'test'.
\--- junit:junit:4.12

BUILD SUCCESSFUL

Total time: 3.352 secs

我不知道您是否可以这样做,因为您已经声明该配置以排除传递依赖项。我看到的另一种模式是相反的,在依赖声明中包括传递和排除。

configurations {
    compile {
            transitive false
    }
    testCompile {
            transitive false
    }
}

dependencies {
    testCompile('junit:junit:4.12') { exclude module: 'exclude.this' }
}

或更改配置以排除除您想要的以外的所有内容