java 什么是带阴影的罐子?uber jar 和 shaded jar 之间有什么区别/相似之处?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49810578/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is a shaded jar? And what is the difference/similarities between uber jar and shaded jar?
提问by user3478709
Can you please help explain what is a shaded jar and how is the maven-shade-plugin
useful? Also what is an uber jar.
你能帮忙解释一下什么是带阴影的罐子以及它maven-shade-plugin
有什么用处吗?还有什么是超级罐子。
回答by glytching
I'll explain what an uber JAR is first because this underpins the shading explanation.
我将首先解释 uber JAR 是什么,因为这是对阴影解释的基础。
Uber JAR
优步 JAR
An uber JAR is a JAR which contains the contents of multiple JARs (or, less commonly, multiple other JARs themselves)
一个超级 JAR 是一个 JAR,它包含多个 JAR 的内容(或者,不太常见的是,多个其他 JAR 本身)
Your application will almost certainly use other packages and these packages might be provided as JARs. When using Maven these dependencies would be expressed as follows:
您的应用程序几乎肯定会使用其他包,这些包可能作为 JAR 提供。使用 Maven 时,这些依赖项将表示如下:
<dependency>
<groupId>...</groupId>
<artifactId>...</artifactId>
<version>...</version>
</dependency>
At runtime your application will expect to find the classes contained in this JAR on its classpath.
在运行时,您的应用程序将期望在其类路径上找到此 JAR 中包含的类。
Rather than shipping each of these dependent JARs along with your application, you could create an uber JAR which contains all of the classes etc from these dependent JARs and then simply run your application from this uber JAR.
您可以创建一个 uber JAR,其中包含来自这些依赖 JAR 的所有类等,而不是将这些依赖 JAR 中的每一个与您的应用程序一起发送,然后只需从这个 uber JAR 运行您的应用程序。
Shading
底纹
Shading provides a way of creating an uber JAR and renaming the packages which that uber JAR contains. If your uber JAR is likely to be used as a dependency in another application then there's a risk that the versions of the dependent classes in the uber JAR might clash with versions of those same dependencies in this other application. Shading helps to avoid any such issue by renaming the packages within the uber JAR.
着色提供了一种创建超级 JAR 和重命名超级 JAR 包含的包的方法。如果您的 uber JAR 很可能被用作另一个应用程序中的依赖项,那么 uber JAR 中依赖类的版本可能会与其他应用程序中的那些相同依赖项的版本发生冲突。着色有助于通过重命名 uber JAR 中的包来避免任何此类问题。
For example:
例如:
- You create an uber JAR which contains v1.0.0 of the
Foo
library. - Someone else uses your uber JAR in their application,
Bar
- The
Bar
application has its own dependency onFoo
but on v1.2.0 of that library.
- 您创建了一个包含
Foo
库v1.0.0 的 uber JAR 。 - 其他人在他们的应用程序中使用了你的 uber JAR,
Bar
- 该
Bar
应用程序依赖于Foo
该库的 v1.2.0。
Now, if there is any clash between versions 1.0.0 and 1.2.0 of Foo
we may have a problem because the owner of Bar
cannot rely on which one will be loaded so either their code will misbehave or your code - when running within their application - will misbehave.
现在,如果Foo
我们的1.0.0 和 1.2.0 版本之间存在任何冲突,我们可能会遇到问题,因为所有者Bar
无法依赖将加载哪个版本,因此他们的代码将出现异常或您的代码 - 在他们的应用程序中运行时 -会行为不端。
Shading helps to avoid issues such as this and also allows the provider of Foo
to be explicit about the versions of the dependent libraries it uses.
着色有助于避免此类问题,并且还允许提供者Foo
明确其使用的依赖库的版本。
The maven-shade-plugin
allows you to (a) create an uber JAR and (b) to shade its contents.
将maven-shade-plugin
让你(一)创建一个超级JAR和(b)遮阳其内容。
Summary
概括
Creating an uber JAR is a useful technique for simplifying your deployment process.
创建 uber JAR 是一种用于简化部署过程的有用技术。
Shading is an extension to the uber JAR idea which is typically limited to use cases where
着色是 uber JAR 想法的扩展,它通常仅限于以下用例
- The JAR is a library to be used inside another application/library
- The authors of the JAR want to be sure that the dependencies used by the JAR are in their control
- The authors of the JAR want to avoid 'version clash' issues for any applications/libraries using the JAR
- JAR 是在另一个应用程序/库中使用的库
- JAR 的作者希望确保 JAR 使用的依赖项在他们的控制之下
- JAR 的作者希望避免使用 JAR 的任何应用程序/库的“版本冲突”问题