如何使用 Maven 创建具有依赖项的可执行 JAR? [英] How can I create an executable JAR with dependencies using Maven?

查看:39
本文介绍了如何使用 Maven 创建具有依赖项的可执行 JAR?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将我的项目打包到单个可执行 JAR 中以进行分发.

I want to package my project in a single executable JAR for distribution.

如何让 Maven 项目将所有依赖 JAR 打包到我的输出 JAR 中?

How can I make a Maven project package all dependency JARs into my output JAR?

推荐答案

<build>
  <plugins>
    <plugin>
      <artifactId>maven-assembly-plugin</artifactId>
      <configuration>
        <archive>
          <manifest>
            <mainClass>fully.qualified.MainClass</mainClass>
          </manifest>
        </archive>
        <descriptorRefs>
          <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
      </configuration>
    </plugin>
  </plugins>
</build>

然后你运行它

mvn clean compile assembly:single

编译目标应该在程序集之前添加:single 否则不包括您自己项目中的代码.

在评论中查看更多详细信息.

See more details in comments.

通常,此目标与构建阶段相关联以自动执行.这可确保在执行 mvn install 或执行部署/发布时构建 JAR.

Commonly this goal is tied to a build phase to execute automatically. This ensures the JAR is built when executing mvn install or performing a deployment/release.

<plugin>
  <artifactId>maven-assembly-plugin</artifactId>
  <configuration>
    <archive>
      <manifest>
        <mainClass>fully.qualified.MainClass</mainClass>
      </manifest>
    </archive>
    <descriptorRefs>
      <descriptorRef>jar-with-dependencies</descriptorRef>
    </descriptorRefs>
  </configuration>
  <executions>
    <execution>
      <id>make-assembly</id> <!-- this is used for inheritance merges -->
      <phase>package</phase> <!-- bind to the packaging phase -->
      <goals>
        <goal>single</goal>
      </goals>
    </execution>
  </executions>
</plugin>

这篇关于如何使用 Maven 创建具有依赖项的可执行 JAR?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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