首页 文章

Gradle覆盖一般transitive = false用于特定依赖

提问于
浏览
2

在玩了一下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

1 回答

  • 1

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

    configurations {
        compile {
                transitive false
        }
        testCompile {
                transitive false
        }
    }
    
    dependencies {
        testCompile('junit:junit:4.12') { exclude module: 'exclude.this' }
    }
    

    或更改配置以排除除所需内容之外的所有内容

相关问题