gradle中的传递文件依赖项

Ing*_*gel 9 java gradle

我想控制多项目Java构建中的哪些依赖项是可传递的.我目前的解决方案是在根项目中设置"导出"配置:

allprojects {
    configurations {
        export {
            description = 'Exported classpath'
        }
        compile {
            extendsFrom export
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

项目A具有多个文件依赖项:

dependencies {
    compile files('A.jar', 'B.jar')
    export files('C.jar')
}
Run Code Online (Sandbox Code Playgroud)

项目B依赖于项目A,但只C.jar应在类路径上进行编译,因此添加:

dependencies {
    export project(path: ':A', configuration:'export')
}
Run Code Online (Sandbox Code Playgroud)

这会产生所需的结果,A.jar并且B.jar不在类路径上,而是C.jar在类路径上进行编译.

我不确定这是否是"gradle"做事的方式.要配置传递性,我宁愿为项目A中的依赖项指定属性或配置闭包,而不是使用不同的"导出"配置.

这可能是文件依赖,还是有另一种方法来实现这一点?

Eri*_*lin 3

如果我正确理解你的场景,那么是的,很容易做到这一点。只需在依赖项声明的末尾添加一个选项闭包即可防止传递依赖项(我已将 A、B、C .jar 更改为 X、Y、Z,因为我猜测它们与项目 A 和 B 不一致) :

// Project A build.gradle
dependencies {
   compile(files('X.jar', 'Y.jar')) { transitive = false }
   export files('Z.jar')
}
Run Code Online (Sandbox Code Playgroud)

这将阻止 X.jar 和 Y.jar 添加到项目 B 的类路径中。

或者,我不知道这对你来说效果如何,并且并不真正推荐它(只是想让你知道可能性),你可以在项目 B 的 build.gradle 中执行此操作:

configurations.compile.dependencies.find { it.name == "A.jar" }.exclude(jar: it)
configurations.compile.dependencies.find { it.name == "B.jar" }.exclude(jar: it) 
Run Code Online (Sandbox Code Playgroud)

希望有帮助。

  • @EricWendelin 那么你发现了什么? (3认同)