Skip to content

Shaded JAR: multi-release entry paths are not relocated, leaving class name/path mismatches #17

Closed

Description

@PIsberg

What is wrong

maven-shade-plugin rewrites the contents of classes inside META-INF/versions/N/ but does not rewrite their entry paths. The shaded nanometer-api-*-all.jar therefore contains entries like:

META-INF/versions/24/net/bytebuddy/jar/asmjdkbridge/AsmAttribute.class

whose class file actually declares:

class nanometer.shaded.bytebuddy.jar.asmjdkbridge.AsmAttribute

The path says net/bytebuddy, the class says nanometer.shaded.bytebuddy. If such an entry were ever loaded, the JVM would reject it with NoClassDefFoundError: ... wrong name.

Why it does not break today

The shaded JAR's manifest does not carry Multi-Release: true. The ManifestResourceTransformer builds the manifest from the project's own JAR plus the configured manifestEntries, and Byte Buddy's own Multi-Release: true is not carried over. Without that attribute the JVM ignores META-INF/versions/* completely, so the mismatched entries are dead weight that is never loaded.

The JAR works by accident of a missing manifest attribute rather than by design.

Why it is worth fixing

  • Anyone who later adds Multi-Release: true, or swaps in a manifest transformer that preserves it, converts ~41 dead entries into load failures with no obvious connection to the change that caused them.
  • The artifact ships bytes that can never execute.
  • On Java 24+, Byte Buddy silently falls back to its bundled ASM instead of the JDK ClassFile API bridge it ships those entries for. That fallback is functional (the suite passes on JDK 21, 25 and 26) but it is an unintended performance/behaviour choice rather than a decided one.

Scope

This predates the Byte Buddy bump. It already affects the relocated org.sqlite entries under META-INF/versions/9/ on main today, verified by decompiling META-INF/versions/9/org/sqlite/nativeimage/SqliteJdbcFeature.class from a main build and finding it declares nanometer.shaded.sqlite.nativeimage.SqliteJdbcFeature. PR #16 adds 32 more such entries from Byte Buddy 1.18.13.

What it would take

Decide between:

  1. Strip META-INF/versions/** from the shaded artifact with a shade filter, making the fallback explicit and the JAR honest about what it contains. Removes the JDK ClassFile API path permanently.
  2. Keep the versioned entries, relocate their paths correctly, and set Multi-Release: true so they are actually used. Needs a check of whether current shade can relocate MR-JAR paths, and a test that loads a versioned class on JDK 24+.

Either way the result needs a check that fails if a shaded entry's path disagrees with its declared class name, so this cannot regress silently.

Why it was not done in #16

#16 is scoped to unblocking the dependency bump in CI. This is a pre-existing artifact-correctness question with a real design decision in it (option 1 versus option 2), and folding it into a build-unblock PR would mix a forced change with a discretionary one.

Activity

  1. PIsberg commented on Sep 11, 2026

    @PIsberg
    OwnerAuthor

    Fixed in fix/shade-plugin-mrjar-bytebuddy (PR #16), taking option 1 from the issue.

    The versioned entries are excluded from the shaded JAR. Since the manifest carries no Multi-Release attribute, the JVM already ignored every one of them, so dropping them removes bytes that could never execute rather than changing behaviour. Option 2 is not reachable with stock shade, which cannot relocate META-INF/versions/N/ entry paths.

    The filter covers every artifact rather than only the relocated ones: org.jspecify also contributes a META-INF/versions/9 module-info.class and OSGi metadata that an uber-JAR cannot use either.

    ShadedJarLayoutIT pins the invariants the issue asked for, and more:

    • every class entry's path matches the class it declares, read back with ASM
    • no META-INF/versions entries and no Multi-Release manifest attribute, so the two cannot drift into the dangerous combination
    • no unrelocated net/bytebuddy or org/sqlite classes, which is the zero-classpath-pollution guarantee
    • the Premain-Class, Agent-Class and retransform manifest entries survive shading

    Verified by removing the filter and rebuilding: classPathsMatchDeclaredNames and carriesNoMultiReleaseEntries both go red and name all 31 offending entries (28 from Byte Buddy 1.18.13, 3 from sqlite).

    Getting these tests to run at all required adding maven-failsafe-plugin to nanometer-api, so integration tests execute after package and can inspect the artifact that actually ships. The surefire suite only ever sees the unshaded module classpath, which is why both this and #18 were invisible.

    One consequence worth recording: Byte Buddy on JDK 24+ now permanently falls back to its bundled ASM rather than the JDK ClassFile API bridge those entries provided. That was already true before this change, since the entries were never loaded, but it is now an explicit decision rather than an accident.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions