如何防止classpath上的第三方JAR覆盖我的类依赖关系? [英] How to prevent third-party JARs on the classpath from overriding my class dependencies?

查看:144
本文介绍了如何防止classpath上的第三方JAR覆盖我的类依赖关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Java编写应用程序插件,并且我的插件依赖于多个第三方JAR。我将这些依赖项与我的插件捆绑在一起,以便我可以只部署一个JAR文件。

I am writing an application plugin in Java, and my plugin has dependencies on several third-party JARs. I am bundling these dependencies with my plugin so that I can deploy just a single JAR file.

主机应用程序也可能正在运行其他供应商的插件。不幸的是,宿主应用程序将所有插件都放在了类路径上,并且我无法更改此行为。如果另一个供应商的插件在我的插件之前加载并使用了不兼容的依赖项版本,则我的插件可能会崩溃。

The host application may also be running plugins from other vendors. Unfortunately the host application puts all the plugins on the classpath, and I am not able to change this behavior. If another vendor's plugin is loaded before mine and uses an incompatible version of a dependency, my plugin could crash.

我无法测试我的插件与其他插件之间的兼容性提前时间。我也不能说插件之间存在不兼容性-如果我的插件崩溃,则对我的公司影响不大。客户不在乎我的插件为什么会崩溃,他们将其归因于我这方面的不良编程。

I am not able to test compatibility between my plugin and other plugins ahead of time. It is also not acceptable for me to say that there is an incompatibility between the plugins--if my plugin crashes, it reflects poorly on my company. The customer does not care why my plugin crashes, they will attribute it to poor programming on my end.

我正在寻找一种防止其他供应商的插件受到干扰的方法用我自己的

I am looking for a way to prevent other vendors' plugins from interfering with my own. Is it possible?

我听说过自定义类加载器,但我不确定该解决方案是否对我有用。

I've heard of custom classloaders but I'm not sure if that solution will work for me.

推荐答案

您可以使用 Uberjar 。它的作用是将所有jar /类移至自定义名称空间,这样就不会发生类冲突,因为依赖项具有不同的名称空间。

You can use Uberjar. What it does is move all your jars/classes to a custom namespace so that none of your classes clash because your dependencies have a different namespace.

您可能想看看 maven阴影

这篇关于如何防止classpath上的第三方JAR覆盖我的类依赖关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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