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

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

问题描述

我想将我的项目打包在一个可执行的JAR中进行分发。

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

如何将所有依赖JAR的Maven项目包装到我的输出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

编译目标应该在汇编之前添加:单个或者不包含您自己项目中的代码。

在注释中查看更多详细信息。

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天全站免登陆