使用 maven-shade-plugin 加载 jar 文件
Loading a jar-file with maven-shade-plugin
我开发了一个API和一个依赖API的插件。所以我用的是类的API。问题是,我必须将这两个 jar 文件放在不同的文件夹中,并且在启动插件时出现 ClassNotFoundException。我问一个朋友如何解决这个问题,他回答说,我应该使用 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>
我开发了一个API和一个依赖API的插件。所以我用的是类的API。问题是,我必须将这两个 jar 文件放在不同的文件夹中,并且在启动插件时出现 ClassNotFoundException。我问一个朋友如何解决这个问题,他回答说,我应该使用 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>