Maven插件创建可执行的jar与依赖关系未打包(jar与jar) [英] Maven plugin to create executable jar with dependencies not unpacked (jar with jars)
问题描述
是否有任何解决方案来创建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屋!