SpringBoot部署起来虽然简单,如果服务器部署在公司内网,速度还行,但是如果部署在公网,部署起来实在头疼:编译出来的 Jar 包很大,如果服务器带宽不是很高,上传一个 Jar 就非常慢了,今天就教大家怎么将SpringBoot项目瘦身。

瘦身前的Jar包

红色的是没瘦身之前的,蓝色是瘦身之后的,两者相差≈73

瘦身

1、修改pom文件

<!-- 只有项目所需要运行的代码 -->
<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <layout>ZIP</layout>
        <includes>
            <include>
                <groupId>nothing</groupId>
                <artifactId>nothing</artifactId>
            </include>
        </includes>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>
                    repackage
                </goal>
            </goals>
        </execution>
    </executions>
</plugin>

<!-- 单独打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>
                <outputDirectory>target/lib</outputDirectory>
                <excludeTransitive>false</excludeTransitive>
                <stripVersion>false</stripVersion>
                <includeScope>runtime</includeScope>
            </configuration>
        </execution>
    </executions>
</plugin>

2、执行 mvn clean install

3、运行

java -Dloader.path=lib所在的目录 -jar  Jar文件名称

4、配合阿里云的Cloud Toolkit部署更方便

传送