sbt无法在项目中导入两个不同版本的弹性搜索库



我正在尝试使用scala代码连接两个elasticsearch集群,并从中查询elasticsearch。使用以下内容scala项目build.sbt中的库和代码:

libraryDependencies +="org.elasticsearch" % "elasticsearch" % "7.2.0"
libraryDependencies += "org.elasticsearch.client" % "elasticsearch-rest-high-level-client" % "7.2.0"

val elastic4sVersion = "6.2.8"
libraryDependencies ++= Seq(
"com.sksamuel.elastic4s" %% "elastic4s-core" % elastic4sVersion,
// for the http client
"com.sksamuel.elastic4s" %% "elastic4s-http" % elastic4sVersion,
)

它们有一个通用的客户端库,在构建时会丢失该库。我可以看到6.x或7.x,但不能两者都看到。我尝试了阴影方法

assemblyShadeRules in assembly := Seq(
ShadeRule.rename("org.elasticsearch.client.**" -> "my_conf.@1")
.inLibrary("org.elasticsearch.client" % "elasticsearch-rest-high-level-client" % "7.2.0")
.inAll
)
assemblyShadeRules in assembly := Seq(
ShadeRule.rename("org.elasticsearch.client.**" -> "my_conf_1.@1")
.inLibrary("org.elasticsearch.client" % "elasticsearch-rest-high-level-client" % "6.2.2")
.inAll
)
assemblyShadeRules in assembly := Seq(
ShadeRule.rename("org.elasticsearch.elasticsearch.**" -> "my_configuration.@1")
.inLibrary("org.elasticsearch" % "elasticsearch" % "7.2.0")
.inAll
)

但我不能不获得可用的阴影版本,并在试图将它们导入项目引用时出错。

好吧,在JVM上,类路径中只能有一个相同.class的版本,所以每个构建工具都会尊重这一点。

sbt将确保项目中只有一个版本的库可用(除非您明确设置,否则我认为它会选择所有冲突版本中的最高版本号(,因此,如果您需要一个由两个依赖项使用的库,我会寻找使用相同版本依赖项的版本。(或者显式覆盖版本,并使用sbt-mima和sbt-missinglink来检查此更改是否没有破坏任何内容(。

在我看来,最简单的方法是使用projects页面上的表(与maven一起(为您想要使用的elasticsearch版本选择正确的elastic4s版本。

最新更新