• Springboot、maven 打包瘦身,去除依赖的jar【springboot外置jar、配置文件】


    背景

    分布式项目、微服务项目一般都会引用许多公共依赖,每次maven打出来的jar包上百M,不利于运维工作、可以在打包时隔离项目本身的jar和依赖的公用jar包,这样项目本身包可以做到很精简。

    本文主要把项目依赖的包和项目的配置文件在打包时导出到本身springboot 的jar包 外。

    本文项目中导入依赖用到了两种方式,一是直接通过maven库,二是通过导入本地第三方jar

    配置

    全部配置

    我在系统根目录下创建了一个boot的文件夹,mvn打包之后会在boot下创建名称为lib、boot-demo的两个文件夹,lib用来存放所有依赖的jar,boot-demo用来存放springboot的jar和application.properties配置文件 

    用到了

    maven-compiler-plugin:负责编译源码

    spring-boot-maven-plugin :负责编译springbootjar

    maven-jar-plugin:负责把项目依赖的jar写到MANIFEST.MF文件

    maven-dependency-plugin:负责导出项目依赖的jar

    maven-resources-plugin:负责导出项目配置文件

    1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    3. <modelVersion>4.0.0modelVersion>
    4. <groupId>com.luckgroupId>
    5. <artifactId>boot-demoartifactId>
    6. <version>1.0-SNAPSHOTversion>
    7. <name>boot-demoname>
    8. <properties>
    9. <global-jar-output>/bootglobal-jar-output>
    10. <system-jar-classpath>. ../lib/mytest-1.0.jarsystem-jar-classpath>
    11. <java.home>${env.JAVA_HOME}java.home>
    12. <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    13. <java.version>1.8java.version>
    14. properties>
    15. <dependencyManagement>
    16. <dependencies>
    17. <dependency>
    18. <groupId>org.springframework.bootgroupId>
    19. <artifactId>spring-boot-dependenciesartifactId>
    20. <version>2.3.0.RELEASEversion>
    21. <type>pomtype>
    22. <scope>importscope>
    23. dependency>
    24. dependencies>
    25. dependencyManagement>
    26. <dependencies>
    27. <dependency>
    28. <groupId>org.springframework.bootgroupId>
    29. <artifactId>spring-boot-starter-webartifactId>
    30. dependency>
    31. <dependency>
    32. <groupId>com.luckgroupId>
    33. <artifactId>mytestartifactId>
    34. <version>1.0version>
    35. <scope>systemscope>
    36. <systemPath>${basedir}/lib/otherjar.jarsystemPath>
    37. dependency>
    38. dependencies>
    39. <build>
    40. <resources>
    41. <resource>
    42. <directory>src/main/javadirectory>
    43. <includes>
    44. <include>**/*.xmlinclude>
    45. includes>
    46. resource>
    47. <resource>
    48. <directory>src/main/resourcesdirectory>
    49. <includes>
    50. <include>**/*.*include>
    51. includes>
    52. <excludes>
    53. <exclude>*.propertiesexclude>
    54. excludes>
    55. resource>
    56. <resource>
    57. <directory>src/main/${package.environment}directory>
    58. <includes>
    59. <include>**/*.*include>
    60. includes>
    61. <excludes>
    62. <exclude>*.propertiesexclude>
    63. excludes>
    64. <targetPath>${project.basedir}/target/classestargetPath>
    65. <filtering>truefiltering>
    66. resource>
    67. resources>
    68. <plugins>
    69. <plugin>
    70. <groupId>org.apache.maven.pluginsgroupId>
    71. <artifactId>maven-compiler-pluginartifactId>
    72. <version>3.1version>
    73. <configuration>
    74. <source>1.8source>
    75. <target>1.8target>
    76. <encoding>UTF8encoding>
    77. <compilerArguments>
    78. <bootclasspath>${java.home}\jre\lib\rt.jar;${java.home}\jre\lib\jce.jarbootclasspath>
    79. compilerArguments>
    80. configuration>
    81. plugin>
    82. <plugin>
    83. <groupId>org.springframework.bootgroupId>
    84. <artifactId>spring-boot-maven-pluginartifactId>
    85. <configuration>
    86. <mainClass>com.luck.MainApplicationmainClass>
    87. <layout>ZIPlayout>
    88. <includes>
    89. <include>
    90. <groupId>nojargroupId>
    91. <artifactId>nojarartifactId>
    92. include>
    93. includes>
    94. <outputDirectory>${global-jar-output}/${project.artifactId}outputDirectory>
    95. configuration>
    96. <executions>
    97. <execution>
    98. <goals>
    99. <goal>repackagegoal>
    100. goals>
    101. execution>
    102. executions>
    103. plugin>
    104. <plugin>
    105. <groupId>org.apache.maven.pluginsgroupId>
    106. <artifactId>maven-jar-pluginartifactId>
    107. <configuration>
    108. <archive>
    109. <manifest>
    110. <addClasspath>trueaddClasspath>
    111. <classpathPrefix>../lib/classpathPrefix>
    112. <useUniqueVersions>falseuseUniqueVersions>
    113. manifest>
    114. <manifestEntries>
    115. <Class-Path>${system-jar-classpath}Class-Path>
    116. manifestEntries>
    117. archive>
    118. configuration>
    119. plugin>
    120. <plugin>
    121. <groupId>org.apache.maven.pluginsgroupId>
    122. <artifactId>maven-dependency-pluginartifactId>
    123. <executions>
    124. <execution>
    125. <id>copy-libid>
    126. <phase>packagephase>
    127. <goals>
    128. <goal>copy-dependenciesgoal>
    129. goals>
    130. execution>
    131. executions>
    132. <configuration>
    133. <outputDirectory>${global-jar-output}/liboutputDirectory>
    134. <excludeTransitive>falseexcludeTransitive>
    135. <stripVersion>falsestripVersion>
    136. <silent>falsesilent>
    137. configuration>
    138. plugin>
    139. <plugin>
    140. <groupId>org.apache.maven.pluginsgroupId>
    141. <artifactId>maven-resources-pluginartifactId>
    142. <executions>
    143. <execution>
    144. <id>copy-configid>
    145. <phase>packagephase>
    146. <goals>
    147. <goal>copy-resourcesgoal>
    148. goals>
    149. <configuration>
    150. <resources>
    151. <resource>
    152. <directory>src/main/resourcesdirectory>
    153. <includes>
    154. <include>*.propertiesinclude>
    155. includes>
    156. resource>
    157. resources>
    158. <outputDirectory>${global-jar-output}/${project.artifactId}outputDirectory>
    159. configuration>
    160. execution>
    161. executions>
    162. plugin>
    163. plugins>
    164. build>
    165. <profiles>
    166. <profile>
    167. <id>testid>
    168. <properties>
    169. <package.environment>res-testpackage.environment>
    170. properties>
    171. profile>
    172. <profile>
    173. <id>devid>
    174. <activation>
    175. <activeByDefault>trueactiveByDefault>
    176. activation>
    177. <properties>
    178. <package.environment>resourcespackage.environment>
    179. properties>
    180. profile>
    181. profiles>
    182. project>

    生成 

     

     

     

     

     

     

    Java中读取application.properties文件

    java读取外部配置文件需要用到

    org.springframework.core.io.ClassPathResource.ClassPathResource这个类

    InputStream in = new ClassPathResource("application.properties").getInputStream()

    参考文章

    SpringBoot 部署 Jar 文件,瘦身优化指南 ! - 知乎

  • 相关阅读:
    国庆将至,景区游客爆满体验差,导览系统轻松解决问题
    国庆作业1
    Spring MVC 中的分页 RESTful API 响应
    数据结构与算法:栈(java)
    渗透测试基础 | 附带测试点、测试场景
    基于stm32的智能药盒
    面向对象分析?如何进行面向对象分析-获取需求?面向对象分析模型由三个独立模型组成:功能模型,对象模型,动态模型
    Solidity案例详解(四)投票智能合约
    强化学习之父Richard Sutton:通往AGI的另一种可能
    图片怎么转换成PDF格式?这两种方法赶紧记下
  • 原文地址:https://blog.csdn.net/anshichuxuezhe/article/details/126895193