• Maven assembly多模块多环境(dev|test|prod)定制化打包SpringBoot项目详解


    目录

    单模块场景

    项目全景图 

    pom.xml

     resources配置 

    maven-jar-plugin配置

    maven-assembly-plugin配置

    profiles配置

    assembly.xml

    打包

    多模块场景 


    声明:实验环境idea2022+SpringBoot 2.1.3。

    单模块场景

            这种方式适用于SpringBoot项目中仅有一个模块的场景。

    需求:

    1、启停可执行jar包的shell脚本单独打包到某个目录下,例如bin目录;

    2、项目resources下的配置从jar包分离出来,单独打包到某个目录下,例如config目录;

    3、项目的所有依赖jar包单独打到某个目录下,例如lib目录。

    打包完成后,可直接执行bin目录下的启停shell脚本运行程序。

    项目全景图 

             项目整体结构如图:

    pom.xml

            build结构如图:

    1. <build>
    2. <plugins>
    3. <plugin>
    4. <groupId>org.apache.maven.pluginsgroupId>
    5. <artifactId>maven-jar-pluginartifactId>
    6. <configuration>
    7. <archive>
    8. <manifest>
    9. <mainClass>马赛克DevopsBrainYmlAutomationApplication
    10. mainClass>
    11. <addClasspath>trueaddClasspath>
    12. <classpathPrefix>libclasspathPrefix>
    13. manifest>
    14. archive>
    15. <excludes>
    16. <exclude>*.*exclude>
    17. excludes>
    18. configuration>
    19. plugin>
    20. <plugin>
    21. <groupId>org.apache.maven.pluginsgroupId>
    22. <artifactId>maven-assembly-pluginartifactId>
    23. <executions>
    24. <execution>
    25. <phase>packagephase>
    26. <goals>
    27. <goal>singlegoal>
    28. goals>
    29. execution>
    30. executions>
    31. <configuration>
    32. <descriptors>
    33. <descriptor>assembly/assembly.xmldescriptor>
    34. descriptors>
    35. <outputDirectory>targetoutputDirectory>
    36. configuration>
    37. plugin>
    38. plugins>
    39. build>
    40. <profiles>
    41. <profile>
    42. <id>devid>
    43. <properties>
    44. <package.environment>devpackage.environment>
    45. properties>
    46. <activation>
    47. <activeByDefault>trueactiveByDefault>
    48. activation>
    49. profile>
    50. <profile>
    51. <id>testid>
    52. <properties>
    53. <package.environment>testpackage.environment>
    54. properties>
    55. profile>
    56. <profile>
    57. <id>prodid>
    58. <properties>
    59. <package.environment>prodpackage.environment>
    60. properties>
    61. profile>
    62. profiles>

    maven-jar-plugin配置

    1. <plugin>
    2. <groupId>org.apache.maven.pluginsgroupId>
    3. <artifactId>maven-jar-pluginartifactId>
    4. <configuration>
    5. <archive>
    6. <manifest>
    7. <mainClass>马斯克拉ainYmlAutomationApplication
    8. mainClass>
    9. <addClasspath>trueaddClasspath>
    10. <classpathPrefix>libclasspathPrefix>
    11. manifest>
    12. archive>
    13. configuration>
    14. plugin>

    它配置了jar包启动的主类(程序入口),以及外部依赖的classpath目录。

    maven-assembly-plugin配置

    1. <plugin>
    2. <groupId>org.apache.maven.pluginsgroupId>
    3. <artifactId>maven-assembly-pluginartifactId>
    4. <executions>
    5. <execution>
    6. <phase>packagephase>
    7. <goals>
    8. <goal>singlegoal>
    9. goals>
    10. execution>
    11. executions>
    12. <configuration>
    13. <descriptors>
    14. <descriptor>assembly/assembly.xmldescriptor>
    15. descriptors>
    16. <outputDirectory>targetoutputDirectory>
    17. configuration>
    18. plugin>

    它主要指定assembly.xml文件完成定制化配置,包括shell脚本、配置文件等。

    profiles配置

            指定打包环境,比如开发环境、测试环境、演示环境和生产环境。配置好后,idea maven打包中会出现配置的环境,打包的时候,选择一个环境即可。如图:

    assembly.xml

    1. <assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
    2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
    4. <id>${package.environment}id>
    5. <formats>
    6. <format>dirformat>
    7. <format>zipformat>
    8. formats>
    9. <includeBaseDirectory>falseincludeBaseDirectory>
    10. <dependencySets>
    11. <dependencySet>
    12. <outputDirectory>/liboutputDirectory>
    13. <excludes>
    14. <exclude>${project.groupId}:${project.artifactId}exclude>
    15. excludes>
    16. dependencySet>
    17. <dependencySet>
    18. <outputDirectory>/outputDirectory>
    19. <includes>
    20. <include>${project.groupId}:${project.artifactId}include>
    21. includes>
    22. dependencySet>
    23. dependencySets>
    24. <fileSets>
    25. <fileSet>
    26. <directory>src/main/resourcesdirectory>
    27. <filtered>truefiltered>
    28. <includes>
    29. <include>application.ymlinclude>
    30. <include>application-${package.environment}.ymlinclude>
    31. includes>
    32. <outputDirectory>/configoutputDirectory>
    33. fileSet>
    34. <fileSet>
    35. <directory>bin/${package.environment}directory>
    36. <filtered>truefiltered>
    37. <includes>
    38. <include>*.*include>
    39. includes>
    40. <outputDirectory>/binoutputDirectory>
    41. <fileMode>0755fileMode>
    42. fileSet>
    43. fileSets>
    44. assembly>

    (1)id:

    assembly的唯一性标识,它会追加到打包后的目录或者zip压缩文件尾部,例如:

     (2)format:

    打包后的格式,dir表示打包后是一个目录,zip表示打包后是一个zip压缩文件。如图:

    includeBaseDirectory:

    是否包含基目录,如果配置为true,会在最后打包的目录下生成一个基目录,如图: 

    dependencySet

    jar依赖配置。assembly.xml中配置了两个dependencySet,第一个dependencySet将所有依赖打包到lib下,除了本项目的jar包。第二个denpendencySet是将项目jar包打包到根目录/下,这里根目录是相对于打包后的目录而言的,如图:

     fileSets:

    文件复制打包,例如shell脚本文件,配置文件等。directory指定输入目录(需要复制的文件所在目录),outputDirectory指定输出目录(复制后的文件所在目录)。如图:

    打包

            配置完成后,现在我们开始打包看下效果。首先选择环境,例如选择开发环境(dev),如图:

     打包结果:

    多模块场景 

    需求:

    除了单模块场景的需求外,新增一个多模块的需求:

    (1)将每个模块项目自身的jar包打成一个jar包。例如项目结构如图:

     api模块依赖其他子模块(bean、dao、service),当然外部依赖还是打包到lib目录下。

    这里需要在api的pom.xml中配置上面单模块的相关配置,还需要新增一个plugin配置:

    1. <plugin>
    2. <groupId>org.springframework.bootgroupId>
    3. <artifactId>spring-boot-maven-pluginartifactId>
    4. <version>2.1.3.RELEASEversion>
    5. <configuration>
    6. <mainClass>马赛克.DevopsBrainManagerApplicationmainClass>
    7. <includes>
    8. <include>
    9. <groupId>马赛克groupId>
    10. <artifactId>马赛克-serviceartifactId>
    11. include>
    12. <include>
    13. <groupId>马赛克groupId>
    14. <artifactId>马赛克-daoartifactId>
    15. include>
    16. <include>
    17. <groupId>马赛克groupId>
    18. <artifactId>马赛克-beanartifactId>
    19. include>
    20. includes>
    21. configuration>
    22. <executions>
    23. <execution>
    24. <goals>
    25. <goal>repackagegoal>
    26. goals>
    27. execution>
    28. executions>
    29. plugin>

    其中includes部分就是将bean、dao、service模块项目自身的jar包打到api模块项目jar中。打包后的效果如图:

    下面给出pom.xml和assembly.xml整体配置:

    assembly.xml

    1. <assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
    2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
    4. <id>${package.environment}id>
    5. <formats>
    6. <format>dirformat>
    7. <format>zipformat>
    8. formats>
    9. <includeBaseDirectory>falseincludeBaseDirectory>
    10. <dependencySets>
    11. <dependencySet>
    12. <outputDirectory>/liboutputDirectory>
    13. <excludes>
    14. <exclude>${project.groupId}:*exclude>
    15. excludes>
    16. dependencySet>
    17. <dependencySet>
    18. <outputDirectory>/outputDirectory>
    19. <includes>
    20. <include>${project.groupId}:${project.artifactId}include>
    21. includes>
    22. dependencySet>
    23. dependencySets>
    24. <fileSets>
    25. <fileSet>
    26. <directory>src/main/resourcesdirectory>
    27. <filtered>truefiltered>
    28. <includes>
    29. <include>application.ymlinclude>
    30. <include>logback-spring.xmlinclude>
    31. <include>whiteList.propertiesinclude>
    32. <include>application-${package.environment}.ymlinclude>
    33. includes>
    34. <outputDirectory>/configoutputDirectory>
    35. fileSet>
    36. <fileSet>
    37. <directory>bin/${package.environment}directory>
    38. <filtered>truefiltered>
    39. <includes>
    40. <include>*.*include>
    41. includes>
    42. <outputDirectory>/binoutputDirectory>
    43. <fileMode>0755fileMode>
    44. fileSet>
    45. fileSets>
    46. assembly>

    pom.xml

    1. <build>
    2. <plugins>
    3. <plugin>
    4. <groupId>org.apache.maven.pluginsgroupId>
    5. <artifactId>maven-jar-pluginartifactId>
    6. <configuration>
    7. <archive>
    8. <manifest>
    9. <addClasspath>trueaddClasspath>
    10. <classpathPrefix>libclasspathPrefix>
    11. manifest>
    12. archive>
    13. <excludes>
    14. <exclude>*.*exclude>
    15. excludes>
    16. configuration>
    17. plugin>
    18. <plugin>
    19. <groupId>org.springframework.bootgroupId>
    20. <artifactId>spring-boot-maven-pluginartifactId>
    21. <version>2.1.3.RELEASEversion>
    22. <configuration>
    23. <mainClass>马赛克nManagerApplicationmainClass>
    24. <includes>
    25. <include>
    26. <groupId>马赛克groupId>
    27. <artifactId>马赛克-serviceartifactId>
    28. include>
    29. <include>
    30. <groupId>马赛克groupId>
    31. <artifactId>马赛克-daoartifactId>
    32. include>
    33. <include>
    34. <groupId>马赛克groupId>
    35. <artifactId>马赛克-beanartifactId>
    36. include>
    37. includes>
    38. configuration>
    39. <executions>
    40. <execution>
    41. <goals>
    42. <goal>repackagegoal>
    43. goals>
    44. execution>
    45. executions>
    46. plugin>
    47. <plugin>
    48. <groupId>org.apache.maven.pluginsgroupId>
    49. <artifactId>maven-assembly-pluginartifactId>
    50. <executions>
    51. <execution>
    52. <id>make-assemblyid>
    53. <phase>packagephase>
    54. <goals>
    55. <goal>singlegoal>
    56. goals>
    57. execution>
    58. executions>
    59. <configuration>
    60. <descriptors>
    61. <descriptor>assembly/assembly.xmldescriptor>
    62. descriptors>
    63. <outputDirectory>targetoutputDirectory>
    64. configuration>
    65. plugin>
    66. plugins>
    67. build>
    68. <profiles>
    69. <profile>
    70. <id>devid>
    71. <properties>
    72. <package.environment>devpackage.environment>
    73. properties>
    74. <activation>
    75. <activeByDefault>trueactiveByDefault>
    76. activation>
    77. profile>
    78. <profile>
    79. <id>testid>
    80. <properties>
    81. <package.environment>testpackage.environment>
    82. properties>
    83. profile>
    84. <profile>
    85. <id>demoid>
    86. <properties>
    87. <package.environment>demopackage.environment>
    88. properties>
    89. profile>
    90. <profile>
    91. <id>prodid>
    92. <properties>
    93. <package.environment>prodpackage.environment>
    94. properties>
    95. profile>
    96. profiles>

  • 相关阅读:
    Java面试大全-基础(持续更新中)
    keil5MDK 及cubeMX安装教程
    Java进阶(再论线程)——线程的4种创建方式 & 线程的生命周期 & 线程的3大特性 & 集合中的线程安全问题
    框架分析(11)-测试框架
    [第十三篇]——Docker Compose
    java计算机毕业设计基于ssm的品牌首饰售卖平台
    物联网中的常见传感器
    什么是 RENGA NFT?
    JAVA学习-----TreeMap
    保姆级思路教程 使用selenium + 简易爬虫 爬取课程网站未完成作业的清单
  • 原文地址:https://blog.csdn.net/L_15156024189/article/details/126192991