赞
踩
效果图:
这样打包的优势:
1.每个业务模块(可以单独启动)的jar包与它所依赖的库分离,业务jar包更轻量,不仅运行时节省内存,而且方便日后的更新部署
2.各个业务模块之间依赖的库文件中有很多是相同的,打到同一个lib目录(而不是各自的lib目录),可以节省空间
pom.xml 片段
<build> <plugins> <!-- 其他plugin省略 以下重点是<outputDirectory>中的boot-jar-output,第一处是lib目录第二处是jar包目录 --> <!-- 拷贝项目所有依赖jar文件到构建lib目录下 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>copy-dependencies</id> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <!-- 各子模块按照实际层级定义各模块对应的属性值,检查所有微服务模块依赖jar文件合并复制到同一个目录 详见各子模块中 boot-jar-output 属性定义 --> <outputDirectory>${boot-jar-output}/lib</outputDirectory> <excludeTransitive>false</excludeTransitive> <stripVersion>false</stripVersion> <silent>false</silent> </configuration> </execution> </executions> </plugin> <!-- Spring Boot模块jar构建 --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <includes> <!-- 不存在的include引用,相当于排除所有maven依赖jar,没有任何三方jar文件打入输出jar --> <include> <groupId>null</groupId> <artifactId>null</artifactId> </include> </includes> <layout>ZIP</layout> <!-- 基于maven-jar-plugin输出微服务jar文件进行二次spring boot重新打包文件的输出目录 所有微服务构建输出jar文件统一输出到与lib同一个目录,便于共同引用同一个lib目录 详见各子模块中boot-jar-output属性定义 --> <!-- --> <outputDirectory>${boot-jar-output}</outputDirectory> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
假设 maven打包路径是 D:\n1 ,命令如下
$ mvn clean install -Dmaven.test.skip=true -Dboot-jar-output=“D:\n1”
参考文章
1.SpringBoot 部署 Jar 文件,瘦身优化指南
Level 2:合并所有模块依赖jar到同一个lib目录
https://mp.weixin.qq.com/s/hstk9tXzKt-Up20kD5x0fg
2.Maven: specify the outputDirectory only for packaging a jar?
Configuring the Jar Plugin
https://stackoverflow.com/questions/4757426/maven-specify-the-outputdirectory-only-for-packaging-a-jar
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。