Search code examples
javajava-modulejava-platform-module-system

Do Java Modules (JPMS/Jigsaw) Solve the Problem that "Shading" Dependencies Solves?


A lot of Java projects shade their dependencies. I believe the main reason is illustrated by the answer on this SO question (What is the maven-shade-plugin used for, and why would you want to relocate Java packages?). 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?


Solution

  • 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.