Selectively excluding dependency in SBT
Many times I have came across issues related to dependency and a lot of time is spend to figure out issues by looking at not so friendly logs and a lot of googling.
For example the latest playframework 2.8.8 on scala 2.13.6 will work with datastax-java-driver 4.9.0 but not with the latest 4.12.0. Therefore one of the easiest solution can be downgrading the datastax-java-driver to the compatible version 4.9.0.
Firstly to avoid dependency related issue is to carefully look at the official documentation and use the libraries as per the recommendation. Secondly while updating libraries update one by one so that you can easily figure out which one is incompatible. The next step is to choose a compatible version either by looking at the documentation, release notes or github issues etc etc.
In a Java application if a library A depends on B-1 and library C depends on B-2 then effectively the latest version of library B ie, B-2 will be used while resolving dependencies for both A and C.
Similarly if there is a transitive dependency of a library A on B-1 and C on B-2 then also the latest version of the dependency ie B-2 will be loaded.
Transitive dependency means that if A depends on B and B depends on C, then A depends on both B and C.
This can cause problem if A doesn’t work with B-2, but if C can work with B-1 then we can see that both A and C will work with B-1, therefore if we can selectively exclude B-2 in this scenario then our problem is solved.
In SBT we can achive this as follows
libraryDependencies ++= Seq(
"com" % "libA" % "1.0.0",
"com" % "libC" % "1.0.0" exclude("com", "libB")
)// an example from SBT website libraryDependencies +=
"log4j" % "log4j" % "1.2.15" exclude("javax.jms", "jms")
References