简体   繁体   English

maven-shade-plugin 和单身人士

[英]maven-shade-plugin and singletons

I think I already know the answer, but would appreciate if someone can confirm it for me.我想我已经知道答案了,但如果有人可以为我确认,我将不胜感激。

A) We are including a library in a build using maven-shade-plugin;
B) This library implements singleton(s);
C) Our resulting jar (uber-jar) is, in turn, used in a build of a megaproject;
D) Other jars in a megaproject also using the same library (A);

Would the library (A) be not acting as a singleton across the entire megaproject?图书馆 (A) 不会在整个大型项目中充当单身人士吗? Would we end up with a separate set of static variables for each shaded copy of an artifact?对于工件的每个着色副本,我们最终会得到一组单独的静态变量吗? Would using maven-assembly-plugin instead of maven-shade plugin help?使用 maven-assembly-plugin 而不是 maven-shade 插件会有帮助吗?

You described a scenario in which the same class could end up in the classpath more than once.您描述了一个场景,在该场景中,同一个类可能会多次出现在类路径中。

In such an application the class loader will pick up the first one found when looking for that class.在这样的应用程序中,类加载器将在查找该类时选择找到的第一个。 Once it has found it, this class is loaded and initialized by this class loader, and he will not look it up again.一旦找到了,这个类就由这个类加载器加载和初始化,他就不会再查找了。 Usually that does not lead to problems, and you will still have the singleton as you want: one instance only.通常这不会导致问题,并且您仍然可以根据需要获得单例:只有一个实例。

If - for some reason - you have multiple class loaders at hand, they each could load this class from another location.如果——出于某种原因——你手头有多个类加载器,它们每个都可以从另一个位置加载这个类。 So you could end with several singleton instances.所以你可以用几个单例实例结束。

The shade plugin seems not to be the best tool for that.阴影插件似乎不是最好的工具。 I would suggest to use it only for standalone applications that you package in a single shaded JAR - the end product.我建议仅将它用于打包在单个阴影 JAR 中的独立应用程序 - 最终产品。

We always use the assembly plugin because it gives a more fine-grained control over the packaged assembly.我们总是使用程序集插件,因为它可以对打包的程序集进行更细粒度的控制。 But you should not use shaded JARs as dependencies, instead simply use the core libraries.但是您不应该使用着色的 JAR 作为依赖项,而应简单地使用核心库。 Even if you have the same dependency in various dependency paths in your project, the assembly plugin will package it only once.即使你在你的项目中的各个依赖路径中有相同的依赖,程序集插件也只会打包一次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM