1、简介
在使用springBoot框架进行项目开发的过程中,使用Maven打的jar包会把依赖的lib目录中的jar包都包含进来,会导致单个jar包特别大,不利于项目部署。在这种情况下,可以采用依赖外置的方式,Maven打的jar包只包含源码,不包含依赖,这样的jar包可以缩小到几MB。
本文介绍单个项目和带有父项目含有多个模块的大型项目,以及公共模块没有主启动类打包出错的解决方案。
2、单个项目
2.1、将依赖的jar包保存到指定的目录下
pom.xml添加对应配置插件如下:
<!-- 复制依赖jar包到指定目录(在package阶段) -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.6.0</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>E:/jar</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
2.2、打包不包含依赖
pom.xml添加对应配置插件如下:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!-- 主启动类全限定类名 -->
<mainClass>*.*.Application</mainClass>
<layout>ZIP</layout>
<!-- 打包不含有依赖jar -->
<includes>
<include>
<groupId>nothing</groupId>
<artifactId>nothing</artifactId>
</include>
</includes>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
3、父项目含有多个模块
3.1、父项目配置
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<layout>ZIP</layout>
<!-- 打包不含有依赖jar -->
<includes>
<include>
<groupId>nothing</groupId>
<artifactId>nothing</artifactId>
</include>
</includes>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- 复制依赖jar包到指定目录(在package阶段) -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.6.0</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>E:/jar</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
3.2、子模块配置
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
4、公共模块没有主启动类打包报错解决方式
在公共模块的pom文件中添加:
<!-- 公共模块跳过打包 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
5、项目启动
项目启动通过 -Dloader.path=E:/jar 指定依赖路径即可。
java -Xmx**m -Xmx**m -Dloader.path=E:/jar **.jar &
总结:本文详细介绍单个项目和带有父项目含有多个模块的大型项目,以及公共模块没有主启动类打包出错的解决方案。
本人是一个从小白自学计算机技术,对运维、后端、各种中间件技术、大数据等有一定的学习心得,想获取自学总结资料(pdf版本)或者希望共同学习,关注微信公众号:it自学社团。后台回复相应技术名称/技术点即可获得。(本人学习宗旨:学会了就要免费分享)