使用Maven阴影将外部jar添加到项目

时间:2018-09-30 08:11:19

标签: maven maven-shade-plugin

我的目标是在我的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被作为依赖项引用,并作为构建的插件包含在内。我正在使用预安装的maven集成通过Eclipse IDE编译项目,例如以下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。这里和整个互联网上都有很多问题和答案,但是似乎没有什么适合我的情况。

1 个答案:

答案 0 :(得分:1)

我不是在使用Eclipse,而是从命令行调用Maven,并且为了使它正常工作,我会向您推荐相同的东西。 一旦Maven构建成功,您就可以尝试使用Eclipse进行下一个挑战,无论如何这都不难。

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

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

  • 将阴影插件版本修复为3.2.0(您的依赖项中正确安装了该插件,但插件声明中未正确安装)

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