Maven插件创建可执行的jar与依赖关系未打包(jar与jar) [英] Maven plugin to create executable jar with dependencies not unpacked (jar with jars)

查看:163
本文介绍了Maven插件创建可执行的jar与依赖关系未打包(jar与jar)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读了很多解决方案来构建具有依赖性的可执行jar(maven shade plugin,maven依赖插件,maven程序集插件)以及所有这些插件解压缩依赖jar,并在可执行jar中重新打包它们。在可执行jar中打包的依赖jar包的唯一插件是一个jar插件,但是这个插件在可执行jar中添加了其转移代码。



是否有任何解决方案来创建jar :

 ├─executable.jar
├──lib/
├──dependency1.jar
├───dependency2.jar



该解决方案可以正常工作。

解决方案

最常见的方法是使用程序集插件,这将允许您按照您需要的方式配置打包

 code><插件> 
< artifactId> maven-assembly-plugin< / artifactId>
< configuration>
< archive>
< manifest>
< mainClass> com.somewhere.Main< / mainClass>
< / manifest>
< / archive>
< descriptorRefs>
< descriptorRef> jar-with-dependencies< / descriptorRef>
< / descriptorRefs>
< / configuration>
<执行>
< execution>
< id> make-assembly< / id>
< phase> package< / phase>
< goals>
< goal> single< / goal>
< / goals>
< / execution>
< / executions>
< / plugin>

还可以为配置指定程序集描述符

 <结构> 
< appendAssemblyId> false< / appendAssemblyId>
< descriptors>
< descriptor> src / main / assembly / assembly.xml< / descriptor>
< / descriptors>
< / configuration>

和assembly.xml本身

 <装配> 
< id>程序集< / id>
< formats>
< format> jar< / format>
< / formats>
< includeBaseDirectory> false< / includeBaseDirectory>
< fileSets>
< fileSet>
< directory> $ {project.build.outputDirectory}< / directory>
< outputDirectory> /< / outputDirectory>
< / fileSet>
< / fileSets>
< / assembly>

汇编描述符还可以包含依赖项:

 <! -  lib  - > 
< dependencySets>
< dependencySet>
< outputDirectory> lib< / outputDirectory>
< / dependencySet>
< / dependencySets>

据我所知,您正在寻找最后一个。因为它只是将jar文件包含到程序集中而无需任何修改。所以最终的解决方案将如下所示:

 < assembly> 
< id>程序集< / id>
< formats>
< format> jar< / format>
< / formats>
< includeBaseDirectory> false< / includeBaseDirectory>
<! - lib - >
< dependencySets>
< dependencySet>
< outputDirectory> lib< / outputDirectory>
< / dependencySet>
< / dependencySets>
< / assembly>

和pom部分:

 <插件> 
< artifactId> maven-assembly-plugin< / artifactId>
< configuration>
< appendAssemblyId> false< / appendAssemblyId>
< descriptors>
< descriptor> src / main / assembly / assembly.xml< / descriptor>
< / descriptors>
< / configuration>
<执行>
< execution>
< id> make-assembly< / id>
< phase> package< / phase>
< goals>
< goal> single< / goal>
< / goals>
< / execution>
< / executions>
< / plugin>


I read a lot of solutions to build executable jar with dependencies (maven shade plugin, maven dependency plugin, maven assembly plugin) and all of this plugins unpack dependency jars and repack them in executable jar. The only plugin that pack dependency jars unpacked in executable jar is one jar plugin but this plugin add its runner code in executable jar.

Is there any solution to create jar like this:

├─executable.jar
├──lib/
├───dependency1.jar
├───dependency2.jar
.
.
.

and that solution to work.

解决方案

The most common way is to use assembly plugin which will allow you to configure packaging in a way you need

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
         <archive>
              <manifest>
                   <mainClass>com.somewhere.Main</mainClass>
              </manifest>
         </archive>
         <descriptorRefs>
              <descriptorRef>jar-with-dependencies</descriptorRef>
         </descriptorRefs>
    </configuration>
    <executions>
          <execution>
               <id>make-assembly</id>
               <phase>package</phase>
               <goals>
                    <goal>single</goal>
               </goals>
         </execution>
    </executions>
</plugin>

Also you can specify assembly descriptor for configuration

<configuration>
    <appendAssemblyId>false</appendAssemblyId>
    <descriptors>
      <descriptor>src/main/assembly/assembly.xml</descriptor>
    </descriptors>
</configuration>

And assembly.xml itself

<assembly>
    <id>assembly</id>
    <formats>
         <format>jar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <fileSets>
        <fileSet>
             <directory>${project.build.outputDirectory}</directory>
             <outputDirectory>/</outputDirectory>
        </fileSet>
    </fileSets>
</assembly>

Assembly descriptor can also contain dependency section:

<!-- lib -->
<dependencySets>
    <dependencySet>
        <outputDirectory>lib</outputDirectory>
    </dependencySet>
</dependencySets>

As far as I understand you're looking for the last one. As it just includes jar files into the assembly without any modifications. So the final solution will look like:

<assembly>
    <id>assembly</id>
    <formats>
         <format>jar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <!-- lib -->
    <dependencySets>
        <dependencySet>
             <outputDirectory>lib</outputDirectory>
        </dependencySet>
    </dependencySets>
</assembly>

and pom part:

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
        <appendAssemblyId>false</appendAssemblyId>
        <descriptors>
             <descriptor>src/main/assembly/assembly.xml</descriptor>
        </descriptors>
    </configuration>
    <executions>
          <execution>
               <id>make-assembly</id>
               <phase>package</phase>
               <goals>
                    <goal>single</goal>
               </goals>
         </execution>
    </executions>
</plugin>

这篇关于Maven插件创建可执行的jar与依赖关系未打包(jar与jar)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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