簡體   English   中英

使用 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>

這是控制台 output:

[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 的依賴項應該是提供的 scope,這樣當您將 api 與您的模塊重疊時,依賴項不會重疊(剛剛發現)。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM