SpringBoot打jar包瘦身-依赖文件夹lib和配置文件夹config分离出来

背景

测试环境修复bug时,经常需要修改代码替换jar包,由于之前使用SpringBoot的默认配置,导致依赖lib文件夹和配置文件夹config都打进jar,使得jar包很大,传输到测试机器上浪费时间,于是琢磨打jar包时将依赖文件夹lib和配置文件夹config分离出来。
原先的pom.xml的配置如下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    

    .............
  

    

    <build>

        <finalName>${project.artifactId}-${project.version}</finalName>


        <plugins>
            <!--定义项目的编译环境-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <!--maven的测试用例插件,建议跳过。-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>

            <!--打成jar包-->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.iflytek.screen.LlmScreenServiceApplication</mainClass>
                </configuration>
            </plugin>
        </plugins>

    </build>

</project>

打成jar包时,大小约98M,如下所示:
image

解决方案

现在开始进行改造,pom.xml修改如下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    

    ......

    <build>

        <finalName>${project.artifactId}-${project.version}</finalName>


        <plugins>
            <!--定义项目的编译环境-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <!--maven的测试用例插件,建议跳过。-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>

            <!-- 打JAR包 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <!-- 不打包资源文件(配置文件和依赖包分开) -->
                    <!-- 根据实际情况 -->
                    <excludes>
                        <exclude>*.yml</exclude>
                        <exclude>*.properties</exclude>
                        <exclude>logback-spring.xml</exclude>
                        <exclude>shell/*.sh</exclude>
                    </excludes>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <!-- MANIFEST.MF 中 Class-Path 加入前缀 -->
                            <classpathPrefix>lib/</classpathPrefix>
                            <!-- jar包不包含唯一版本标识 -->
                            <useUniqueVersions>false</useUniqueVersions>
                            <!--指定启动类 -->
                            <mainClass>com.iflytek.screen.LlmScreenServiceApplication</mainClass>
                        </manifest>
                        <manifestEntries>
                            <!--MANIFEST.MF 中 Class-Path 加入资源文件目录 -->
                            <Class-Path>./config/</Class-Path>
                        </manifestEntries>
                    </archive>
                    <outputDirectory>${project.build.directory}</outputDirectory>
                </configuration>
            </plugin>
            <!-- 该插件的作用是用于复制依赖的jar包到指定的文件夹里 -->
            <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>${project.build.directory}/lib/</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <!-- 该插件的作用是用于复制指定的文件 -->
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <executions>
                    <execution> <!-- 复制配置文件 -->
                        <id>copy-resources</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <resources>
                                <resource>
                                    <directory>src/main/resources</directory>
                                    <includes>
                                        <!-- 根据实际情况 -->
                                        <include>*.yml</include>
                                        <include>*.properties</include>
                                        <include>logback-spring.xml</include>
                                        <include>shell/*.sh</include>
                                    </includes>
                                </resource>
                            </resources>
                            <outputDirectory>${project.build.directory}/config</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>

    </build>

</project>


重新打包之后,发现target文件夹下多了configlib文件夹,jar包大小也缩小为166K。
image

这样当修改完bug重新打包之后,只需要上传166K的jar即可,减少上传时间。

Reference

给你的 SpringBoot 工程部署的 jar 包瘦瘦身吧! - 雨点的名字 - 博客园

Spring Boot项目瘦身

posted @ 2023-11-28 16:46  Reecelin  阅读(15)  评论(0编辑  收藏  举报