使用maven-assembly-plugin打包多模块项目
概述
maven-assembly-plugin 是目前maven项目中最常用的打包工具,它便利、配置简单,因此可以满足我们大部分的需求。
实际开发过程中大部分Maven项目都是多模块的,因为工作需要,对其进行了研究与尝试,目前可以将多模块按照自己需求打包到一起。
1. 需求
项目本身代码非常复杂,最深可以到三层模块,即GrandFather -> Parent -> child,要求打包后的结构如下:目录的含义不再追溯,下面直接将打包方法。
2. 打包流程
2.1 新建打包模块
Maven多模块打包在一起时,需要新建一个专门用于打包的模块,该模块不需要有任何Java代码,只需要配置一些最终形成的环境即可。但是该模块有一个非常重要的要求: 该模块必须是整个项目最后一个进行打包的。
可以通过配置pom.xml中加载模块的顺序来控制,最后配置进入的即最后进行打包的:
本人项目比较复杂,最终配置的打包是在tools子模块下的tools-package模块中进行配置。
2.2 配置打包模块
根据最终生成的路径需求,将所有的相关路径及一些命令、配置文件等按照要求在打包模块中实现,放在resources目录即可。
注:该项目中不需要任何Java代码
2.3 配置打包模块的pom.xml
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4.1</version>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<finalName>xxxxx</finalName>
<descriptors>
<descriptor>src/main/resources/assemble/assemble.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
2.4 配置其他模块打包方式
因为项目中使用的SpringBoot,SpringBoot也提供了一种打包方式:spring-boot-maven-plugin,这种方式打包时会将该项目中所有依赖的包(包括静态文件)统一打包至一个jar中,这个不是我们项目的需求,我们的需求是将所有的jar包(包括第三方及当前项目的jar包)都放在lib目录,且是单独存在。
因此需要将其他模块的打包配置修改,修改分为两步:
- 1)不再使用spring-boot-maven-plugin进行打包,即将其从pom.xml中删除;
- 2)在pom.xml文件中使用maven-jar-plugin进行打包,该打包如何配置请参考文章(待定)。
2.5 配置assemble.xml文件
assemble.xml文件中描述了具体如何打包,该打包过程和实际需求关系密切
<?xml version='1.0' encoding='UTF-8'?>
<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>${project.version}</id>
<formats>
<format>tar.gz</format>
</formats>
<includeBaseDirectory>true</includeBaseDirectory>
<moduleSets>
<moduleSet>
<useAllReactorProjects>true</useAllReactorProjects>
<includes>
<include>xxx.xx.x:base1</include>
<include>xxx.xx.x:base2</include>
<include>xxx.xx.x:base3</include>
<include>xxx.xx.x:base4</include>
</includes>
<binaries>
<outputDirectory>lib</outputDirectory>
<unpack>false</unpack>
</binaries>
</moduleSet>
</moduleSets>
<fileSets>
<fileSet>
<directory>src/main/resources/bin</directory>
<outputDirectory>bin</outputDirectory>
</fileSet>
<fileSet>
<directory>src/main/resources/conf</directory>
<outputDirectory>conf</outputDirectory>
</fileSet>
<fileSet>
<directory>src/main/resources/docs</directory>
<outputDirectory>docs</outputDirectory>
</fileSet><fileSet>
<directory>src/main/resources/keys</directory>
<outputDirectory>keys</outputDirectory>
</fileSet>
<fileSet>
<includes>
<include>README.md</include>
</includes>
<outputDirectory></outputDirectory>
</fileSet>
</fileSets>
<dependencySets>
<dependencySet>
<unpack>false</unpack>
<useProjectArtifact>true</useProjectArtifact>
<outputDirectory>lib</outputDirectory>
<scope>provided</scope>
</dependencySet>
<dependencySet>
<unpack>false</unpack>
<useProjectArtifact>true</useProjectArtifact>
<outputDirectory>lib</outputDirectory>
<scope>system</scope>
</dependencySet>
<dependencySet>
<unpack>false</unpack>
<useProjectArtifact>true</useProjectArtifact>
<outputDirectory>lib</outputDirectory>
<scope>runtime</scope>
</dependencySet>
</dependencySets>
</assembly>
具体的配置参数可以查看 https://maven.apache.org/plugins/maven-assembly-plugin/assembly.html
本人在实验过程中重点是处理了dependencySet这个配置:
<dependencySets>
<dependencySet>
<unpack>false</unpack>
<useProjectArtifact>true</useProjectArtifact>
<outputDirectory>lib</outputDirectory>
<scope>provided</scope>
</dependencySet>
<dependencySet>
<unpack>false</unpack>
<useProjectArtifact>true</useProjectArtifact>
<outputDirectory>lib</outputDirectory>
<scope>system</scope>
</dependencySet>
<dependencySet>
<unpack>false</unpack>
<useProjectArtifact>true</useProjectArtifact>
<outputDirectory>lib</outputDirectory>
<scope>runtime</scope>
</dependencySet>
</dependencySets>
请配置上上述三个部分,要不然总是会缺少一些依赖包打不进来,Mark一下,很重要!