英文:
Do Java Modules (JPMS/Jigsaw) Solve the Problem that "Shading" Dependencies Solves?
问题
许多Java项目都会对其依赖项进行阴影处理。我认为主要原因可以在这个Stack Overflow问题的回答中找到(https://stackoverflow.com/questions/13620281/what-is-the-maven-shade-plugin-used-for-and-why-would-you-want-to-relocate-java)。总结一下:
- 库Foo具有依赖关系Bar:1.0,并且无法升级到Bar:2.0
- Qux依赖于库Foo,但也依赖于Bar:2.0
- 如果不进行阴影处理,Bar:1.0和Bar:2.0会冲突,因此库Foo使用了Bar的阴影版本,并依赖于这个阴影版本,以避免对希望使用不同版本的Bar的下游项目产生负面影响。
我的问题是,Java模块(JPMS/Jigsaw)是否也可以在不需要阴影处理的情况下解决这个问题?我理解今天大多数Java项目并没有使用JPMS,但在Qux/Foo/Bar都完全使用JPMS模块的情况下,它们是否可以避免冲突,而不需要Foo使用Bar的阴影版本?
英文:
A lot of Java projects shade their dependencies. I believe the main reason is illustrated by the answer on this SO question (https://stackoverflow.com/questions/13620281/what-is-the-maven-shade-plugin-used-for-and-why-would-you-want-to-relocate-java). To summarize:
- Library Foo has dependency Bar:1.0 and can't upgrade to Bar:2.0
- Qux depends on library Foo but also depends on Bar:2.0
- Without shading, Bar:1.0 and Bar:2.0 would conflict, so library Foo uses a shaded version of Bar and depends on this shaded version to not negatively impact downstream projects that wish to use a different version of Bar.
My question is does Java Modules (JPMS/Jigsaw) also solve this problem without the need for shading? I understand that today, most Java projects aren't using JPMS, but in the scenario that Qux/Foo/Bar were all fully using JPMS modules, could they avoid conflicts without Foo needing to use a shaded version of Bar?
答案1
得分: 1
不支持版本控制。
Jigsaw 在这方面唯一做的事情就是禁止一个模块导出相同的包。如果发生这种情况,你会收到这样的错误信息:
java.lang.LayerInstantiationException: 包 org.example 分别在模块 somejar.v2 和模块 somejar.v1 中
换句话说,它会告诉你何时需要开始 shading。
不幸的是,maven-shade-plugin
尚不支持 Jigsaw: https://issues.apache.org/jira/browse/MSHADE-234
可以使用 jlink
创建运行时映像,但不能将其放在另一个 Java 可执行文件的模块路径上(而且它们也更大且依赖于平台,顺便说一句)。
在 Jigsaw 中也没有自包含模块(带有依赖关系的 JAR)的概念。
英文:
No
Jigsaw does not support versioning at all.
The only thing that Jigsaw does in this respect is to prohibit having more than one module exporting the same package. If that happens you will get an error message like this:
Error occurred during initialization of boot layer
java.lang.LayerInstantiationException: Package org.example in both module somejar.v2 and module somejar.v1
In other words: It tells you when you need to start shading.
Unfortunately the maven-shade-plugin
does not support Jigsaw yet: https://issues.apache.org/jira/browse/MSHADE-234
One can create runtime images with jlink
, but they cannot be put on the module path of another Java executable (They are also much bigger and platform dependent BTW).
There is also no concept of self-contained modules (jar with dependencies) in Jigsaw.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论