用Maven 2构建一个可运行的jar [英] Building a runnable jar with Maven 2

查看:147
本文介绍了用Maven 2构建一个可运行的jar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对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 描述符。您还需要为此超级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

如果你想生成程序集作为构建的一部分,只需将程序集:单个 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆