使用 maven-shading 添加外部 jar 到项目

Adding external jar to project using maven-shading

我的目标是将外部 jar 添加到我的 Maven 项目中。 运行 项目调用 NoClassDefFoundError。所以我做了一些研究,发现我创建的 jar 文件中可能没有包含外部 jar。

因此我找到了使用 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>this.isa.test</groupId>
  <artifactId>test</artifactId>
  <version>0.0.1</version>
  <build>
   <plugins>
       <plugin>
           <groupId>org.apache.maven.plugins</groupId>
           <artifactId>maven-compiler-plugin</artifactId>
           <configuration>
               <source>1.8</source>
               <target>1.8</target>
           </configuration>
       </plugin>
       <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>3.2</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
   </plugins>
</build>
<repositories>
    <repository>
      <id>spigot-repo</id>
      <url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
    </repository>
</repositories>
<dependencies>
       <dependency>
           <groupId>org.bukkit</groupId>
           <artifactId>bukkit</artifactId>
           <version>1.12.2-R0.1-SNAPSHOT</version><!--change this value depending on the version or use LATEST-->
           <type>jar</type>
           <scope>provided</scope>
       </dependency>
       <dependency>
           <groupId>org.spigotmc</groupId>
           <artifactId>spigot-api</artifactId>
           <version>1.12.2-R0.1-SNAPSHOT</version><!--change this value depending on the version-->
           <type>jar</type>
           <scope>provided</scope>
       </dependency>
       <dependency>
           <groupId>org.apache.maven.plugins</groupId>
           <artifactId>maven-shade-plugin</artifactId>
           <version>3.2.0</version>
         <type>maven-plugin</type>
        </dependency>
   </dependencies>
</project>

请注意,maven-shade-plugin 被引用为依赖项并作为构建的插件包含在内。我正在通过 Eclipse IDE 使用预安装的 Maven 集成编译项目,如下所示 mvn-install.

控制台中将给出以下错误消息:

[INFO] --------------------------------[ jar ]---------------------------------
[WARNING] The POM for org.apache.maven.plugins:maven-shade-plugin:jar:3.2 is missing, no dependency information available
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.195 s
[INFO] Finished at: 2018-09-30T09:48:43+02:00
[INFO] ------------------------------------------------------------------------
[ERROR] Plugin org.apache.maven.plugins:maven-shade-plugin:3.2 or one of its dependencies could not be resolved: Failure to find org.apache.maven.plugins:maven-shade-plugin:jar:3.2 in https://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginResolutionException

老实说,我有点受不了了,因为这是我第一次尝试使用 Maven。这里和整个互联网上有很多问题和答案,但似乎没有适合我的情况。

我没有使用 Eclipse,而是从命令行调用 maven - 为了使它正常工作,我会向您推荐相同的方法。 maven 构建工作后,您可以尝试使用 Eclipse 进行下一个挑战,无论如何这都不难。

您的 pom 需要进行以下修复:

  • 从依赖项中删除 shade 插件;依赖项应仅包含 javac 编译项目所需的工件,在您的情况下还应包含运行时所需的工件,但绝不(除了极少数例外)maven 工具工件

  • 将 shade 插件版本修复为 3.2.0(您的依赖项中有正确的版本,但插件声明中没有)

然后在命令行上尝试 mvn clean install(确保你在你的 pom 文件所在的同一目录中)并且它应该工作 - 至少它对我有用。