在项目开发中,测试环境时需要上传到服务器,maven依赖比较多时,打包后的jar包会比较大,上传需要很多时间。简单思路如下:
maven依赖一般不会随意改动,将maven依赖的jar包单独导出来,上传到服务器,通过 -Dloader.path=来指定jar包的路径
- 先将jar包导出:(-DoutputDirectory为导出路径)
mvn dependency:copy-dependencies -DoutputDirectory=C:\Users\xxx\Desktop\lib -DincludeScope=runtime
将lib目录上传到服务器
- 在项目进行打包前,进行配置如下:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<layout>ZIP</layout>
<includes>
<!--去除所有的jar包-->
<include>
<groupId>nothing</groupId>
<artifactId>nothing</artifactId>
</include>
<!--保留需要的jar包,比如多模块项目中的 common 模块,-->
<include>
<groupId>com.xxx</groupId>
<artifactId>xxx-common</artifactId>
</include>
</includes>
</configuration>
</plugin>
配置后打包体积将缩小很多
- 在启动SpringBoot程序时加上jar包的路径:
java -Dloader.path=/root/xxx/lib/ -jar xxx.jar
-Dloader.path用来指定jar包的路径。