问题描述
我对 Maven 咒语比较陌生,但我正在尝试使用 Maven 构建一个命令行可运行 jar.我已经设置了我的依赖项,但是当我运行 mvn install
并尝试运行 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 没有将我的依赖库打包在 jar 中,因此我无法将 jar 作为独立应用程序运行.我该如何纠正?
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?
推荐答案
最简单的方法是使用 maven-assembly-plugin
和预定义的 jar 创建程序集-with-dependencies
描述符.您还需要为这个 uber jar 生成一个带有主类条目的清单.下面的代码片段显示了如何配置程序集插件以执行此操作:
The easiest way to do this would be to create an assembly using the maven-assembly-plugin
and the predefined jar-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:
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>fully.qualified.MainClass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
然后,要生成程序集,只需运行:
Then, to generate the assembly, just run:
mvn assembly:assembly
如果您想在构建过程中生成程序集,只需将 assembly:single
mojo 绑定到包阶段:
If you want to generate the assembly as part of your build, simply bind the assembly:single
mojo to the package phase:
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>fully.qualified.MainClass</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
然后简单地运行:
mvn package
这篇关于使用 Maven 2 构建可运行的 jar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!