How to use multiple versions of a library in Scala?

后端 未结 2 1293
星月不相逢
星月不相逢 2021-01-12 23:17

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

Now, I am also using a library say B which is dependent on version

2条回答
  •  温柔的废话
    2021-01-12 23:43

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

    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.

    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:

    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 )

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

    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
        )
    

提交回复
热议问题