lundi 1 août 2022

How can I get a separate reference to each version/artifact of the same published java package, in one JVM (stack = sbt + scala)?

I am considering building a small tool for running different releases of the same project.

So far, i can import the project, and can even import both versions without compilation failures.

name := "diffing-tool"

val sparkVersion = "3.1.2"
resolvers += Resolver.publishMavenLocal
resolvers += Resolver.defaultLocal
libraryDependencies ++= Seq(
  "org.apache.spark" %% "spark-core" % sparkVersion,
  "org.apache.spark" %% "spark-sql" % sparkVersion,
  "com.foo" %% "bar" % "1.0",
  "com.foo" %% "bar" % "1.1"

)

My code can compile and run the following code


object Hello extends App {
  println("Hello world")
  val fromJar1 = com.foo.bar.Run.main()
  val fromJar2 = com.foo.bar.Run.main()

  def diff(fromJar1: Any, fromJar2: Any) = {
    println("Hello world")
  }

  diff(fromJar1, fromJar2)
}

However, I do not know a good way to rename the packages/artifacts such that the code can refer to each version of the code separately.

Ideas of what this would look like include

  1. the use of shading in the build.sbt file (which is usually used for dependencies of dependencies, and I have not yet figured out what to do in my current use case -- also, i would like to avoid sbt assembly if possible which is coupled with shading if i am not mistaken). ill need detailed instructions on how to set this up.
  2. Another approach is, at runtime, to use reflection, the ClassLoader, or perhaps something from the sbt.librarymanagement package, to get a separate reference to each artifact. Will need detailed instructions on how to set this up.
  3. Other ideas are very welcome.




Aucun commentaire:

Enregistrer un commentaire