Gradle中的传递文件依赖关系 [英] Transitive file dependencies in gradle

查看:375
本文介绍了Gradle中的传递文件依赖关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  allprojects {
configurations {
export {
description ='导出的类路径'
}
编译{
extendsFrom export
}
}
}

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

 依赖项{
编译文件('A.jar','B.jar')
导出文件('C.jar')
}

项目B依赖项目A,但只有 C.jar 应该在类路径上进行编译,所以添加:

 依赖项{
export project(path:': A',配置:'export')
}

这会产生所需的结果, code> A.jar 和 B.jar 不在类路径中,但是 C.jar 位于co的类路径上mpilation。

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



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

解决方案

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

  //项目A build.gradle 
依赖关系{
compile(files('X.jar ','Y.jar')){transitive = false}
导出文件('Z.jar')
}

这会阻止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)

帮助。


I would like to control which of my dependencies in a multi-project Java build are transitive. My current solution is to set up an "export" configuration in the root project:

allprojects {
    configurations {
        export {
            description = 'Exported classpath'
        }
        compile {
            extendsFrom export
        }
    }
}

Project A has multiple file dependencies:

dependencies {
    compile files('A.jar', 'B.jar')
    export files('C.jar')
}

Project B has a dependency on project A, but only C.jar should be on the classpath for compilation, so add:

dependencies {
    export project(path: ':A', configuration:'export')
}

This produces the desired results, A.jar and B.jar are not on the class path, but C.jar is on the classpath for compilation.

I am unsure if this is "gradle" way of doing things. To configure transitivity, I would rather specify an attribute or a configuration closure to the dependency entries in project A, instead of using a different "export" configuration.

Is this possible for file dependencies, or is there another way to achieve this?

解决方案

If I understand your scenario correctly, then yes it's easy to do this. Just add an options closure to the end of the dependency declaration to prevent transitive dependencies (I've changed A,B,C .jar to X,Y,Z because I'm guessing they don't coincide with projects A and B):

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

Which would prevent X.jar and Y.jar from being added to the classpath for project B.

Alternatively, and I don't know how well this would work for you and don't really recommend it (just want you to know of the possibilities) you could do this in project B's build.gradle:

configurations.compile.dependencies.find { it.name == "A.jar" }.exclude(jar: it)
configurations.compile.dependencies.find { it.name == "B.jar" }.exclude(jar: it) 

Hope that helps.

这篇关于Gradle中的传递文件依赖关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆