I’ve just finished writing my first Sponge Plugin and as a compiled artefact the jar file is ~4.7MB.
My plugin relies on a number of other libraries that are imported via Maven and these seem to be bundled directly into the jar file and as a result increases the size of the overall plugin.
I’m wondering if there’s a way that I can decrease the size of the compiled jar file? I’m thinking along the lines of excluding unused parts of 3rd party dependency jars? I did try looking at using ProGuard but trying to process my plugin using this tool reports almost 2000 warnings that must be corrected before it can attempt to shrink my plugin.
Can anyone advise on a method to make my plugin jar smaller?
If you’re using maven, you can configure the maven-shade-plugin to only include the stuff you actually use.
It also sports the option minimizeJar. If that is set to true, all classes that are not explicitly referred in your plugin code will be excluded from the shaded artifact (will still respect your specified inclusion filters).
I suspect there might be something equivalent for gradle, but I cannot give any info on that.
I’ve just tried to use the Shade plugin, but I don’t think I’m using it correctly. As per the above link you provided, I have added the following to the build section of my POM.xml
It is possible that if your plugin somehow (directly or indirectly) references all classes in your libraries, so none can be removed by the plugin. But I consider that rather unlikely. Is your project available on a public git repo?
If you open the JAR file as a zip, what are the contents? You definitely don’t want to be adding Sponge/SpongeAPI into the jar - that gets loaded at run time. Same goes for the other default libraries like Guava.
Currently its hosted on a private git repo that I host, so there’s currently no public access to it.
I’ve just exacted the jar file and it does look as though all of my dependencies that were defined in POM.xml are included in the output jar (including what looks like the full sponge api)
Does that mean that I’ve got my POM.xml setup incorrectly? I’ve uploaded a copy of POM.xml to PasteBin Here
Hm, nothing obviously wrong in there. Are you using mvn to compile the jar, or are you using your IDE’s tools? Most IDE’s will automatically include all dependencies (eg those loaded by maven) when they compile.
Add <scope>provided</scope> to all the dependencies that you know to be present on the sponge server. AFAIK that should be all except for that findbugs thingy and configurate-json.
I’m using IntelliJ IDEA to compile an artefact at the end of each build. If I look in Project Settings -> Artifacts, in the Output Layout tab, I can see each of the dependencies listed with the word extracted before each one.
I’ve just tried to build with mvn but it complains that it can’t find the POM file for the Sponge API.
It’s amazing what you don’t see until someone else points it out to you. I’ve removed my artefact as a build plugin from POM.xml and added the Scope Provided section to each of the dependancies (other that the two custom ones) and my output size is not 422KB
Thanks to you both for your support, it’s greatly appreciated.