Is it possible to shade some package in only one package in sbt?

Tags: , , , ,



let’s consider a following source code:

assemblyShadeRules in assembly ++= Seq(
  ShadeRule.rename("com.google.common.**" -> "my_package.@1")
   .inAll
)

It renames com.google.common and change package names as well. Additionally, thanks to inAll it rewrites all imports and another references in all classes in jar. It works fine. However, the question is:

Is it possible to rewrite only chosen references? I mean pointed by me using path to package?

Let’s say something like:

assemblyShadeRules in assembly ++= Seq(
  ShadeRule.rename("com.google.common.**" -> "my_package.@1")
   .inPath("org.apache.spark")
)

I would expect that only classes under org.apache.spark package will be rewritten (its references to com.google.common).

Answer

It can be achieved using inLibrary method. It’s usage is:

assemblyShadeRules in assembly ++= Seq(
  ShadeRule.rename("com.google.common.**" -> "my_package.@1") // it defines what/how to rewrite (belowe we define where to rewrite)
    .inLibrary("com.google.guava" % "guava" % "30.1-jre") // it rewrites references and packages names "com.google.common -> my_package.com.google.common")
    .inLibrary("org.apache.spark" % "spark-core" % "2.4.7") // it rewrites references (e.g. imports to guava from spark-core code)
)


Source: stackoverflow