Java 如何在 jar 文件中包含 Maven 依赖项?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28334606/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to include maven dependencies in a jar file?
提问by bernie2436
I have an eclipse Luna project with a bunch of maven dependencies defined in a pom.xml
我有一个 Eclipse Luna 项目,在 pom.xml 中定义了一堆 maven 依赖项
Everything works fine in eclipse. But now I need to include all of those dependencies in an exportable jar file (so that I can ship them to workers in Apache Spark).
在 Eclipse 中一切正常。但是现在我需要将所有这些依赖项包含在一个可导出的 jar 文件中(以便我可以将它们发送给 Apache Spark 中的工作人员)。
I keep fiddling with the export settings, but I don't see any way to export them into the jar file.
我一直在摆弄导出设置,但我没有看到任何将它们导出到 jar 文件的方法。
I find some answers explaining how to configure maven to package its dependencies. Is that my only option, or is there some way to do this in eclipse?
我找到了一些解释如何配置 maven 以打包其依赖项的答案。这是我唯一的选择,还是有什么方法可以在日食中做到这一点?
采纳答案by Tarik
Take a look at this question: How can I create an executable JAR with dependencies using Maven?
看看这个问题:How can I create an executable JAR with dependencies using Maven?
I think that @Rocky Inde's answer is what you are looking for (using eclipse):
我认为@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
1) 只需右键单击您的项目文件夹(在 Eclipse 中)并选择导出
2) 然后选择 Java -> Runnable Jar
3) 系统会要求您选择 jar 文件的位置
4) 最后,选择要运行的 Main 方法所在的类,然后选择 Package dependencies with the Jar 文件,然后单击 Finish
回答by Iker Aguayo
I think that this question is duplicated:
我认为这个问题是重复的:
If you want to do it with the console and Maven you could take a look into this thread: the link that @Tarik mentions
如果你想用控制台和 Maven 来做,你可以看看这个线程:@Tarik 提到的链接
If you want to use Eclipse, take a look into this one: How to create a jar with external libraries included in eclipse?
如果您想使用 Eclipse,请看一看:如何使用 Eclipse 中包含的外部库创建 jar?
回答by kasinavijay
回答by Zoltán
I suggest you use Maven assembly plugin. From the usage page:
我建议您使用 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:
例如,假设我们的项目生成一个 JAR。如果我们想创建一个包含项目依赖项的程序集二进制文件,我们可以利用程序集插件的预制描述符之一。您在项目的 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>