【问题标题】:Converting a transitive dependency into a compileOnly dependency将传递依赖转换为 compileOnly 依赖
【发布时间】:2021-07-28 04:52:57
【问题描述】:

我正在尝试使用 OSGI 来允许我使用两个不同版本的传递依赖项。计划是一个版本(较新的版本)将隐藏在 OSGI 包中,另一个将像往常一样位于运行时类路径中。

我已经使用 Gradle(使用 Groovy DSL)构建了 bundle jar,但问题是它相关的运行时依赖项是错误的 - 它带来了更新的版本,它应该隐藏在 bundle 中。当我这样做时,这仍然是正确的,在 build.gradle 文件中:

compileOnly deps.diffx
runtimeOnly(deps.diffx) {
    exclude group: 'com.propensive', module: 'magnolia_' + versions.scala_v
}

如果我使用 Gradle dependencies 任务检查依赖关系,它显示 magnolia 已按预期从 runtimeOnly 配置中排除 - 但 没有runtimeClasspath 配置中排除。

如果我随后使用./gradlew dependencyInsight --dependency magnolia_2.12 --configuration runtime 来尝试找出此依赖项的来源,它会告诉我新版本来自runtimeClasspath,具体取决于diffx,这是通过冲突解决选择的。好吧,谢谢-我已经知道了。问题是,为什么我的排除不适用于派生配置?

基本上我想做与this question相反的事情。

我也尝试了约束版本,但它们表现出同样的问题:

compileOnly deps.diffx
runtimeOnly(deps.diffx) {
    constraints {
        implementation('com.propensive:magnolia_' + versions.scala_v + ':0.10.0') {
            because 'this version is required by our other dependencies'
        }
    }
}

【问题讨论】:

    标签: gradle osgi dependency-management bnd


    【解决方案1】:

    来自the Gradle documentation

    从好的方面来说,与 Maven 相比,Gradle 的排除处理将整个依赖关系图考虑在内。因此,如果一个库上有多个依赖项,则只有在所有依赖项都同意它们时才会执行排除。例如,如果我们在上面的项目中添加 opencsv 作为另一个依赖项,该依赖项也依赖于 commons-beanutils,则不再排除 commons-collection,因为 opencsv 本身并不排除它。

    因此,我们可以使用dependency resolve rule

    def magnoliaVersion = '0.10.0'
    
    configurations.runtimeClasspath {
        resolutionStrategy.eachDependency { DependencyResolveDetails details ->
            if (details.requested.group == 'com.propensive' && details.requested.name.startsWith("magnolia_") && details.requested.version != magnoliaVersion) {
                details.useVersion magnoliaVersion
                details.because 'this version is required by our other dependencies'
            }
        }
    }
    

    然后将依赖改回一个简单的、单一的implementation 一个:

    implementation deps.diffx
    

    不幸的是,正如文档所述,此解析规则并未发布,因此必须在任何依赖的 Gradle 模块中再次应用它。

    【讨论】:

      猜你喜欢
      • 2021-08-27
      • 2018-07-06
      • 1970-01-01
      • 2019-08-23
      • 2019-10-29
      • 2021-07-26
      • 2023-03-09
      • 1970-01-01
      相关资源
      最近更新 更多