在Maven中着色依赖项 [英] Shading a dependency in Maven
问题描述
我有两个通过Maven导入的依赖项,它们都导入一个公共库,但是版本不同,但是版本彼此不兼容.本质上,该问题在这篇文章中描述了 :
I have two dependencies imported via Maven that both import a common library, but at different versions but the versions are not compatible with each other. Essentially the problem described in this post:
但是不幸的是,对于我来说,解决方案并不像博客文章中描述的那样简单,因为没有一个通用版本的软件包Z
可同时用于这两个依赖项.
But unfortunately for me, the solution is not as simple as the blog post describes, because there isn't a common version of package Z
that works for both dependencies.
由于我不控制所有这些库,因此跳过了导致这一点的不良设计决策,因此我希望重新包装顶级依赖项之一并对其所有依赖项进行着色,以使其实质上可以使用自己的,Z
的隔离版本.用Maven可以做到吗?
Skipping the poor design decisions that led to this point as I don't control any of these libraries, I'm looking to repackage one of the top-level dependencies and shade all of its dependencies so it can essentially use its own, isolated version of Z
. Is this possible to accomplish with Maven?
我考虑过的一种解决方案是隔离所有依赖于软件包Y
的类,并将它们放在单独的应用程序中,并将其作为带阴影的jar
运送给X
,但是我想知道是否存在更简单的方法来实现这一目标.
One solution I have considered is isolating all the classes that depend on package Y
and putting them in a separate application and shipping that as a shaded jar
which X
imports, however I'm wondering if there's a simpler way to accomplish that.
推荐答案
每个人都建议您使用 maven-shade-plugin .开源项目通过为每个需要着色的依赖项创建一个maven子项目来处理此问题.因此,在您的情况下,您将需要3个Maven项目:
As everyone has suggested you can use the maven-shade-plugin. Open source projects handle this by creating one maven sub project for each dependency that needs to be shaded. So in your case you would need 3 maven projects:
- 一个用于阴影依赖性Y
- 一个用于阴影依赖性G
- 一个用于您的原始项目.您的原始项目包括在1.和2.中创建的工件作为依赖项.
您的Maven项目层次结构如下所示:
Your maven project hierarchy would look like this:
- 项目
- pom.xml
- shade-Y
- pom.xml
- project
- pom.xml
- shade-Y
- pom.xml
- pom.xml
- pom.xml
在其中有一个用于隐藏依赖项的maven子项目的项目示例是此处.查看 shaded-ning19 文件夹,以了解如何创建专用的Maven项目以对依赖项进行着色.
An example of a project which has a maven sub project for shading a dependency is here. Look at the shaded-ning19 folder to see how to create a dedicated maven project for shading a dependency.
这篇关于在Maven中着色依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!