在Spring Boot Gradle插件中切换Spring数据发布系列的正确方法

问题描述

我正在尝试构建一个新的Spring Boot项目,但找不到切换Spring Data发布系列的方法。我们正在使用6.8版的Elasticsearch,因此我不能只使用Spring Boot提供的认依赖项。

Spring Data Elasticsearch参考页告诉您,要使用6.8版,我需要使用Moore发布火车https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#preface.versions

我看过Maven https://docs.spring.io/spring-boot/docs/2.1.10.RELEASE/reference/html/using-boot-build-systems.html#using-boot-maven-without-a-parent的文档,但是Gradle的相同文档缺少Release Train开关示例https://docs.spring.io/spring-boot/docs/2.1.10.RELEASE/gradle-plugin/reference/html/#managing-dependencies-using-in-isolation

简而言之,我在build.gradle中包含以下内容

plugins {
    id 'org.springframework.boot' version '2.3.5.RELEASE'
    id 'io.spring.dependency-management' version '1.0.10.RELEASE'
    id 'java'
}

dependencyManagement {
    imports {
        mavenBom 'org.springframework.data:spring-data-releasetrain:Moore-SR11'
    }
}

dependencies {
   implementation 'org.springframework.boot:spring-boot-starter-data-elasticsearch'
}

dependencyManagement部分中切换BOM有助于将spring-data-elasticsearch的版本从4.0.5.RELEASE切换到3.2.11.RELEASE,但是elasticsearch的版本仍被视为{{1} }从某处(7.6.2输出):

gradle dependencies

有没有很好的例子?或者我在这里错过了什么?

更新:我找到了为什么要为Elasticsearch使用7.6.2版本的原因。它来自+--- org.springframework.boot:spring-boot-starter-data-elasticsearch -> 2.3.5.RELEASE | +--- org.springframework.boot:spring-boot-starter:2.3.5.RELEASE (*) | \--- org.springframework.data:spring-data-elasticsearch:4.0.5.RELEASE -> 3.2.11.RELEASE | +--- org.springframework:spring-context:5.2.10.RELEASE (*) | +--- org.springframework:spring-tx:5.2.10.RELEASE (*) | +--- org.springframework.data:spring-data-commons:2.2.11.RELEASE | | +--- org.springframework:spring-core:5.2.10.RELEASE (*) | | +--- org.springframework:spring-beans:5.2.10.RELEASE (*) | | \--- org.slf4j:slf4j-api:1.7.26 -> 1.7.30 | +--- joda-time:joda-time:2.10.8 | +--- org.elasticsearch.plugin:transport-netty4-client:6.8.13 -> 7.6.2 | | +--- io.netty:netty-buffer:4.1.43.Final -> 4.1.53.Final | | +--- io.netty:netty-codec:4.1.43.Final -> 4.1.53.Final | | +--- io.netty:netty-codec-http:4.1.43.Final -> 4.1.53.Final | | +--- io.netty:netty-common:4.1.43.Final -> 4.1.53.Final | | +--- io.netty:netty-handler:4.1.43.Final -> 4.1.53.Final | | +--- io.netty:netty-resolver:4.1.43.Final -> 4.1.53.Final | | \--- io.netty:netty-transport:4.1.43.Final -> 4.1.53.Final | +--- org.elasticsearch.client:elasticsearch-rest-high-level-client:6.8.13 -> 7.6.2 | | +--- org.elasticsearch:elasticsearch:7.6.2 项目https://github.com/spring-projects/spring-boot/blob/v2.3.5.RELEASE/spring-boot-project/spring-boot-dependencies/build.gradle#L274。 仍在寻找一种替代方法

解决方法

使用 gradle 和 java-platform 插件,我们通过调整依赖项版本取得了一些成功,如 https://docs.gradle.org/current/userguide/dependency_version_alignment.html#sec:align-versions-unpublished

这会将为所有 org.elasticsearch* 依赖项解析的版本覆盖为 6.8.13

class ElasticSearchBomAlignmentRule implements ComponentMetadataRule {
  void execute(ComponentMetadataContext ctx) {
    ctx.details.with {
      // Force specific ES version
      if (id.group.startsWith("org.elasticsearch")) {
        // declare that Elastic Search modules all belong to the ES virtual platform
        belongsTo("org.elasticsearch:elasticsearch-virtual-platform:6.8.13")
      }
    }
  }
}

dependencies {
  components.all(ElasticSearchBomAlignmentRule)
  ...

根据我们的经验,还需要降级 spring-data-elasticsearch,但这要容易得多,而且是在平台的约束下完成的

ext {
  ...
  // Downgrade below the boot integration for compatibility with es 6.8.X
  springDataEsVersion = '3.2.12.RELEASE'
  ...
}

dependencies {
  ...
  constraints {
    api "org.springframework.data:spring-data-elasticsearch:$springDataEsVersion"
    ...
  }
}