如何在 jar 文件中包含 Maven 依赖项?

How to include maven dependencies in a jar file?

我有一个 eclipse Luna 项目,在 pom.xml

中定义了一堆 Maven 依赖项

eclipse 一切正常。但现在我需要将所有这些依赖项包含在一个可导出的 jar 文件中(以便我可以将它们发送给 Apache Spark 中的工作人员)。

我一直在摆弄导出设置,但我看不出有什么方法可以将它们导出到 jar 文件中。

我找到了一些解释如何配置 maven 来打包其依赖项的答案。这是我唯一的选择,还是有什么方法可以在 eclipse 中做到这一点?

看看这个问题:How can I create an executable JAR with dependencies using Maven?

我认为@Rocky Inde 的答案就是您要找的(使用 eclipse):

1) Just right-click on your project folder (in Eclipse) and select Export

2) Then select Java -> Runnable Jar

3) You will be asked to choose the location of the jar file

4) Finally, select the class that has the Main method that you want to run and choose Package dependencies with the Jar file and click Finish

我认为这个问题是重复的:

如果你想用控制台和 Maven 来做,你可以看看这个线程:the link that @Tarik mentions

如果你想使用 Eclipse,看看这个:How to create a jar with external libraries included in eclipse?

然后您需要在 pom.xml 中包含 shade 插件,mvn package 将生成 shade jar(fat jar)this link 提供有关 shade 插件的信息

我建议你使用 Maven 程序集插件。来自使用页面:

For example, imagine that our project produces a JAR. If we want to create an assembly binary that includes our project's dependencies, we can take advantage of one of the Assembly Plugin's prefabricated descriptors. You configure it as follows in your project's pom.xml:

<project>
  [...]
  <build>
  [...]
  <plugins>
    <plugin>
      <!-- NOTE: We don't need a groupId specification because the group is
         org.apache.maven.plugins ...which is assumed by default.
       -->
      <artifactId>maven-assembly-plugin</artifactId>
      <version>2.5.3</version>
      <configuration>
        <descriptorRefs>
          <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
      </configuration>
      [...]
</project>