【问题标题】:How can I create an executable jar without dependencies using Maven?如何使用 Maven 创建没有依赖关系的可执行 jar?
【发布时间】:2011-11-30 12:17:06
【问题描述】:

我不想将它打包到单个可执行 jar 中以进行分发。我需要一个类似于 main.jar 的可执行文件,并且所有依赖项都在 libs/*.jar 中

如何在不预先包含依赖库的情况下制作 maven 可执行 jar?

在 How can I create an executable JAR with dependencies using Maven? 中有一条注释,由 2010 年 12 月 1 日 10:46 回答 André Aronsen,但那个根本不起作用(未设置失败的 s.a.descriptorRef)。

【问题讨论】:

  • 你看过onejar-maven-plugin吗? code.google.com/p/onejar-maven-plugin
  • 这个插件正好相反 - 它将所有 jar 包含在一个主 jar 存档中,而不是在子目录中。
  • 我使用 Capsule,功能比我需要的多

标签: java maven pom.xml


【解决方案1】:

我更喜欢这个稍加修改的解决方案。创建带有类路径集的可执行 jar 并将所有依赖项复制到给定目录。

您不需要任何其他文件。

在安装阶段正在复制依赖项。

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                        <mainClass>fully.qualified.MainClass</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <phase>install</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.directory}/lib</outputDirectory>
                        <!-- optional -->
                        <!-- exclude copying test and provided dependencies -->
                        <includeScope>runtime</includeScope>
                        <excludeScope>provided</excludeScope>
                        <!-- optional -->
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

我更新了答案以仅复制运行时依赖项,如 here - copying the right dependencies 所述。排除测试和提供的依赖项。

事实证明,如果你想从测试和提供的范围中排除依赖关系,你需要排除提供的范围并包含运行时范围。

【讨论】:

  • 谢谢@Rafal,它对我有用。是的,优点是我们不需要任何额外的文件。
  • 链接已失效。
【解决方案2】:

你可以在一定程度上做到这一点。

首先,您将通过适当地配置maven jar plugin 创建一个executable jar。

然后您将使用 maven 程序集插件创建一个带有依赖项的 jar,不包括您的项目 jar。为此,您将创建一个描述符文件,例如 src/main/assembly/descriptor.xml,如下所示。

<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 - 一个是您的项目创建的可执行 jar,另一个是程序集插件创建的 jar-with-dependencies。

【讨论】:

    猜你喜欢
    • 2010-10-09
    • 2021-09-12
    • 2021-09-04
    相关资源
    最近更新 更多