Use additional libraries and package them into jar


I am experimenting with JPA to access a database. But for this I need some external libraries.

In the development-environment it is no problem. I just add them in the “ProjectStructure” (IntelliJ) to SpongeVanilla_main and it works.

But when I generate the jar of my plugin, it doesn’t.

I tried it with the shadow-plugin for gradle:

and generated the jar with the gradle-task “shadowJar”.

When I put the generated jar into the mods-folder and start the server, I get:

java.lang.NoClassDefFoundError: javax/persistence/Persistence ... Caused by: java.lang.ClassNotFoundException: javax.persistence.Persistence

But when I open the char, there is the file: javax/persistence/Persistence.class

What can I do to make this work?

I don’t know why, but now it wworks. I changed nothing…

You probably ran ‘build’ the first time, and ‘shadowJar’ the second time. Add this to your build.gradle so it doesn’t happen again:

build.dependsOn shadowJar

You could also add jar.enabled = false to completely disable the non-shaded JAR (and add classifier = null to your shadowJar block to remove the -all from the shadowed JAR).

By the way, you can also exclude the Sponge dependency without stringing it by using a separate configuration; this is generally considered a better practice.

configurations {
    compile.extendsFrom shadow

shadowJar {
    configurations = [project.configurations.shadow]

Then just use shadow for the stuff you’re shadowing in instead of compile.

Lastly, you don’t need an apply statement for a plugin if you’re already using that plugin in the plugins block. That is in fact the purpose of the plugins block. This could also be the source of your problem - I don’t know if a plugin can be enabled twice at once, but if it could, this would do it.


great, thank you!