★ Founding developer program: Pro free for 90 days for the first 50 developers →

Guides · Engineering

Shading and relocating libraries in a Minecraft plugin (Gradle and Maven)

Why your plugin needs to relocate the libraries it bundles, how to do it with Gradle Shadow or maven-shade, and how to fix the errors when it goes wrong.

Updated · by Skilled · 5 min read

Sooner or later your plugin needs a library the server doesn't provide: a database driver, a config library, an HTTP client, a metrics SDK. The usual answer is to put it inside your jar, called shading, and to move it into your own package, called relocating. Skip the second part and your plugin works fine on your test server, then breaks on a server that runs another plugin with the same library.

This guide covers why that happens, the build setup for Gradle and Maven, and the errors you'll see when something is off.

Why relocation matters

Bukkit-based servers let plugins see each other's classes. That's how plugin APIs work: your plugin can call Vault or PlaceholderAPI because their classes are visible to you.

The side effect is that two plugins bundling the same library can end up sharing one copy of it. If plugin A ships version 2 of a library and plugin B ships version 3, B may get A's version 2 classes at runtime. Sometimes nothing happens. Sometimes you get a NoSuchMethodError in code you didn't change, on servers you can't reproduce, depending on which plugin loaded first.

Relocation fixes it by rewriting the library's package names inside your jar. com.zaxxer.hikari becomes me.you.arenaplus.libs.hikari, every reference in your code is rewritten to match, and your copy can no longer collide with anyone else's.

What to shade and what not to

  • Shade: libraries the server doesn't have. Relocate every one of them.
  • Don't shade: the server API (Spigot, Paper, Velocity), or other plugins you depend on. Those are compileOnly in Gradle and provided in Maven. Shading the Bukkit API into your jar makes it huge and causes class conflicts.
  • Think twice about: libraries the server already ships. Paper and Spigot bundle things like Gson and Guava; if you can use the server's copy, you don't need your own.

Gradle with Shadow

The Shadow plugin builds a jar with your dependencies inside. Kotlin DSL:

plugins {
    java
    id("com.gradleup.shadow") version "8.3.9"
}

dependencies {
    compileOnly("io.papermc.paper:paper-api:1.21.1-R0.1-SNAPSHOT")
    implementation("com.zaxxer:HikariCP:5.1.0")
}

tasks.shadowJar {
    archiveClassifier.set("")
    relocate("com.zaxxer.hikari", "me.you.arenaplus.libs.hikari")
}

tasks.build {
    dependsOn(tasks.shadowJar)
}

A few details that save time:

  • implementation dependencies go into the shadow jar; compileOnly ones don't.
  • archiveClassifier.set("") makes the shaded jar the main one, so you don't upload the jar without your libraries by mistake.
  • Relocate into a sub-package of your plugin, like me.you.arenaplus.libs. It's the convention, and it makes stack traces easy to read.
  • Libraries that pull in their own dependencies need those relocated too. Check what ended up in the jar (below) and add a relocate for each top-level package.

Maven with maven-shade-plugin

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-shade-plugin</artifactId>
  <version>3.6.0</version>
  <executions>
    <execution>
      <phase>package</phase>
      <goals>
        <goal>shade</goal>
      </goals>
      <configuration>
        <createDependencyReducedPom>false</createDependencyReducedPom>
        <relocations>
          <relocation>
            <pattern>com.zaxxer.hikari</pattern>
            <shadedPattern>me.you.arenaplus.libs.hikari</shadedPattern>
          </relocation>
        </relocations>
      </configuration>
    </execution>
  </executions>
</plugin>

Dependencies in the default compile scope go into the jar; mark the server API as provided. mvn package then writes the shaded jar to target/.

Check the jar, every time you change the build

Don't trust the build script, look inside the jar:

unzip -l build/libs/ArenaPlus.jar | grep -v "me/you/arenaplus"

Everything left in that list, apart from plugin.yml, resources and META-INF, is a class outside your package. Each one is a collision waiting to happen. Add a relocate for it, or move it to compileOnly if the server already provides it.

The errors, and what they usually mean

NoClassDefFoundError or ClassNotFoundException for a library class. The library isn't in the jar you uploaded. Usually you put the plain jar output on the server instead of the shadow one, or the dependency is compileOnly/provided when it should be bundled.

NoSuchMethodError in library code, only on some servers. Another plugin's copy of the same library won. Relocate yours.

ClassCastException saying a class can't be cast to itself. Two copies of the same class, loaded by different class loaders. Same fix: relocate, and make sure you aren't also shading a plugin you depend on.

A library stops working after relocation. Some libraries find classes by name at runtime, through reflection or META-INF/services files. Shadow's mergeServiceFiles() rewrites service files to the relocated names; for string-based reflection, check the library's own docs for shading notes.

Your jar suddenly got very big. Something like the server API or Kotlin's standard library got bundled. Look at the jar listing and move it to compileOnly.

The alternative: libraries in plugin.yml

Spigot can download libraries for you: list Maven Central coordinates under libraries: in plugin.yml and the server fetches them at startup and gives them to your plugin's class loader. Your jar stays small, and each plugin gets its own copy, so relocation isn't needed for them.

libraries:
  - com.zaxxer:HikariCP:5.1.0

The trade-offs: it only works for libraries on Maven Central, the server needs internet access on the first start, and it doesn't exist on very old server versions. Paper plugins that use paper-plugin.yml have their own loader API for the same job. For a small library, or one that has to work everywhere, shading is still the simpler choice.

A real example: an SDK that checks for itself

Some libraries refuse to run unrelocated, precisely because of these collisions. bStats is one. The PluginAnalytics SDK is another: it compares its own package name with the original at startup, and if it wasn't relocated it turns itself off with a warning instead of sharing state with another plugin's copy:

[ArenaPlus] PluginAnalytics is disabled: relocate com.gamepathics.pluginanalytics into your plugin's package.

Your plugin keeps working either way. The setup is one more relocate line next to your other libraries (installation). If you also obfuscate, relocate first and run ProGuard on the shaded jar, as in obfuscating a plugin with ProGuard and Gradle.

Checklist

  • Server API and plugin dependencies: compileOnly or provided.
  • Every bundled library relocated into yourpackage.libs.
  • The shaded jar is the one you upload, and the plain one isn't produced or is clearly named.
  • unzip -l shows nothing outside your package.
  • Test once on a server with a few popular plugins installed, not only on an empty one.
Skilled

Makes Minecraft plugins (including The Sift on SpigotMC) and PluginAnalytics, so other plugin developers can see how their plugins are found, installed and used.

More guides

Engineering

Obfuscating a Minecraft plugin with ProGuard and Gradle (without breaking stack traces)

A working ProGuard setup for Spigot and Paper plugins with Gradle: the task, the keep rules Bukkit needs, and readable stack traces with mappings.

3 min read
Engineering

Error tracking for Minecraft plugins: find the bug before the 1-star review

How Spigot and Paper plugin exceptions reach the console, why waiting for reports fails, and how to collect, group and fix errors from every server.

4 min read

See how your own plugin is found and used

Free for your first plugin or mod. One line of code.