1

The scenario. I write LibraryA which needs to use a newer version of a library (Caffeine in this case, and specifically v2.6.2). I will build and upload this artifact to our build artifactory

LibraryA is intended to be used by other teams (unknown to me) in an older Spring Boot 1.5 environment . The issue is that Spring Boot internally uses an older version of Caffeine 2.3.5 (although to me , it could use a newer one).

What I see is at runtime, the older version dominates the newer version resulting in class not found exceptions in my lib

Is there a way, I can force my library to always use the newer desired Caffeine version . This would be easy to do if I had control over the code using my library but I'd like to accomplish this within the scope of my library. I tried all the dependency resolution strategies in the gradle documentaion but they are always overridden by the invoking (spring-boot) application

Krzysztof Atłasik
  • 21,985
  • 6
  • 54
  • 76
Vinay B
  • 673
  • 8
  • 21

1 Answers1

2

You should use package relocating from gradle shadow plugin.

In your case you just add following to your build:

shadowJar {
   relocate 'com.github.benmanes.caffeine', 'shadow.caffeine'
}
Krzysztof Atłasik
  • 21,985
  • 6
  • 54
  • 76
  • Thanks. I tried that but the packages are not relocated ` shadowJar { baseName = 'shadow-wrapped' version = null zip64 true relocate 'com.github.ben-manes.caffeine', 'shadow-wrapped.caffeine' mergeServiceFiles() } ` but looking at the generated jar, ..nothing has been relocated ` $ jar -tf shadow-wrapped-all.jar |grep -i caf com/github/benmanes/caffeine/ com/github/benmanes/caffeine/cache/ ...... ` – Vinay B Apr 04 '19 at 23:11
  • Ok, worked , I had to use `relocate 'com.github.benmanes.caffeine', 'shadow.caffeine'` (remove the dash between ben and manes - as that doesn't exist in the actual directory pkg structure – Vinay B Apr 04 '19 at 23:19
  • Sorry for that and thanks for the remark I fixed my answer. I'm glad it worked. – Krzysztof Atłasik Apr 05 '19 at 04:35