Java 8 here.
Say there is an old version of the widget
libray, with Maven coordinates widgetmakers:widget:1.0.4
, that has a class defined in it like so:
public class Widget {
private String meow;
// constructor, getters, setters, etc.
}
Years pass. The maintainers of this widget
library decide that a Widget
should never meow
, rather, that it should in fact bark
. And so a new release is made, with Maven coordinates widgetmakers:widget:2.0.0
and with Widget
looking like:
public class Widget {
private Bark bark;
// constructor, getters, setters, etc.
}
So now I go to build my app, myapp
. And, wanting to use the latest stable versions of all my dependencies, I declare my dependencies like so (inside of build.gradle
):
dependencies {
compile (
,'org.slf4j:slf4j-api:1.7.20'
,'org.slf4j:slf4j-simple:1.7.20'
,'bupo:fizzbuzz:3.7.14'
,'commons-cli:commons-cli:1.2'
,'widgetmakers:widget:2.0.0'
)
}
Now let's say that this (fictional) fizzbuzz
library has always depended on a 1.x version of the widget
library, where Widget
would meow
.
So now, I'm specifying 2 versions of widget
on my compile classpath:
widgetmakers:widget:1.0.4
which is pulled in by thefizzbuzz
library, as a dependency of it; andwidgetmakers:widget:2.0.0
which I am referencing directly
So obviously, depending on which version of Widget
gets classloaded first, we will either have a Widget#meow
or a Widget#bark
.
Does Gradle provide any facilities for helping me out here? Is there any way to pull in multiple versions of the same class, and configure fizzbuzz
classes to use the old version of Widget
, and my classes to use the new version? If not, the only solutions I can think of are:
- I might be able to accomplish some kind of shading- and/or fatjar-based soltuion, where perhaps I pull in all my dependencies as packages under
myapp/bin
and then give them different version-prefixes. Admittedly I don't see a clear solution here, but am sure something is feasible (yet totally hacky/nasty). Or... - Carefully inspect my entire dependency graph and just make sure that all of my transitive dependencies don't conflict with each other. In this case for me, this means either submitting a pull-request to the
fizzbuzz
maintainers to upgrade it to the latestwidget
version, or, sadly, downgradingmyapp
to use the olderwidget
version.
But Gradle (so far) has been magic for me. So I ask: is there any Gradle magic that can avail me here?