首页 > 解决方案 > Gradle 共享依赖版本问题

问题描述

我对 Gradle 构建所采用的错误版本的依赖项有疑问。我使用 Gradle 4.6。

我有一个我开发的库,它带来了 Utils 的功能之王。我们称之为utils.jar。我还开发了另一个用于与 Web 服务通信的库。我们称之为network.jar。然后我开发了一个需要 network.jar 依赖和 utils.jar 依赖的项目。但我需要一个高于 network.jar 使用的 utils.jar 版本。

My project 
 |
 |-> network.jar:v1
 |       |
 |       |
 |       -> utils.jar:v1
 |
 -> utils.jar:v2

这是网络库的 build.gradle

dependencies {   
    compile "com.mycompany:utils:1"
}

这是我的项目的 build.gradle

dependencies {   
    compile "com.mycompany:network:1" // pull utils.jar:1
    compile "com.mycompany:utils:2" // I need utils.jar:2 in my project
}

在我的项目中,当我尝试使用utils.jar的v2时,v2中引入的新方法不可用。在 IntelliJ 中,我可以在类路径中看到 utils.jar v2,但是当我尝试使用 com.mycompany.utils.CollectionUtils 类时,它使用嵌入在 network.jar 库(v1)中的 utils 代码,而不是使用实用程序 v2。

因此,我尝试从 network.jar 中显式删除 utils.jar (v1),同时将此依赖项导入我的项目中。build.gradle 在我的项目中:

dependencies {   
    compile("com.mycompany:network:1") {
        exclude group: 'com.mycompany', module: 'utils' 
    }
    compile "com.mycompany:utils:2"
}

但是 utils v1 代码仍然嵌入在 network.jar 代码中,这是用来代替 v2 的。

我怎样才能让 Gradle 明白我想要 utils v2 而不是 network.jar 拉出的 utils v1 ?

标签: javagradle

解决方案


我发现了错误。我在 Maven repo 上构建和发布 network.jar lib 作为 fat-jar。因此,utils.jar 的代码总是嵌入在network.jar 的代码中。

如果人们犯了同样的错误,我会发布解决方案。

我从 network.jar build.gradle 文件中删除了它:

jar {
    // Remove that to not build a fat-jar in this case
    // from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } 

    manifest {
        attributes 'Implementation-Title': 'Utils',
            'Implementation-Version': version,
            'Created-By': 'Myself'
    }
}

推荐阅读