且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用Maven创建没有依赖关系的可执行jar?

更新时间:2021-12-10 02:26:06

您可以在一定程度上达到此目的。

You can achieve this to a certain extent.

首先,您要创建一个可执行jar ,适当地配置 maven jar插件

Firstly, you would create an executable jar by configuring maven jar plugin suitably.

然后你将使用maven程序集插件创建一个jar-with-dependencies,不包括你的项目jar 。为此,您将创建一个描述符文件,例如 src / main / assembly / descriptor.xml ,就像这样。

You would then use maven assembly plugin to create a jar-with-dependencies, excluding your project jar. To do this, you would create a descriptor file, say src/main/assembly/descriptor.xml, like this.

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
  <id>jar-with-dependencies</id>
  <formats>
    <format>jar</format>
  </formats>
  <includeBaseDirectory>false</includeBaseDirectory>
  <dependencySets>
    <dependencySet>
      <outputDirectory>/</outputDirectory>
      <useProjectArtifact>false</useProjectArtifact>
      <unpack>true</unpack>
      <scope>runtime</scope>
    </dependencySet>
  </dependencySets>
</assembly>

在你的项目中使用它。

<project>
  [...]
  <build>
    [...]
    <plugins>
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.2.1</version>
        <configuration>
          <descriptors>
            <descriptor>src/main/assembly/descriptor.xml</descriptor>
          </descriptors>
        </configuration>
        [...]
      </plugin>
    </plugins>
  </build>
</project>

你最终会得到两个罐子 - 一个是你的项目创建的可执行jar,另一个是罐子由程序集插件创建的依赖项。

You will end up getting two jars - one the executable jar created by your project and the other the jar-with-dependencies created by the assembly plugin.