maven打包插件--maven-assembly-plugin

0、背景

  最近我们项目越来越多了,然后发现项目中都使用assembly插件进行打包的,于是就进行了深入的了解,本文分享该插件的配置以及微服务的统一打包方式。

一、介绍

maven提供的打包插件有如下三种:
plugin function
maven-jar-plugin maven 默认打包插件【springboot默认使用该方式打包】,用来创建 project jar
maven-shade-plugin 用来打可执行包,executable(fat) jar
 maven-assembly-plugin  支持定制化打包方式,例如 apache 项目的打包方式

每种打包方式都具有自己的应用场景。

二、Assembly 是什么意思?

maven打包插件--maven-assembly-plugin_第1张图片

三、maven-assembly-plugin是什么?

1

它是maven中针对打包任务而提供的标准插件。

四、maven-assembly-plugin插件的作用?

Assembly 插件的主要作用是,允许用户将项目输出与它的依赖项、模块、站点文档、和其他文件一起组装成一个可分发的归档文件。说白了就是:结构定制化的打包。

五、maven-assembly-plugin插件在maven项目中如何使用(即使用步骤)?

  1.  需要指定一个Assembly描述符文件。该文件指定了打包格式,包含的文件/过滤的文件等信息,可以同时指定多个描述符文件,打包成不同的格式。

  2.  在Maven工程的pom.xml文件里配置maven-assembly-plugin插件,引入Assembly描述符文件。

六、maven项目中Assembly描述符文件详解


   
     
     
     
     
  1. <assembly >
  2. <id/>
  3. <formats/>
  4. <includeBaseDirectory/>
  5. <baseDirectory/>
  6. <includeSiteDirectory/>
  7. <containerDescriptorHandlers>
  8. <containerDescriptorHandler>
  9. <handlerName/>
  10. <configuration/>
  11. containerDescriptorHandler>
  12. containerDescriptorHandlers>
  13. <moduleSets>
  14. <moduleSet>
  15. <useAllReactorProjects/>
  16. <includeSubModules/>
  17. <includes/>
  18. <excludes/>
  19. <sources>
  20. <useDefaultExcludes/>
  21. <outputDirectory/>
  22. <includes/>
  23. <excludes/>
  24. <fileMode/>
  25. <directoryMode/>
  26. <fileSets>
  27. <fileSet>
  28. <useDefaultExcludes/>
  29. <outputDirectory/>
  30. <includes/>
  31. <excludes/>
  32. <fileMode/>
  33. <directoryMode/>
  34. <directory/>
  35. <lineEnding/>
  36. <filtered/>
  37. fileSet>
  38. fileSets>
  39. <includeModuleDirectory/>
  40. <excludeSubModuleDirectories/>
  41. <outputDirectoryMapping/>
  42. sources>
  43. <binaries>
  44. <outputDirectory/>
  45. <includes/>
  46. <excludes/>
  47. <fileMode/>
  48. <directoryMode/>
  49. <attachmentClassifier/>
  50. <includeDependencies/>
  51. <dependencySets>
  52. <dependencySet>
  53. <outputDirectory/>
  54. <includes/>
  55. <excludes/>
  56. <fileMode/>
  57. <directoryMode/>
  58. <useStrictFiltering/>
  59. <outputFileNameMapping/>
  60. <unpack/>
  61. <unpackOptions>
  62. <includes/>
  63. <excludes/>
  64. <filtered/>
  65. <lineEnding/>
  66. <useDefaultExcludes/>
  67. <encoding/>
  68. unpackOptions>
  69. <scope/>
  70. <useProjectArtifact/>
  71. <useProjectAttachments/>
  72. <useTransitiveDependencies/>
  73. <useTransitiveFiltering/>
  74. dependencySet>
  75. dependencySets>
  76. <unpack/>
  77. <unpackOptions>
  78. <includes/>
  79. <excludes/>
  80. <filtered/>
  81. <lineEnding/>
  82. <useDefaultExcludes/>
  83. <encoding/>
  84. unpackOptions>
  85. <outputFileNameMapping/>
  86. binaries>
  87. moduleSet>
  88. moduleSets>
  89. <fileSets>
  90. <fileSet>
  91. <useDefaultExcludes/>
  92. <outputDirectory/>
  93. <includes/>
  94. <excludes/>
  95. <fileMode/>
  96. <directoryMode/>
  97. <directory/>
  98. <lineEnding/>
  99. <filtered/>
  100. fileSet>
  101. fileSets>
  102. <files>
  103. <file>
  104. <source/>
  105. <outputDirectory/>
  106. <destName/>
  107. <fileMode/>
  108. <lineEnding/>
  109. <filtered/>
  110. file>
  111. files>
  112. <dependencySets>
  113. <dependencySet>
  114. <outputDirectory/>
  115. <includes/>
  116. <excludes/>
  117. <fileMode/>
  118. <directoryMode/>
  119. <useStrictFiltering/>
  120. <outputFileNameMapping/>
  121. <unpack/>
  122. <unpackOptions>
  123. <includes/>
  124. <excludes/>
  125. <filtered/>
  126. <lineEnding/>
  127. <useDefaultExcludes/>
  128. <encoding/>
  129. unpackOptions>
  130. <scope/>
  131. <useProjectArtifact/>
  132. <useProjectAttachments/>
  133. <useTransitiveDependencies/>
  134. <useTransitiveFiltering/>
  135. dependencySet>
  136. dependencySets>
  137. <repositories>
  138. <repository>
  139. <outputDirectory/>
  140. <includes/>
  141. <excludes/>
  142. <fileMode/>
  143. <directoryMode/>
  144. <includeMetadata/>
  145. <groupVersionAlignments>
  146. <groupVersionAlignment>
  147. <id/>
  148. <version/>
  149. <excludes/>
  150. groupVersionAlignment>
  151. groupVersionAlignments>
  152. <scope/>
  153. repository>
  154. repositories>
  155. <componentDescriptors/>
  156. assembly>

true标签作用?

     指定打的包是否包含打包层目录,比如finalName是terminal-dispatch,当值为true,所有文件被放在包内的terminal-dispatch目录下,否则直接放在包的根目录下。

  如下图所示:

maven打包插件--maven-assembly-plugin_第2张图片

七、实践出真知

实际使用:

assembly.xml


   
     
     
     
     
  1. <assembly >
  2. <id >assembly < /id >
  3. <formats >
  4. <!--zip /tar /tar.gz或tgz /tar.bz 2或tbz 2 /tar.snappy /tar.xz或txz /jar /dir /war-- >
  5. < format >tar.gz < / format >
  6. < /formats >
  7. <includeBaseDirectory > true < /includeBaseDirectory >
  8. <fileSets >
  9. <fileSet >
  10. <lineEnding >unix < /lineEnding >
  11. <directory >${project.basedir} /src /main /resources < /directory >
  12. <outputDirectory >. /config < /outputDirectory >
  13. <includes >
  14. <include >application *.properties < /include >
  15. <include >log *.xml < /include >
  16. < /includes >
  17. < /fileSet >
  18. <fileSet >
  19. <lineEnding >unix < /lineEnding >
  20. <directory >${project.basedir} /bin < /directory >
  21. <outputDirectory >. /bin < /outputDirectory >
  22. < /fileSet >
  23. <!-- 把项目自己编译出来的jar文件,打包进zip文件的根目录 -- >
  24. <fileSet >
  25. <directory >${project.build.directory} < /directory >
  26. <outputDirectory >. /lib < /outputDirectory >
  27. <includes >
  28. <include > *.jar < /include >
  29. < /includes >
  30. < /fileSet >
  31. < /fileSets >
  32. < /assembly >

 pom.xml中引入assembly.xml所在的位置。


   
     
     
     
     
  1. <?xml version = "1.0" encoding = "UTF-8"? >
  2. <project xmlns = "http://maven.apache.org/POM/4.0.0" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" >
  4. <dependencies >
  5. <dependencie >
  6. ...
  7. < /dependencie >
  8. < /dependencies >
  9. <build >
  10. <plugins >
  11. <plugin >
  12. ...
  13. < /plugin >
  14. <!-- 这个插件是关键 -- >
  15. <plugin >
  16. <groupId >org.apache.maven.plugins < /groupId >
  17. <artifactId >maven-assembly-plugin < /artifactId >
  18. < configuration >
  19. <!-- 这个是assembly 所在位置;${basedir}是指项目的的根路径 -- >
  20. <descriptors >
  21. descriptor >${basedir} /src /main /assembly /assembly.xml < /descriptor >
  22. < /descriptors >
  23. <!--打包解压后的目录名;${project.artifactId}是指:项目的artifactId-- >
  24. <finalName >${project.artifactId} < /finalName >
  25. <!-- 打包压缩包位置-- >
  26. <outputDirectory >${project.build.directory} / release < /outputDirectory >
  27. <!-- 打包编码 -- >
  28. <encoding >UTF- 8 < /encoding >
  29. < / configuration >
  30. <executions >
  31. <execution > <!-- 配置执行器 -- >
  32. <id >make-assembly < /id >
  33. <phase >package < /phase > <!-- 绑定到package生命周期阶段上 -- >
  34. <goals >
  35. <goal >single < /goal > <!-- 只运行一次 -- >
  36. < /goals >
  37. < /execution >
  38. < /executions >
  39. < /plugin >
  40. < /plugins >
  41. < /build >
  42. < /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两个脚本文件用来开启和关闭该服务。

maven打包插件--maven-assembly-plugin_第3张图片

maven打包插件--maven-assembly-plugin_第4张图片

maven打包插件--maven-assembly-plugin_第5张图片

maven打包插件--maven-assembly-plugin_第6张图片

八、总结归纳【这么多项目为什么要使用assembly插件来打包】

  随着微服务的越来越火,我们将服务进行细粒度拆分后,也需要很好的把服务的打包以及部署方式进行统一,这样我们就可以走自动化发布工具来进行统一服务的管理和部署,使得越来越方便。

你可能感兴趣的:(Java,java,spring,boot,开发语言)