6

I have a project with several utility classes. Let's name it Utils. I have a proj1 which depends on Utils. And another proj2 that depends on proj1 and Utils.

The problem is if both proj1 and proj2 depend on different Utils version this will lead to problems.

What's the best solution?

This situation occurs in Scala/SBT projects, but I guess other languages have the same problems.

Edit:

Just to be clear, proj2 is the project that will run, that uses some code from proj1 and Utils.

Community
  • 1
  • 1
pedrorijo91
  • 7,635
  • 9
  • 44
  • 82
  • There really is no best solution. Just workarounds and a lot of hope. You will have to choose one version of Utils over the other at some point. If it's the newer version, you better hope it is backward compatible. If it's the older version, you better hope you the library is not using any methods that do not exist yet. Either way, cross your fingers and try, that's the only solution I can think of (if of course updating the project to use the same version of Utils is out of the question). – Tunaki Nov 23 '15 at 19:53
  • 1
    Is utils another sub-project or can it be split out and published separately? The sbt-assembly project [has support for shading](https://github.com/sbt/sbt-assembly#shading) so you could deal with the conflict by changing the name of the older dependency. – Sean Vieira Nov 23 '15 at 20:14
  • @SeanVieira they are 3 different projects. No sub projects involved – pedrorijo91 Nov 23 '15 at 20:36

2 Answers2

3

This is classic Jar Hell, and it is a problem on any JVM based project not just scala with sbt.

There are 4 common solutions

  1. Get rid of conflict by changing code, consolidate your multiple version dependency into a single dependency.

  2. Shading (as mentioned above by @Sean Viera)

  3. Multiple ClassLoader component architecture like OSGI (as mentioned by @tuxdna)

  4. Run in separate JVMs like a microservice architecture (also mentioned by @tuxdna)

David Holbrook
  • 1,617
  • 14
  • 18
1

You have three different projects:

  • Utils
  • proj1 <- depends on Utils v1
  • proj2 <- depends on Utils v2

The only way you can be 100% sure that there are no conflicts between proj1 and proj2 is to run them in isolation.

As soon as you will mix proj1 and proj2 with different versions of Utils on the same classpath, you will end up override one or the other project.

You can achive isolation using:

  • run them in separate JVMs, with appropriate version of Utils
  • run them in same JVM but in different class loaders
tuxdna
  • 8,257
  • 4
  • 43
  • 61