【问题标题】:Gradle shared dependency version issueGradle 共享依赖版本问题
【发布时间】:2019-06-27 10:00:23
【问题描述】:

我对 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?

【问题讨论】:

    标签: java gradle


    【解决方案1】:

    我发现了错误。我在 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'
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-03
      • 1970-01-01
      • 1970-01-01
      • 2017-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-02
      相关资源
      最近更新 更多