使用maven的springboot项目依赖和代码分开打包

背景
微服务项目,各微服务划分混乱,依赖也十分混乱,导致随便一个服务打的 jar 包都达到300M+,但是其实写的代码量少得可怜,又加上代码迭代频繁,每次更新传 jar 包都得老半天。

1 确定方案

最开始设想的方案是梳理下项目混乱的 maven 依赖,毕竟每个微服务都带个登录模块像话嘛!但是开始梳理就发现不对劲了,这个涉及的东西也太多了,改别人的代码,这不是纯纯地出力不讨好嘛。而且项目使用的是公司自研的框架,这玩意也都混乱的,得,这个方案毙掉。
然后,发现可以分离业务代码和依赖打包,这样因为每次更新的都是业务代码,所以只需要一开始把依赖上传到服务器上,后面只需要把业务代码打包上传就可以了,这个会节省很多传输时间。

2 实现

Spring Boot 提供了 -Dloader.path 参数,用于在运行时指定一个外部的 JAR 文件目录或者外部 JAR 文件路径。通过这种方式,你可以避免将所有的依赖包打包到主 JAR 文件中,从而减小打包后的 JAR 文件体积。
上面是原理,下面是实操。
其他的都不用改,改下 pom.xml 中的构建插件配置就行。

<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.mediway.hos</groupId>
        <artifactId>hil-base-parent</artifactId>
        <version>1.0.0.RELEASE</version>
    </parent>

    <artifactId>hil-web</artifactId>
    <packaging>jar</packaging>


    <dependencies>
        <!-- 本地模块hil-service,默认compile作用域 -->
        <dependency>
            <groupId>com.mediway.hos</groupId>
            <artifactId>hil-service</artifactId>
        </dependency>
    </dependencies>


    <build>
        <plugins>
            <!--打包的时候去除依赖包-->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <!--启动类路径-->
                    <mainClass>com.mediway.HilBaseApplication</mainClass>
                    <layout>ZIP</layout>
                    <includes>
                        <!-- 必须包含本地模块 -->
                        <!--依赖自己其它项目的工程,也是会经常变动的,所以不宜打到外部的lib,不然就会需要经常上传更新-->
                        <include>
                            <groupId>com.mediway.hos</groupId>
                            <artifactId>hil-service</artifactId>
                        </include>

                        <!-- 内嵌Tomcat和关键依赖 -->
                        <include>
                            <groupId>org.springframework.boot</groupId>
                            <artifactId>spring-boot-starter-tomcat</artifactId>
                        </include>
                        <include>
                            <groupId>org.apache.tomcat.embed</groupId>
                            <artifactId>tomcat-embed-core</artifactId>
                        </include>
                        <include>
                            <groupId>org.apache.tomcat.embed</groupId>
                            <artifactId>tomcat-embed-el</artifactId>
                        </include>
                        <include>
                            <groupId>org.apache.tomcat.embed</groupId>
                            <artifactId>tomcat-embed-websocket</artifactId>
                        </include>
                        <include>
                            <groupId>org.glassfish</groupId>
                            <artifactId>jakarta.el</artifactId>
                        </include>
                    </includes>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <!--target/lib是依赖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>target/lib</outputDirectory>
                            <excludeTransitive>false</excludeTransitive>
                            <stripVersion>false</stripVersion>
                            <includeScope>runtime</includeScope>
                            <excludeArtifactIds>hil-service</excludeArtifactIds> <!-- 避免重复复制 -->
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

这样使用 mvn package 之后,得到的 target 就是一个包含依赖的 lib 目录和只包含业务代码的 jar 包。
启动命令,加上依赖的目录就行。

java -Dloader.path=./lib -jar hil-web-1.0.0.RELEASE.jar

分离之后业务代码的 jar 包从300MB+ --> 5MB。

posted @ 2024-12-30 16:54  大唐冠军侯  阅读(512)  评论(0)    收藏  举报