且构网

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

Maven插件创建可执行的jar与依赖关系未打包(jar与jar)

更新时间:2022-10-20 21:41:37

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

 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>

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

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

和assembly.xml本身

 &LT;装配&GT; 
< 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部分:

 &LT;插件&GT; 
< 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>