Solved Using Maven to add additional dependencies other than Bukkit

Discussion in 'Plugin Development' started by monjary, May 17, 2018.

Thread Status:
Not open for further replies.
  1. Offline

    monjary

    Before you read: I was a bit stumped on where to post this problem, so if this does not belong in the Bukkit forums I am sincerely sorry in advance. This is my first post here.

    Hello everyone,

    I have just got into using Maven when making plugins and they have worked perfectly fine. However, my current project needs another library, more specifically 'Reflections' to run. Although Bukkit and Spigot seem to add themselves perfectly fine, Reflections is nowhere to be found. The plugin always builds successfully with no errors. It should be noted that this is a test plugin and all it does is instantiate a Reflections object to test the class exists; rest assured, if I can get this test plugin to work, I will also be able to get my main plugin to work. No errors come up when I instantiate Reflections in my IDE (IntelliJ IDEA).

    Here is my pom.xml:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>me.monjary</groupId>
        <artifactId>maventest</artifactId>
        <version>1.0</version>
    
        <repositories>
            <repository>
                <id>spigot-repo</id>
                <url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
            </repository>
        </repositories>
    
        <dependencies>
            <dependency>
                <groupId>org.reflections</groupId>
                <artifactId>reflections</artifactId>
                <version>0.9.11</version>
            </dependency>
    
            <dependency>
                <groupId>org.bukkit</groupId>
                <artifactId>bukkit</artifactId>
                <version>1.12.2-R0.1-SNAPSHOT</version>
                <type>jar</type>
                <scope>provided</scope>
            </dependency>
    
            <dependency>
                <groupId>org.spigotmc</groupId>
                <artifactId>spigot-api</artifactId>
                <version>1.12.2-R0.1-SNAPSHOT</version>
                <type>jar</type>
                <scope>provided</scope>
            </dependency>
        </dependencies>
    
        <build>
            <finalName>MavenTest</finalName>
    
            <resources>
                <resource>
                    <directory>src/main/resources</directory>
                    <filtering>true</filtering>
                </resource>
            </resources>
        </build>
    </project>
    Here is the maven output:
    Code:
    [INFO] Scanning for projects...
    [INFO]                                                                        
    [INFO] ------------------------------------------------------------------------
    [INFO] Building maventest 1.0
    [INFO] ------------------------------------------------------------------------
    [INFO]
    [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maventest ---
    [WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
    [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
    [INFO] Copying 1 resource
    [INFO]
    [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maventest ---
    [INFO] Changes detected - recompiling the module!
    [WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
    [INFO] Compiling 1 source file to C:\Users\USERNAME\workspace\MavenTest\target\classes
    [INFO]
    [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maventest ---
    [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
    [INFO] skip non existing resourceDirectory C:\Users\USERNAME\workspace\MavenTest\src\test\resources
    [INFO]
    [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ maventest ---
    [INFO] Nothing to compile - all classes are up to date
    [INFO]
    [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maventest ---
    [INFO] No tests to run.
    [INFO]
    [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ maventest ---
    [INFO] Building jar: C:\Users\USERNAME\workspace\MavenTest\target\MavenTest.jar
    [INFO]
    [INFO] --- maven-install-plugin:2.4:install (default-install) @ maventest ---
    [INFO] Installing C:\Users\USERNAME\workspace\MavenTest\target\MavenTest.jar to C:\Users\USERNAME\.m2\repository\me\monjary\maventest\1.0\maventest-1.0.jar
    [INFO] Installing C:\Users\USERNAME\workspace\MavenTest\pom.xml to C:\Users\USERNAME\.m2\repository\me\monjary\maventest\1.0\maventest-1.0.pom
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 2.030 s
    [INFO] Finished at: 2018-05-17T19:04:27+01:00
    [INFO] Final Memory: 17M/164M
    [INFO] ------------------------------------------------------------------------
    
    Process finished with exit code 0
    Finally, here's the relevant stack trace for when the plugin is run:
    Code:
    17.05 20:16:09 [Server] INFO ... 12 more
    17.05 20:16:09 [Server] INFO at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_152]
    17.05 20:16:09 [Server] INFO at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_152]
    17.05 20:16:09 [Server] INFO at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:76) ~[CraftBukkit.jar:git-Bukkit-3a482bd]
    17.05 20:16:09 [Server] INFO at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:128) ~[CraftBukkit.jar:git-Bukkit-3a482bd]
    17.05 20:16:09 [Server] INFO at java.net.URLClassLoader.findClass(Unknown Source) ~[?:1.8.0_152]
    17.05 20:16:09 [Server] INFO Caused by: java.lang.ClassNotFoundException: org.reflections.Reflections
    17.05 20:16:09 [Server] INFO at java.lang.Thread.run(Unknown Source) [?:1.8.0_152]
    17.05 20:16:09 [Server] INFO at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:522) [CraftBukkit.jar:git-Bukkit-3a482bd]
    17.05 20:16:09 [Server] INFO at net.minecraft.server.v1_12_R1.DedicatedServer.init(DedicatedServer.java:248) [CraftBukkit.jar:git-Bukkit-3a482bd]
    17.05 20:16:09 [Server] INFO at net.minecraft.server.v1_12_R1.MinecraftServer.a(MinecraftServer.java:328) [CraftBukkit.jar:git-Bukkit-3a482bd]
    17.05 20:16:09 [Server] INFO at net.minecraft.server.v1_12_R1.MinecraftServer.l(MinecraftServer.java:373) [CraftBukkit.jar:git-Bukkit-3a482bd]
    17.05 20:16:09 [Server] INFO at net.minecraft.server.v1_12_R1.MinecraftServer.t(MinecraftServer.java:412) [CraftBukkit.jar:git-Bukkit-3a482bd]
    17.05 20:16:09 [Server] INFO at org.bukkit.craftbukkit.v1_12_R1.CraftServer.enablePlugins(CraftServer.java:328) [CraftBukkit.jar:git-Bukkit-3a482bd]
    17.05 20:16:09 [Server] INFO at org.bukkit.craftbukkit.v1_12_R1.CraftServer.enablePlugin(CraftServer.java:367) [CraftBukkit.jar:git-Bukkit-3a482bd]
    17.05 20:16:09 [Server] INFO at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:402) [CraftBukkit.jar:git-Bukkit-3a482bd]
    17.05 20:16:09 [Server] INFO at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:329) [CraftBukkit.jar:git-Bukkit-3a482bd]
    17.05 20:16:09 [Server] INFO at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:264) ~[CraftBukkit.jar:git-Bukkit-3a482bd]
    17.05 20:16:09 [Server] INFO at me.monjary.maventest.MavenTest.onEnable(MavenTest.java:11) ~[?:?]
    17.05 20:16:09 [Server] INFO java.lang.NoClassDefFoundError: org/reflections/Reflections
    17.05 20:16:09 [Server] ERROR Error occurred while enabling maventest v1.0 (Is it up to date?)
    Although I have tried quite a few things, I doubt I did any of them correctly. I'm open to any suggestions.

    Thanks in advance, everyone!
     
  2. Offline

    JanTuck

  3. Offline

    monjary

    Thanks for the quick reply and the info.
    However, I was hoping I could include Reflections in the .jar itself, as your reply seems to say that I can't put it into the .jar file. Is shading the way to go about this?
     
  4. Offline

    timtower Administrator Administrator Moderator

    @monjary Which Reflections are we talking about?
     
  5. Offline

    monjary

  6. Offline

    timtower Administrator Administrator Moderator

    @monjary Shading is your best option here.
    I personally prefer to have a library jar that I package in my plugin jar and extract + load when I enable the plugin.
     
  7. Offline

    monjary

    May I have some information on the erorr that is going on here? From what I've read up, it seems two versions of one dependency are needed and so Maven can't decide between the two.
     
  8. Offline

    monjary

Thread Status:
Not open for further replies.

Share This Page