【问题标题】:Gradle Dependency of Dependency SubstitutionGradle 依赖替换的依赖
【发布时间】:2022-08-16 09:15:50
【问题描述】:

我有一个 spring-boot gradle 项目。它具有依赖关系的依赖关系,我由于内部原因无法使用。我已经分叉了该模​​块并将其发布到我的存储库中,并带有我想在我的项目中使用的自己的版本号。

部分依赖树如下所示:

compileClasspath - Compile classpath for source set \'main\'.
+--- org.springframework.boot:spring-boot-starter-web:2.7.2
|    +--- org.springframework.boot:spring-boot-starter-tomcat:2.7.2
|    |    +--- jakarta.annotation:jakarta.annotation-api:1.3.5
|    |    +--- org.apache.tomcat.embed:tomcat-embed-core:9.0.65
|    |    +--- org.apache.tomcat.embed:tomcat-embed-el:9.0.65
|    |    \\--- org.apache.tomcat.embed:tomcat-embed-websocket:9.0.65

我需要强制我的项目使用底部两个模块的自定义版本,让我们将新版本称为org.apache.tomcat.embed:tomcat-embed-el:9.0.65.CUSTOMorg.apache.tomcat.embed:tomcat-embed-websocket:9.0.65.CUSTOM

我从来没有尝试过这个。我正在我的项目的 build.gradle 文件中尝试所有不同的设置,例如

configurations { main }
configurations.main.resolutionStrategy.dependencySubstitution {
    substitute module(\'org.apache.tomcat.embed:tomcat-embed-el:9.0.65\') using module(\'org.apache.tomcat.embed:tomcat-embed-el:9.0.65.CUSTOM\')
    substitute module(\'org.apache.tomcat.embed:tomcat-embed-websocket:9.0.65\') using module(\'org.apache.tomcat.embed:tomcat-embed-websocket:9.0.65.CUSTOM\')
}

但是 9.0.65 版本不断被注入。所以我的问题是:

  • 我是否错误地使用了dependencySubstitution,它应该在build.gradle 的什么位置?
  • dependencySubstitution 是否适用于依赖项的依赖项(令人作呕),还是仅适用于直接依赖项?
  • 没有分叉所有这些依赖项来声明它们的依赖项,有什么方法可以强制我的 Gradle绝不使用版本9.0.65只要曾经使用过9.0.65.CUSTOM,无论它在依赖树中的位置如何?
  • 也许`configurations.main.resolutionStrategy { force \'org.apache.tomcat.embed:tomcat-embed-el:9.0.65.CUSTOM\' force \'org.apache.tomcat.embed:tomcat-embed-websocket:9.0 .65.CUSTOM\' } ` 如果没记错的话。
  • 那不编译。显示错误:\“在 org.gradle.api.internal.artifacts 类型的对象上找不到参数 [org.apache.tomcat.embed:tomcat-embed-el:9.0.65.CUSTOM] 的方法 force()。 ivyservice.dependencysubstitution.DefaultDependencySubstitutions.\"。顺便说一句,我正在使用 Gradle 版本 6.8.1。
  • 我不会称之为“依赖注入”,也不会这样标记它。请参阅en.wikipedia.org/wiki/Dependency_injection 了解该术语的普遍理解。
  • 该手册有一个配方,我没有尝试过,但它可能只是工作:docs.gradle.org/6.8.1/userguide/…
  • 我认为最大的障碍是您创建自己的配置(“main”),并让替换只适用于那里。 configurations.all 将包含所有有趣的配置。

标签: spring spring-boot gradle


【解决方案1】:

假设您的配置设置正确并且您的自定义模块的坐标是正确的,您应该能够做到

allprojects {
    configurations.all {
        resolutionStrategy {
            dependencySubstitution {
                substitute(module("org.apache.tomcat.embed:tomcat-embed-el")).with(module("org.apache.tomcat.embed:tomcat-embed-el:9.0.65.CUSTOM"))
                substitute(module("org.apache.tomcat.embed:tomcat-embed-websocket")).with(module("org.apache.tomcat.embed:tomcat-embed-websocket:9.0.65.CUSTOM"))
            }
        }
    }
}

我会仔细检查您是否拥有存储库、配置,以及您是否在需要将给定模块替换为自定义模块的模块中进行此依赖替换。否则你可能会遇到错误。

【讨论】:

    猜你喜欢
    • 2018-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 2015-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多