使用 Maven 2 构建可运行的 jar
我对 Maven 口号比较陌生,但我正在尝试使用 Maven 构建一个命令行可运行的 jar。我已经设置了依赖项,但是当我运行 mvn install 并尝试运行 jar 时,会发生两件事。首先,没有找到主类,这是可以纠正的。当我更正这个问题后,我在运行时收到错误,指出找不到类。
Maven 没有将我的依赖库打包到 jar 中,因此我无法将 jar 作为独立应用程序运行。我该如何纠正这个问题?
I'm relatively new to the Maven mantra, but I'm trying to build a command-line runnable jar with Maven. I've setup my dependencies, but when I run mvn install
and attempt to run the jar, two things happen. First, no main class is found, which is correctable. When I've corrected this, I get errors on run stating that classes cannot be found.
Maven is not packaging my dependency libraries inside of the jar, so I am unable to run the jar as a stand-alone application. How do I correct this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
最简单的方法是使用
maven-assemble-plugin
和预定义的jar-with-dependencies
描述符创建程序集。您还需要生成一个清单,其中包含此 uber jar 的主类条目。下面的代码片段展示了如何配置程序集插件来执行此操作:然后,要生成程序集,只需运行:
如果您想生成程序集作为构建的一部分,只需绑定
assemble:single
打包阶段的魔力:只需运行:
The easiest way to do this would be to create an assembly using the
maven-assembly-plugin
and the predefinedjar-with-dependencies
descriptor. You'll also need to generate a manifest with a main-class entry for this uber jar. The snippet below shows how to configure the assembly plugin to do so:Then, to generate the assembly, just run:
If you want to generate the assembly as part of your build, simply bind the
assembly:single
mojo to the package phase:And simply run:
Maven 不会将您的依赖项打包到 jar 文件中,因为您通常不会使用 Java 程序执行此操作。
相反,您将依赖项与 jar 文件一起提供,并在 Manifest 的
Class-Path
标头。要走这条路线,您需要启用
addClasspath
属性(记录为 此处)用于maven- jar-插件
。如果您确实想在 jar 文件中包含所有依赖项,那么您可以使用 Maven Assembly 插件创建一个
jar-with-dependency
。Maven is not packaging your dependencies inside your jar file, because you don't usually do this with Java programs.
Instead you deliver the dependencies together with your jar file and mention them in the
Class-Path
header of the Manifest.To go this route, you'll need to enable the
addClasspath
property (documented here) for themaven-jar-plugin
.If you really want to include all your dependencies in your jar file, then you can use the Maven Assembly plugin to create a
jar-with-dependencies
.这就是我对小型项目所做的事情。大多数时候你不需要一个大罐子。
构建:
mvn clean dependency:复制依赖包
执行(在目标目录):
java -cp myjar.jar:./dependency/* com.something.MyClass
This is what I would do for small projects. Most of the time you don't want one huge jar.
to build:
mvn clean dependency:copy-dependencies package
to execute (in target dir):
java -cp myjar.jar:./dependency/* com.something.MyClass
我同意 Joachim Sauer 的观点,
您应该像 pom.xml 中那样配置 jar 插件,而不是使用 jar-with-dependency:
并使用此程序集配置将 jar 依赖项添加到程序集中:
I Agree with Joachim Sauer,
Instead of using jar-with-dependency you should configure the jar plugin like that in your pom.xml:
And use this assembly configuration to add the jar dependencies to you assembly:
只需在 pom.xml 中添加以下代码并运行为: maven:install 。该 jar 将在 eclipse 的目标文件夹中创建,可用作“java -jar Hello.jar”。但请确保主类的名称为 com.abc.HelloWorld.java
Just add the below code in pom.xml and Run as: maven:install . The jar will be created in target folder of eclipse which can be used as "java -jar Hello.jar" . but make sure that name of main class is given com.abc.HelloWorld.java
我有一个spring boot应用程序,maven包创建的jar可以运行而无需设置额外的插件。
在我的 pom.xml 中:
https://spring.io/projects/spring-boot
I have a spring boot application, and the jar created by maven package can be run without setting up additional plugins.
Inside my pom.xml:
https://spring.io/projects/spring-boot
要将依赖项打包到 JAR 中以便它可以作为独立应用程序运行,您需要配置 Maven Assembly Plugin 或 Maven Shade Plugin。以下是使用 Maven Shade 插件执行此操作的方法:
pom.xml
文件中的以下部分:org.mapReduce.WordCountRunner
替换为主类的完全限定名称。To package your dependencies inside the JAR so that it can be run as a standalone application, you need to configure the Maven Assembly Plugin or the Maven Shade Plugin. Here's how you can do it using the Maven Shade Plugin:
pom.xml
file inside the section:org.mapReduce.WordCountRunner
with the fully qualified name of your main class.mvn clean package
to create the shaded JAR.