Use external libraries

Discussion in 'Spigot Plugin Development' started by MrSpomg, Jul 11, 2019.

  1. Hi!

    I wonder how use an external library during runtime, but without compiling it. The library wont be a plugin so it wont be loaded by Bukkit.

    For example: my plugin needs a method which is in a .jar I added as provided dependency on Maven (so is not compiled), the .jar will be located under some directory inside the plugin directory, and I will be able to access the methods of that library from my plugin.

    What shall I do? Load it with a ClassLoader...?

    Thanks!
     
    • Winner Winner x 1
  2. you can just shade those .jar file in your packaged .jar file.

    If you're using Maven, use maven-shade-plugin,
    If you're using Gradle, you can use something like com.github.johnrengelman.shadow plugin
     
  3. So did you try to set the scope as "compile" instead?
    (https://maven.apache.org/guides/int...to-dependency-mechanism.html#Dependency_Scope)

    Using "provided" tells maven that this dependency will be available at runtime, and thus, doesn't need to be included in your project build. The "compile" scope should be default, so you should just be able to remove the scope property entirely from the dependency.

    Using shading is an alternative.
     
  4. You both missed the crux of his question. He wants to have a "libraries" subfolder where he keeps his dependancies separate.
    He doesn't want a fatjar.
     
    • Agree Agree x 1
    • Informative Informative x 1
  5. seriously...
    The OP did not say he does not want fatjar .
    Sharing will have the same effect anyway.
     
  6. Not shading has its benefits also.
    (But a massive drawback being the need for internet first launch.)
     
  7. Actually he did? And also specifically mentioned he doesn't want to compile the dependency into the jar
    LuckPerms is the best implementation of runtime dependency downloading you'll find on here, but luck also has a dumbed down version of it in his helper library, which may be easier to follow. https://github.com/lucko/helper/tree/master/helper/src/main/java/me/lucko/helper/maven

    It is missing some crucial components however, for example hash verification. Something to also keep in mind, this method will only work on java 8 and lower. Both LuckPerms, and helper's implementation use a functionality of the URLClassLoader, which was removed in java 9.
     
    • Agree Agree x 1
    • Winner Winner x 1
  8. well only statement resembling to what you pointed out was "but without compiling it" but it does not clearly communicate whether that means he does not want to shade it. if he meant that he did not want to shade it, then there are few different ways to do it, you can find the jar file and manually load it or have your custom class loader based on the default loader.

    @Plajer optimistic? I'm already doing those in my plugins and working fine.
     
    #10 vk2gpz, Jul 11, 2019
    Last edited: Jul 11, 2019
    • Agree Agree x 1
    • Optimistic Optimistic x 1
  9. Ok guys, thanks so much! It is true that I didnt explain myself well enough, so its really awesone that some of you understood me. Yeah so what @Paradaux, @PiggyPiglet and @ExpDev said before is what I need.

    About the system used by LuckPerms, is it actually downloading the dependencies? I dont understand the lines of code in which you have to specify the mvn repo and artifact... Whats that for? Something else, the library I will use is actually a project with no actual code, but only other libraries (such as JDA) shaded with it. So, if I want to load the classes if this file, how will I do that? (I am newbie with the Reflection API)

    BTW, I cant shade it it because of the 4mb limit of Spigot.

    Thanks!
     
  10. You can use dependency management or the shade plugin’s exclude property to explicitly exclude some dependencies from being shaded (like JPA that already comes with spigot).
     
  11. I'm sure you won't be using all the class files in those depending jar files. you should just trace the reference tree and extract classes actually used and shade them.