带有 JDA 的 Maven 阴影插件

Posted

技术标签:

【中文标题】带有 JDA 的 Maven 阴影插件【英文标题】:Maven shade plugin with JDA 【发布时间】:2021-02-22 12:10:14 【问题描述】:

我正在尝试制作一个 Minecraft 插件,可以将消息从我的世界发送到不和谐。我想使用 JDA(Java Discord API)。但我收到一个错误:

java.lang.NoClassDefFoundError: net/dv8tion/jda/api/JDABuilder
    at me.lisuu.discord.bot.Main.startBot(Main.java:26) ~[?:?]
    at me.lisuu.discord.bot.Main.onEnable(Main.java:22) ~[?:?]
    at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:263) ~[spigot-1.16.3.jar:git-Spigot-2740d5a-890130b]
    at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:351) ~[spigot-1.16.3.jar:git-Spigot-2740d5a-890130b]
    at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:480) ~[spigot-1.16.3.jar:git-Spigot-2740d5a-890130b]
    at org.bukkit.craftbukkit.v1_16_R2.CraftServer.enablePlugin(CraftServer.java:494) ~[spigot-1.16.3.jar:git-Spigot-2740d5a-890130b]
    at org.bukkit.craftbukkit.v1_16_R2.CraftServer.enablePlugins(CraftServer.java:408) ~[spigot-1.16.3.jar:git-Spigot-2740d5a-890130b]
    at net.minecraft.server.v1_16_R2.MinecraftServer.loadWorld(MinecraftServer.java:435) ~[spigot-1.16.3.jar:git-Spigot-2740d5a-890130b]
    at net.minecraft.server.v1_16_R2.DedicatedServer.init(DedicatedServer.java:216) ~[spigot-1.16.3.jar:git-Spigot-2740d5a-890130b]
    at net.minecraft.server.v1_16_R2.MinecraftServer.w(MinecraftServer.java:808) ~[spigot-1.16.3.jar:git-Spigot-2740d5a-890130b]
    at net.minecraft.server.v1_16_R2.MinecraftServer.lambda$0(MinecraftServer.java:164) ~[spigot-1.16.3.jar:git-Spigot-2740d5a-890130b]
    at java.lang.Thread.run(Thread.java:832) [?:?]
Caused by: java.lang.ClassNotFoundException: net.dv8tion.jda.api.JDABuilder
    at java.net.URLClassLoader.findClass(URLClassLoader.java:435) ~[?:?]
    at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:167) ~[spigot-1.16.3.jar:git-Spigot-2740d5a-890130b]
    at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:96) ~[spigot-1.16.3.jar:git-Spigot-2740d5a-890130b]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:589) ~[?:?]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:522) ~[?:?]
    ... 12 more

我在论坛上寻求帮助,但解决方案总是:You have to use maven shadow plugin。我不知道该怎么做。我是新手,所以有人可以帮忙吗? 这是我的 pom.xml 文件。

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>me.lisuu.discord.bot.maven</groupId>
  <artifactId>MavenDiscordBotLisuu</artifactId>
  <version>0.0.1-SNAPSHOT</version>
    <repositories>
        <!-- This adds the Spigot Maven repository to the build -->
        <repository>
            <id>spigot-repo</id>
            <url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
        </repository>
        <repository>
            <id>jcenter</id>
            <name>jcenter-bintray</name>
            <url>https://jcenter.bintray.com</url>
        </repository>
    </repositories>
  <dependencies>
    <!-- https://mvnrepository.com/artifact/junit/junit -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.13.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <!--This adds the Spigot API artifact to the build -->
        <groupId>org.spigotmc</groupId>
        <artifactId>spigot-api</artifactId>
        <version>1.16.4-R0.1-SNAPSHOT</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>net.dv8tion</groupId>
        <artifactId>JDA</artifactId>
        <version>4.2.0_168</version>
    </dependency>
  </dependencies>
</project>

【问题讨论】:

你看过shade插件的文档了吗?:maven.apache.org/plugins/maven-shade-plugin/index.html 是的,我做到了!这对我来说毫无意义! 【参考方案1】:

在这里

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>me.lisuu.discord.bot.maven</groupId>
    <artifactId>MavenDiscordBotLisuu</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <repositories>
        <!-- This adds the Spigot Maven repository to the build -->
        <repository>
            <id>spigot-repo</id>
            <url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
        </repository>
        <repository>
            <id>jcenter</id>
            <name>jcenter-bintray</name>
            <url>https://jcenter.bintray.com</url>
        </repository>
    </repositories>
    <dependencies>
        <!-- https://mvnrepository.com/artifact/junit/junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <!--This adds the Spigot API artifact to the build -->
            <groupId>org.spigotmc</groupId>
            <artifactId>spigot-api</artifactId>
            <version>1.16.4-R0.1-SNAPSHOT</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>net.dv8tion</groupId>
            <artifactId>JDA</artifactId>
            <version>4.2.0_168</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.4</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>your.main.class.if.any.MainClass</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

【讨论】:

我应该在这里放什么?我的插件主类还是JDA主类? 我对 minecraft 插件开发一无所知 - 恢复到文档,也许你不需要在那里放任何东西(并删除整个 部分) - 我建议在那里设置你的主类(在最坏的情况下场景-什么都不会发生:))。插件配置正确(有或没有 部分),它将为您创建 uber-jar。 它不工作,但感谢您的帮助!还有一个问题,我必须将 JDA 放到我的 .jar 文件中吗? maven-shade-plugin 正在将 JDA 放入您的 jar 文件中:) 您指的是“不工作”的哪一部分?阴影插件不适合你?还是插件本身? 我的插件没有任何 JDA 文件。如果我手动添加它们会怎样?

以上是关于带有 JDA 的 Maven 阴影插件的主要内容,如果未能解决你的问题,请参考以下文章

Maven 阴影插件重定位不更新资源文件中的条目

MAVEN 上的 Java BOT(带有 JDA 库)在 Heroku 上运行

带有 Scala 插件、Maven 和 Spark 的 Eclipse 项目

带有 Maven Shade 插件的 Spring Boot - 未映射控制器(404 错误)

带有 git 存储库的 Maven 发布插件忽略 scm.user scm.password

在 Maven 配置文件中使用带有 plexus-compiler-eclipse 插件的 JAXB