解决Java中的传递依赖冲突 [英] Resolving transitive dependency conflicts in Java

查看:413
本文介绍了解决Java中的传递依赖冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个与HBase通信的 Dropwizard (Jersey)REST端点。尽管这些是我唯一的两个顶级依赖项,但这两个依赖项都会加载许多冲突的传递依赖项。一个简单的例子就是谷歌的Guava:HBase客户端指定版本11


  • Dropwizard指定18



  • Dropwizard不适用于版本11,HBase不适用于版本18.



    我检查了Maven树荫插件文档,但它似乎没有让您重新定位在依赖jar中找到的类。所以我不知道如何解决这个问题,而不是将这两个组件分离成单独的JVM。

是肮脏的解决方案。但是你可以......



创建一个项目/模块,在其中定义一组服务接口,您的dropwizard应用程序将用于与HBase交谈。 / p>

创建另一个实现这些接口并使用HBase类的模块/项目。阴影这个项目。

在你的Dropwizard项目中只包括界面jar,但是创建一个任务来将阴影的工件复制到你的资源中。

为您的着色HBase客户端工件创建JARClassLoader。您可能必须创建一个不会委托给父级的特殊子类,因为默认情况下,类加载器会要求父级解析链接,并且可能会从外部类加载器中提取较新版本的番石榴。



从Jar加载器请求服务合约的实例...
$ b

Businessing api = Class.forName(com。 awesome.Businessing,true,jarLoader).newInstance();


I am trying to build a Dropwizard (Jersey) REST endpoint that communicates with HBase. Although these are my only two top-level dependencies, both of these dependencies come loaded with many transitive dependencies that conflict. A simple example of such a conflict is Google's Guava:

  • The HBase client specifies version 11
  • Dropwizard specifies 18

Dropwizard will not work with version 11 and HBase will not work with version 18.

I have examined the Maven shade plugin documentation, but it does not seem to let you relocate classes found in dependency jars. So I don't know how to resolve this issue short of separating these two components into separate JVMs.

解决方案

This is a dirty solution. But you could...

Create a project / module where you define a set of service interfaces that your dropwizard app will use to talk to HBase.

Create another module / project that implements these interfaces and uses the HBase classes. Shade this project.

In your Dropwizard project include only the interface jar but create a task to copy the shaded artifact into your resources.

Create a JARClassLoader for your shaded HBase client artifact. You may have to make a special subclass that does not delegate to the parent as by default the classloader will ask the parent to resolve linkages and may pull the newer version of guava from outer classloader.

Ask for an instance of the service contract from the Jar loader...

Businessing api = Class.forName("com.awesome.Businessing", true, jarLoader).newInstance();

这篇关于解决Java中的传递依赖冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆