【问题标题】:Adding external jar to project using maven-shading使用 maven-shading 将外部 jar 添加到项目中
【发布时间】:2018-09-30 08:11:18
【问题描述】:

我的目标是在我的 Maven 项目中添加一个外部 jar。运行项目会调用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。这里和整个互联网都有很多问题和答案,但似乎没有什么适合我的情况。

【问题讨论】:

    标签: maven maven-shade-plugin


    【解决方案1】:

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

    您的 pom 需要以下修复:

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

    • 将阴影插件版本修复为3.2.0(您的依赖项中正确设置了它,但插件声明中没有)

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

    【讨论】:

    • 修复版本号是我的问题的解决方案。感谢您的这一点以及有关依赖关系的提示!
    猜你喜欢
    • 1970-01-01
    • 2013-07-15
    • 1970-01-01
    • 1970-01-01
    • 2015-09-10
    • 1970-01-01
    • 1970-01-01
    • 2012-05-03
    • 1970-01-01
    相关资源
    最近更新 更多