如何在 Scala 中使用多个版本的库? [英] How to use multiple versions of a library in Scala?

查看:68
本文介绍了如何在 Scala 中使用多个版本的库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Scala 中使用了一个库,比如 A,它依赖于另一个库的 x.11 版本,比如 Z.

I am using a library say A in Scala which is dependent on version x.11 of another library say Z.

现在,我还使用了一个库,比如 B,它依赖于 Z 的 x.31 版本.

Now, I am also using a library say B which is dependent on version x.31 of Z.

这会导致编译错误,因为我们将有两个版本的库 Z,如何在 Scala 的 sbt 中同时使用库 A 和 B?有什么办法可以指定.

This leads to compile error because we will have two versions of library Z, how can I use both libraries A and B in scala's sbt? Is there any way to specify it.

推荐答案

如果用更新的版本完全替换一个依赖项碰巧有效,那么 Sparko 的解决方案也有效.然而,情况并非总是如此.

If completely replacing one dependency with a newer version happens to work, then Sparko's solution works. However, that isn't always the case.

如果要在 sbt-assembly 生成的 uber-jar 中包含库的两个版本,则需要使用着色.请参阅这篇博文,了解什么是着色以及与之相关的一些缺点.

If you want to include both versions of a library in the uber-jar produced by sbt-assembly, you'll need to use shading. See this post for an overview of what shading is, and some of the drawbacks associated with it.

着色在 sbt-assembly 的文档这里中有介绍,但如果您是像我这样的人,他们的解释方式会让你比开始时更困惑.有一篇很好的博客文章,Spark, Uber Jars 和 Shading with sbt-assembly (waybackmachine 链接),这有助于揭开它的神秘面纱.这是相关部分:

Shading is covered in sbt-assembly's documentation here, but if you're anything like me, their way of explaining it will leave you more confused than you started. There's a good blog post, Spark, Uber Jars and Shading with sbt-assembly (waybackmachine link), that helps to demystify it a bit. Here's the relevant section:

我可以遮蔽我的类型安全配置版本,给它一个不同的名称,因此 Spark 不会在版本之间混淆.我赶紧去了到我的 build.sbt 文件,并添加以下代码:

I can shade over my typesafe config version, giving it a different name so Spark won’t get confused between the versions. I quickly went to my build.sbt file, and added the following code:

assemblyShadeRules in assembly := Seq(
ShadeRule.rename("com.typesafe.config.**"->"my_conf.@1").inLibrary("com.typesafe" % "config" % "1.3.0").inProject)

assemblyShadeRules in assembly := Seq(
ShadeRule.rename("com.typesafe.config.**" -> "my_conf.@1") .inLibrary("com.typesafe" % "config" % "1.3.0") .inProject )

根据文档,这应该将任何类放在com.typesafe.config 在新包 my_conf 下.

According to the documentation, this should place any class under com.typesafe.config under the new package my_conf.

对于您的情况,解决方案是在您的 build.sbt 文件中添加如下内容:

For your case, the solution would be adding something like this to your build.sbt file:

assemblyShadeRules in assembly := Seq(
      ShadeRule.rename("com.somecompany.**" -> "my_conf.@1")
      .inLibrary("com.somecompany" % "libraryZ" % "0.11")
      .inProject
    )

这篇关于如何在 Scala 中使用多个版本的库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆