Maven Shade跳过MCCoroutine类文件致Spigot插件启动失败求助
Kotlin+Maven构建Spigot插件时MCCoroutineImpl类缺失问题解决
问题概述
用Kotlin和Maven构建的Spigot插件运行时抛出类缺失错误,打包生成的jar文件中找不到com.github.shynixn.mccoroutine.bukkit.impl.MCCoroutineImpl,执行ls -R命令已确认该问题。不过impl目录下存在一个命名特殊的文件:./com/github/shynixn/mccoroutine/bukkit/impl: 'CoroutineSessionImpl$init$inlined$Runnable$1.class'。
环境细节
- Kotlin v1.9.0
- Kotlin插件 232-1.9.20-release-507-IJ10072.27
- Maven Shade插件 v3.5.1(v3.5.0版本也存在相同问题)
- Maven编译器插件 v3.11.0
- 已启用
minimizeJar配置
构建日志
[INFO] Including com.github.shynixn.mccoroutine:mccoroutine-bukkit-api:jar:2.13.0 in the shaded jar. [INFO] Including com.github.shynixn.mccoroutine:mccoroutine-bukkit-core:jar:2.13.0 in the shaded jar.
运行时错误日志
java.lang.RuntimeException: Failed to load MCCoroutine implementation. Shade mccoroutine-bukkit-core into your plugin. at com.github.shynixn.mccoroutine.bukkit.MCCoroutineKt$mcCoroutine$2.invoke(MCCoroutine.kt:20) ~[?:?] at com.github.shynixn.mccoroutine.bukkit.MCCoroutineKt$mcCoroutine$2.invoke(MCCoroutine.kt:15) ~[?:?] at me.sd_master92.kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74) ~[?:?] at com.github.shynixn.mccoroutine.bukkit.MCCoroutineKt.getMcCoroutine(MCCoroutine.kt:15) ~[?:?] at com.github.shynixn.mccoroutine.bukkit.MCCoroutineKt.getMinecraftDispatcher(MCCoroutine.kt:40) ~[?:?] at com.github.shynixn.mccoroutine.bukkit.MCCoroutineKt.launch$default(MCCoroutine.kt:85) ~[?:?] at me.sd_master92.customvoting.CV.enable(CV.kt:53) ~[?:?] at me.sd_master92.core.plugin.CustomPlugin.onEnable(CustomPlugin.kt:42) ~[?:?] at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:266) ~[spigot-api-1.20.2-R0.1-SNAPSHOT.jar:?] at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:342) ~[spigot-api-1.20.2-R0.1-SNAPSHOT.jar:?] at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:480) ~[spigot-api-1.20.2-R0.1-SNAPSHOT.jar:?] at org.bukkit.craftbukkit.v1_20_R2.CraftServer.enablePlugin(CraftServer.java:548) ~[spigot-1.20.2-R0.1-SNAPSHOT.jar:3943-Spigot-7de1049-8e162d0] at org.bukkit.craftbukkit.v1_20_R2.CraftServer.enablePlugins(CraftServer.java:462) ~[spigot-1.20.2-R0.1-SNAPSHOT.jar:3943-Spigot-7de1049-8e162d0] at org.bukkit.craftbukkit.v1_20_R2.CraftServer.reload(CraftServer.java:970) ~[spigot-1.20.2-R0.1-SNAPSHOT.jar:3943-Spigot-7de1049-8e162d0] at org.bukkit.Bukkit.reload(Bukkit.java:834) ~[spigot-api-1.20.2-R0.1-SNAPSHOT.jar:?] at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:27) ~[spigot-api-1.20.2-R0.1-SNAPSHOT.jar:?] at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:149) ~[spigot-api-1.20.2-R0.1-SNAPSHOT.jar:?] at org.bukkit.craftbukkit.v1_20_R2.CraftServer.dispatchCommand(CraftServer.java:879) ~[spigot-1.20.2-R0.1-SNAPSHOT.jar:3943-Spigot-7de1049-8e162d0] at org.bukkit.craftbukkit.v1_20_R2.CraftServer.dispatchServerCommand(CraftServer.java:864) ~[spigot-1.20.2-R0.1-SNAPSHOT.jar:3943-Spigot-7de1049-8e162d0] at net.minecraft.server.dedicated.DedicatedServer.bf(DedicatedServer.java:412) ~[spigot-1.20.2-R0.1-SNAPSHOT.jar:3943-Spigot-7de1049-8e162d0] at net.minecraft.server.dedicated.DedicatedServer.b(DedicatedServer.java:388) ~[spigot-1.20.2-R0.1-SNAPSHOT.jar:3943-Spigot-7de1049-8e162d0] at net.minecraft.server.MinecraftServer.a(MinecraftServer.java:1179) ~[spigot-1.20.2-R0.1-SNAPSHOT.jar:3943-Spigot-7de1049-8e162d0] at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:996) ~[spigot-1.20.2-R0.1-SNAPSHOT.jar:3943-Spigot-7de1049-8e162d0] at net.minecraft.server.MinecraftServer.lambda$0(MinecraftServer.java:298) ~[spigot-1.20.2-R0.1-SNAPSHOT.jar:3943-Spigot-7de1049-8e162d0] at java.lang.Thread.run(Thread.java:1583) ~[?:?] Caused by: java.lang.ClassNotFoundException: com.github.shynixn.mccoroutine.bukkit.impl.MCCoroutineImpl at org.bukkit.plugin.java.PluginClassLoader.loadClass0(PluginClassLoader.java:160) ~[spigot-api-1.20.2-R0.1-SNAPSHOT.jar:?] at org.bukkit.plugin.java.PluginClassLoader.loadClass(PluginClassLoader.java:112) ~[spigot-api-1.20.2-R0.1-SNAPSHOT.jar:?] at java.lang.ClassLoader.loadClass(ClassLoader.java:526) ~[?:?] at java.lang.Class.forName0(Native Method) ~[?:?] at java.lang.Class.forName(Class.java:421) ~[?:?] at java.lang.Class.forName(Class.java:412) ~[?:?] at com.github.shynixn.mccoroutine.bukkit.MCCoroutineKt$mcCoroutine$2.invoke(MCCoroutine.kt:17) ~[?:?] ... 24 more
解决方法
问题根源是Maven Shade插件的minimizeJar配置会移除它认为未被直接引用的类,而MCCoroutineImpl是通过反射加载的,Shade插件无法识别这种间接引用关系,导致被误删。
方案1:禁用minimizeJar
在Maven Shade插件配置中删除minimizeJar参数,或者将其设置为false。此方法会保留所有依赖类,虽然jar体积会增大,但能彻底避免类被误删。
方案2:强制保留特定类
如果需要保留minimizeJar的优化功能,可以在Shade插件配置中添加过滤规则,强制保留mccoroutine-bukkit-core中impl包下的所有类:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.5.1</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <minimizeJar>true</minimizeJar> <filters> <filter> <artifact>com.github.shynixn.mccoroutine:mccoroutine-bukkit-core</artifact> <includes> <include>com/github/shynixn/mccoroutine/bukkit/impl/**</include> </includes> </filter> </filters> </configuration> </execution> </executions> </plugin>
该配置会确保反射加载的实现类不会被Shade插件的最小化功能移除。
内容的提问来源于stack exchange,提问作者sd_master92
相关产品推荐
相关产品推荐

