【转】SpringBoot多模块打包瘦身分离

背景

首先项目是个多层级的多模块springBoot项目,每次打出来的jar包都在90M,其中包含核心代码以及所有依赖的jar包,上传到服务器速度比较慢。

由于核心代码(controller、service、dao、model)会经常改动进行发布上线,而依赖的jar包(pom文件的依赖引用)并不是经常更新,所以希望进行分离打包,改动代码只需要上线发布核心jar包(几十KB吧),提高效率。


打包方法

 
步骤1:清理之前的jar包

步骤2:进行重新打包
不分离打包

1、pom文件配置

    <build>
            <finalName>TC_NLP_Platform</finalName>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>



2、 箭头指向的就是完整的可运行的jar包,根据项目的不同大小不同,整体来说还是很大的。

 

 


 
分离打包

1、pom文件配置,由于项目是多模块的(分层的),所以打包的时候希望TCSP_Management(controller层)、TCSP_DAO、TCSP_Model、TCSP_Service可以合在一起打成一个核心jar包,其他的依赖打包到另外一个地方,达到核心代码与依赖分离打包的目的。
部分配置解释

a、<!--依赖jar包的输出目录,根据自己喜好配置-->

     <outputDirectory>${project.build.directory}/lib</outputDirectory>

      将不经常改动的依赖包,打包到lib文件夹下

 b、<!--排除以下artifactId的jar包-->
    <excludeArtifactIds>TCSP_Model,TCSP_DAO,TCSP_Service</excludeArtifactIds>

     目的是排除TCSP_Management(controller层)、TCSP_DAO、TCSP_Model、TCSP_Service,核心的业务包,不打包到lib文件夹下面

 

   <build>
            <finalName>TC_NLP_Platform</finalName>
            <plugins>
                <plugin>
                    <!--打包时去除第三方依赖-->
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                        <executable>true</executable>
                        <layout>ZIP</layout>
                        <!--将下面三个模块的jar包,打包到核心包中-->
                        <includes>
                            <include>
                                <artifactId>TCSP_Model</artifactId>
                                <groupId>com.toycloud.tcspeech</groupId>
                            </include>
                            <include>
                                <artifactId>TCSP_DAO</artifactId>
                                <groupId>com.toycloud.tcspeech</groupId>
                            </include>
                            <include>
                                <artifactId>TCSP_Service</artifactId>
                                <groupId>com.toycloud.tcspeech</groupId>
                            </include>
                        </includes>
     
                    </configuration>
                    <executions>
                        <execution>
                            <goals>
                                <goal>repackage</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
                <!--拷贝第三方依赖文件到指定目录-->
                <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>
                                <!--依赖jar包的输出目录,根据自己喜好配置-->
                                <outputDirectory>${project.build.directory}/lib</outputDirectory>
                                <overWriteReleases>false</overWriteReleases>
                                <overWriteSnapshots>false</overWriteSnapshots>
                                <overWriteIfNewer>true</overWriteIfNewer>
                                <!--排除以下artifactId的jar包-->
                                <excludeArtifactIds>TCSP_Model,TCSP_DAO,TCSP_Service</excludeArtifactIds>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>


2、 下图中,1表示的就是所有依赖打包后所在的路径,2表示核心代码所在的路径。

核心代码解压后,包含TCSP_DAO、TCSP_Model、TCSP_Service三个核心模块的jar包

发布到服务器运行:

 java -Dloader.path=./lib -Duser.timezone=Asia/Shanghai -jar TC_NLP_Platform.jar

注意:lib文件夹(依赖的jar包)必须和TC_NLP_Platform.jar(核心jar包)在同级目录

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章