SpringBoot打包后Jar包很大怎么办?

问题:

以甲蛙博客为例:按传统方法打成一个Jar包,大小大概是41M:

图片

主要是在Jar里的blog1.jar\BOOT-INF\lib\这个目录下,把所有依赖的第三方Jar全加进来了。

解决方法:

第1步:将第三方Jar单独存放,使用下面的命令,把依赖的包放到D:\temp\lib:

1
mvn dependency:copy-dependencies -DoutputDirectory=D:\temp\lib -DincludeScope=runtime

第2步:单独打包我们自己写的代码,修改pom.xml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <mainClass>com.jiawa.blog.config.BlogApplication</mainClass>
        <layout>ZIP</layout>
        <includes>
            <include>
                <groupId>nothing</groupId>
                <artifactId>nothing</artifactId>
            </include>
        </includes>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>repackage</goal>
            </goals>
        </execution>
    </executions>
</plugin>

打出来的包,只有几百K

图片

第3步,怎么运行呢?使用如下命令,相关的路径需要改成自己的

1
java -Dloader.path=d:\temp\lib -jar target/blog.jar

大功告成!!!

后续项目迭代,如果有加第三方依赖,重复第1步,导出所有Jar,再传到服务器上。如果没有,则可忽略第1步