使用Maven着色将外部jar添加到项目

gn

我的目标是在我的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。这里和整个互联网上都有很多问题和答案,但是似乎没有什么适合我的情况。

彼得·科泽尔卡

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

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

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

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Maven将属性文件添加到jar文件

如何使用gradle将外部文件添加到jar中?

将外部jar(lib / *。jar)添加到IntelliJ IDEA项目的正确方法

如何将本地jar文件添加到Maven项目?

如何使用Maven将Implementation-Version值添加到jar清单中?

将引用的Eclipse项目添加到Maven依赖项

Maven:将本地依赖项添加到jar

将外部JAR添加到Eclipse Application调试配置

将ojdbc添加到Maven项目

如何将.jar文件添加到BlueJ项目?

将外部JAR´S添加到JDK

将外部C ++库添加到CLion项目

将外部源文件添加到Kotlin项目

使用Spring Boot Maven插件将Maven构建时间添加到jar名称构建中

如何在不使用gradle或Maven或Eclipse的情况下将jar文件添加到Java项目

使用Maven将JAR文件添加到Eclipse中的项目中

java.lang.IllegalStateException / java.lang.ClassDefNotFoundError将外部Jar添加到android项目

将外部文件添加到jar

maven将项目的jar库添加到可执行jar

将jar文件添加到我的Xamarin项目

使用Maven从本地将JAR添加到WAR包装

将外部库添加到cmake项目

如何使用外部SQL查询将项目版本添加到jira数据库

如何将新的Maven项目添加到SVN

将构建任务添加到Maven Pom项目

如何使用 Maven Assembly 插件将文件从 webapp 目录添加到 jar?

如何使用 Gradle 将 jar 文件添加到 libgdx 项目

如何将外部 Maven 项目的依赖项添加到当前项目?

将外部 jar 添加到 Maven 存储库不起作用