gpt4 book ai didi

java - 使用 maven-shade-plugin 加载 jar 文件

转载 作者:行者123 更新时间:2023-12-01 17:26:00 25 4
gpt4 key购买 nike

我开发了一个API和一个具有API依赖的插件。所以我正在使用 API 的类。问题是,我必须将两个 jar 文件放在不同的文件夹中,并且在启动插件时遇到 ClassNotFoundException。我问一个 friend 如何解决这个问题,他回答说,我应该使用 maven-shade-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>de.iltisauge</groupId>
<artifactId>API-Plugin-Test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>Test-API</module>
<module>Test-Plugin</module>
</modules>
</project>

这是我的插件模块的 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>
<parent>
<groupId>de.iltisauge</groupId>
<artifactId>API-Plugin-Test</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>Test-Plugin</artifactId>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.2</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.8.8-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>de.iltisauge</groupId>
<artifactId>Test-API</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>

这是 api 模块的 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>
<parent>
<groupId>de.iltisauge</groupId>
<artifactId>API-Plugin-Test</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>Test-API</artifactId>
</project>

这是控制台输出:

[17:01:24 ERROR]: Could not load 'plugins\Test-Plugin-0.0.1-SNAPSHOT-shaded.jar' in folder 'plugins'
org.bukkit.plugin.InvalidPluginException: java.lang.NoClassDefFoundError: de/iltisauge/test/api/IHuman
at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:135) ~[spigot1.8.8.jar:git-Spigot-db6de12-18fbb24]
at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:329) ~[spigot1.8.8.jar:git-Spigot-db6de12-18fbb24]
at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:251) [spigot1.8.8.jar:git-Spigot-db6de12-18fbb24]
at org.bukkit.craftbukkit.v1_8_R3.CraftServer.loadPlugins(CraftServer.java:292) [spigot1.8.8.jar:git-Spigot-db6de12-18fbb24]
at net.minecraft.server.v1_8_R3.DedicatedServer.init(DedicatedServer.java:198) [spigot1.8.8.jar:git-Spigot-db6de12-18fbb24]
at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:525) [spigot1.8.8.jar:git-Spigot-db6de12-18fbb24]
at java.lang.Thread.run(Unknown Source) [?:1.8.0_241]
Caused by: java.lang.NoClassDefFoundError: de/iltisauge/test/api/IHuman
at java.lang.Class.forName0(Native Method) ~[?:1.8.0_241]
at java.lang.Class.forName(Unknown Source) ~[?:1.8.0_241]
at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:64) ~[spigot1.8.8.jar:git-Spigot-db6de12-18fbb24]
at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:131) ~[spigot1.8.8.jar:git-Spigot-db6de12-18fbb24]
... 6 more
Caused by: java.lang.ClassNotFoundException: de.iltisauge.test.api.IHuman
at java.net.URLClassLoader.findClass(Unknown Source) ~[?:1.8.0_241]
at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:101) ~[spigot1.8.8.jar:git-Spigot-db6de12-18fbb24]
at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:86) ~[spigot1.8.8.jar:git-Spigot-db6de12-18fbb24]
at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_241]
at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_241]
at java.lang.Class.forName0(Native Method) ~[?:1.8.0_241]
at java.lang.Class.forName(Unknown Source) ~[?:1.8.0_241]
at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:64) ~[spigot1.8.8.jar:git-Spigot-db6de12-18fbb24]
at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:131) ~[spigot1.8.8.jar:git-Spigot-db6de12-18fbb24]
... 6 more

最佳答案

让子模块依赖于 api。值得注意的是,api 的依赖项应该在提供的范围内,这样当您使用模块遮蔽 api 时,依赖项就不会重叠(刚刚发现这一点)。

<dependencies>
<dependency>
<groupId>de.iltisauge</groupId>
<artifactId>API-Plugin-Test</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>

关于java - 使用 maven-shade-plugin 加载 jar 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61210889/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com