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

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

问题描述

我想将它打包在一个可执行的jar中以便分发。我需要一个像main.jar这样的可执行文件,并且所有依赖项都在libs / *中.jar

I want to package it not in a single executable jar for distribution. I need an executable to be something like main.jar and all dependencies to be in libs/*.jar

如何在没有预先包含在其中的maven可执行jar的依赖库中?

How can I make maven executable jar without preincluded into it dependencies libraries?

我怎样才能使用Maven创建一个带有依赖关系的可执行JAR?有一条注释在2010年12月1日10:46 $ b $bAndréAronsen回答,但是那个只是没有t work(未设置sadescriptorRef失败)。

In How can I create an executable JAR with dependencies using Maven? there is a note by answered Dec 1 '10 at 10:46 André Aronsen, but that one simply doesn't work (failed s.a.descriptorRef is not set).

推荐答案

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

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.

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

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