最近我们项目越来越多了,然后发现项目中都使用assembly插件进行打包的,于是就进行了深入的了解,本文分享该插件的配置以及微服务的统一打包方式。
plugin | function |
maven-jar-plugin | maven 默认打包插件【springboot默认使用该方式打包】,用来创建 project jar |
maven-shade-plugin | 用来打可执行包,executable(fat) jar |
maven-assembly-plugin | 支持定制化打包方式,例如 apache 项目的打包方式 |
每种打包方式都具有自己的应用场景。
1 |
|
Assembly 插件的主要作用是,允许用户将项目输出与它的依赖项、模块、站点文档、和其他文件一起组装成一个可分发的归档文件。说白了就是:结构定制化的打包。
1. 需要指定一个Assembly描述符文件。该文件指定了打包格式,包含的文件/过滤的文件等信息,可以同时指定多个描述符文件,打包成不同的格式。
2. 在Maven工程的pom.xml文件里配置maven-assembly-plugin插件,引入Assembly描述符文件。
-
<assembly >
-
-
-
<id/>
-
-
-
<formats/>
-
-
-
<includeBaseDirectory/>
-
-
-
<baseDirectory/>
-
-
-
<includeSiteDirectory/>
-
-
-
-
<containerDescriptorHandlers>
-
-
<containerDescriptorHandler>
-
-
-
<handlerName/>
-
-
-
<configuration/>
-
containerDescriptorHandler>
-
containerDescriptorHandlers>
-
-
-
<moduleSets>
-
-
<moduleSet>
-
-
-
<useAllReactorProjects/>
-
-
-
<includeSubModules/>
-
-
-
<includes/>
-
-
-
<excludes/>
-
-
-
<sources>
-
-
-
<useDefaultExcludes/>
-
-
-
<outputDirectory/>
-
-
-
<includes/>
-
-
-
<excludes/>
-
-
-
<fileMode/>
-
-
-
<directoryMode/>
-
-
-
<fileSets>
-
-
<fileSet>
-
-
-
<useDefaultExcludes/>
-
-
-
<outputDirectory/>
-
-
-
<includes/>
-
-
-
<excludes/>
-
-
-
<fileMode/>
-
-
-
<directoryMode/>
-
-
-
<directory/>
-
-
-
<lineEnding/>
-
-
-
<filtered/>
-
fileSet>
-
fileSets>
-
-
-
<includeModuleDirectory/>
-
-
-
<excludeSubModuleDirectories/>
-
-
-
<outputDirectoryMapping/>
-
sources>
-
-
-
<binaries>
-
-
-
<outputDirectory/>
-
-
-
<includes/>
-
-
-
<excludes/>
-
-
-
<fileMode/>
-
-
-
<directoryMode/>
-
-
-
<attachmentClassifier/>
-
-
-
<includeDependencies/>
-
-
<dependencySets>
-
-
<dependencySet>
-
-
-
<outputDirectory/>
-
-
-
<includes/>
-
-
-
<excludes/>
-
-
-
<fileMode/>
-
-
-
<directoryMode/>
-
-
-
<useStrictFiltering/>
-
-
-
<outputFileNameMapping/>
-
-
-
<unpack/>
-
-
<unpackOptions>
-
-
-
<includes/>
-
-
-
<excludes/>
-
-
-
<filtered/>
-
-
-
<lineEnding/>
-
-
-
<useDefaultExcludes/>
-
-
-
<encoding/>
-
unpackOptions>
-
-
-
<scope/>
-
-
-
<useProjectArtifact/>
-
-
-
<useProjectAttachments/>
-
-
-
<useTransitiveDependencies/>
-
-
-
<useTransitiveFiltering/>
-
dependencySet>
-
dependencySets>
-
-
-
<unpack/>
-
-
<unpackOptions>
-
-
-
<includes/>
-
-
-
<excludes/>
-
-
-
<filtered/>
-
-
-
<lineEnding/>
-
-
-
<useDefaultExcludes/>
-
-
-
<encoding/>
-
unpackOptions>
-
-
-
<outputFileNameMapping/>
-
binaries>
-
moduleSet>
-
moduleSets>
-
-
-
<fileSets>
-
-
<fileSet>
-
-
-
<useDefaultExcludes/>
-
-
-
<outputDirectory/>
-
-
-
<includes/>
-
-
-
<excludes/>
-
-
-
<fileMode/>
-
-
-
<directoryMode/>
-
-
-
<directory/>
-
-
-
<lineEnding/>
-
-
-
<filtered/>
-
fileSet>
-
fileSets>
-
-
-
<files>
-
-
<file>
-
-
-
<source/>
-
-
-
<outputDirectory/>
-
-
-
<destName/>
-
-
-
<fileMode/>
-
-
-
<lineEnding/>
-
-
-
<filtered/>
-
file>
-
files>
-
-
<dependencySets>
-
-
<dependencySet>
-
-
-
<outputDirectory/>
-
-
-
<includes/>
-
-
-
<excludes/>
-
-
-
<fileMode/>
-
-
-
<directoryMode/>
-
-
-
<useStrictFiltering/>
-
-
-
<outputFileNameMapping/>
-
-
-
<unpack/>
-
-
<unpackOptions>
-
-
-
<includes/>
-
-
-
<excludes/>
-
-
-
<filtered/>
-
-
-
<lineEnding/>
-
-
-
<useDefaultExcludes/>
-
-
-
<encoding/>
-
unpackOptions>
-
-
-
<scope/>
-
-
-
<useProjectArtifact/>
-
-
-
<useProjectAttachments/>
-
-
-
<useTransitiveDependencies/>
-
-
-
<useTransitiveFiltering/>
-
dependencySet>
-
dependencySets>
-
-
-
<repositories>
-
<repository>
-
-
-
<outputDirectory/>
-
-
-
<includes/>
-
-
-
<excludes/>
-
-
-
<fileMode/>
-
-
-
<directoryMode/>
-
-
-
<includeMetadata/>
-
-
-
<groupVersionAlignments>
-
<groupVersionAlignment>
-
-
-
<id/>
-
-
-
<version/>
-
-
-
<excludes/>
-
groupVersionAlignment>
-
groupVersionAlignments>
-
-
-
<scope/>
-
repository>
-
repositories>
-
-
-
<componentDescriptors/>
-
assembly>
指定打的包是否包含打包层目录,比如finalName是terminal-dispatch,当值为true,所有文件被放在包内的terminal-dispatch目录下,否则直接放在包的根目录下。
如下图所示:
实际使用:
assembly.xml
-
<assembly
>
-
<id
>assembly
<
/id
>
-
<formats
>
-
<!--zip
/tar
/tar.gz或tgz
/tar.bz
2或tbz
2
/tar.snappy
/tar.xz或txz
/jar
/dir
/war--
>
-
<
format
>tar.gz
<
/
format
>
-
<
/formats
>
-
<includeBaseDirectory
>
true
<
/includeBaseDirectory
>
-
-
<fileSets
>
-
<fileSet
>
-
<lineEnding
>unix
<
/lineEnding
>
-
<directory
>${project.basedir}
/src
/main
/resources
<
/directory
>
-
<outputDirectory
>.
/config
<
/outputDirectory
>
-
<includes
>
-
<include
>application
*.properties
<
/include
>
-
<include
>log
*.xml
<
/include
>
-
<
/includes
>
-
<
/fileSet
>
-
<fileSet
>
-
<lineEnding
>unix
<
/lineEnding
>
-
<directory
>${project.basedir}
/bin
<
/directory
>
-
<outputDirectory
>.
/bin
<
/outputDirectory
>
-
<
/fileSet
>
-
<!-- 把项目自己编译出来的jar文件,打包进zip文件的根目录 --
>
-
<fileSet
>
-
<directory
>${project.build.directory}
<
/directory
>
-
<outputDirectory
>.
/lib
<
/outputDirectory
>
-
<includes
>
-
<include
>
*.jar
<
/include
>
-
<
/includes
>
-
<
/fileSet
>
-
<
/fileSets
>
-
-
<
/assembly
>
pom.xml中引入assembly.xml所在的位置。
-
<?xml version
=
"1.0" encoding
=
"UTF-8"?
>
-
<project xmlns
=
"http://maven.apache.org/POM/4.0.0" xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance"
-
xsi:schemaLocation
=
"http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"
>
-
<dependencies
>
-
<dependencie
>
-
...
-
<
/dependencie
>
-
<
/dependencies
>
-
<build
>
-
<plugins
>
-
<plugin
>
-
...
-
<
/plugin
>
-
-
<!-- 这个插件是关键 --
>
-
<plugin
>
-
<groupId
>org.apache.maven.plugins
<
/groupId
>
-
<artifactId
>maven-assembly-plugin
<
/artifactId
>
-
<
configuration
>
-
<!-- 这个是assembly 所在位置;${basedir}是指项目的的根路径 --
>
-
<descriptors
>
-
descriptor
>${basedir}
/src
/main
/assembly
/assembly.xml
<
/descriptor
>
-
<
/descriptors
>
-
<!--打包解压后的目录名;${project.artifactId}是指:项目的artifactId--
>
-
<finalName
>${project.artifactId}
<
/finalName
>
-
<!-- 打包压缩包位置--
>
-
<outputDirectory
>${project.build.directory}
/
release
<
/outputDirectory
>
-
<!-- 打包编码 --
>
-
<encoding
>UTF-
8
<
/encoding
>
-
<
/
configuration
>
-
<executions
>
-
<execution
>
<!-- 配置执行器 --
>
-
<id
>make-assembly
<
/id
>
-
<phase
>package
<
/phase
>
<!-- 绑定到package生命周期阶段上 --
>
-
<goals
>
-
<goal
>single
<
/goal
>
<!-- 只运行一次 --
>
-
<
/goals
>
-
<
/execution
>
-
<
/executions
>
-
<
/plugin
>
-
<
/plugins
>
-
<
/build
>
-
<
/project
>
打包命令:
mvn clean package -Dmaven.test.skip=true
或者
mvn -s /src/main/assembly/assembly.xml package assembly:assembly
其中,生成的lib文件夹下放该项目的所有依赖以及该服务jar包;应用相关的配置文件放在config目录下;bin文件夹下我们一般放start.sh和stop.sh两个脚本文件用来开启和关闭该服务。
随着微服务的越来越火,我们将服务进行细粒度拆分后,也需要很好的把服务的打包以及部署方式进行统一,这样我们就可以走自动化发布工具来进行统一服务的管理和部署,使得越来越方便。