SpringBoot依赖外置

Easter79
• 阅读 575

背景

近期有个SpringBoot的项目需要频繁更新,但是每次上传到服务器上几十MB,实在是花时间,所以打算优化打包方案,将第三方依赖外置

流程

  • 首先使用SpringBoot打包插件将第三方排除,但是一些版本号同步更新的本地模块依赖需要放到一个jar中
  • 使用maven dependency插件将第三方依赖复制到构建目录中
  • 使用maven过滤功能实现一个启动脚本
  • 使用assembly打包一个完整版,包括boot jar,第三方依赖,启动脚本
  • 第一次部署使用完整版,后续更新只需要上传boot jar就行了

实现方法

首先在 resource 目录中准备一个脚本

内容如下

java -Dloader.path=lib/ -Dfile.encoding=utf-8 -jar @project.build.finalName@.jar

其中 @project.build.finalName@ 是最后生成的可执行jar的文件名 path指定第三方依赖目录


其次修改pom文件如下

    <build>
        <resources>
            <resource>
            <!-- 启用maven过滤,主要为脚本做准备 -->
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
             </resource>
        </resources>

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

        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>xxx.Application</mainClass>

                    <layout>ZIP</layout>
                    <executable>true</executable>
                    <includes>
                    <!-- 以下为需要打包到jar中的本地模块依赖,主要是版本号需要更新,如果放到第三方依赖中,可能会出现多个版本  -->
                        <include>
                            <groupId>xxx.xxx</groupId>
                            <artifactId>upload-starter</artifactId>
                        </include>
                        <include>
                            <groupId>xxx.xxx</groupId>
                            <artifactId>pay</artifactId>
                        </include>
                        <include>
                            <groupId>${project.parent.groupId}</groupId>
                            <artifactId>swagger-starter</artifactId>
                        </include>
                    </includes>

                </configuration>
            </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>
          <!-- 输出的第三方依赖位置 -->          <outputDirectory>${project.build.directory}/lib</outputDirectory>
                            <overWriteReleases>false</overWriteReleases>
                            <!-- 此处排除需要打到boot jar中的本地模块依赖 -->
                            <excludeGroupIds>
                                ${project.parent.groupId},xxx.xxx
                            </excludeGroupIds>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <descriptors>
                        <descriptor>assembly.xml</descriptor>
                    </descriptors>
                </configuration>
                <executions>
                    <execution><!-- 配置执行器 -->
                        <id>make-assembly</id>
                        <phase>package</phase><!-- 绑定到package生命周期阶段上 -->
                        <goals>
                            <goal>single</goal><!-- 只运行一次 -->
                        </goals>

                        <!--                        <configuration>-->
                        <!--                            <finalName>${project.name}</finalName>-->

                        <!--                            <descriptor>src/main/assembly.xml</descriptor>&lt;!&ndash;配置描述文件路径&ndash;&gt;-->
                        <!--                        </configuration>-->
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

pom.xml 同级目录创建 assembly.xml

内容如下

<assembly>
    <id>release</id>
    <formats>
        <format>zip</format><!--打包的文件格式,也可以有:war zip-->
    </formats>
    <!--tar.gz压缩包下是否生成和项目名相同的根目录-->
    <includeBaseDirectory>false</includeBaseDirectory>
    
    <!-- 如果使用这个,也可以不使用maven-dependency插件 -->
<!--    <dependencySets>-->
<!--        <dependencySet>-->
<!--            &lt;!&ndash;是否把本项目添加到依赖文件夹下&ndash;&gt;-->
<!--            <useProjectArtifact>true</useProjectArtifact>-->
<!--            <outputDirectory>lib</outputDirectory>-->
<!--            &lt;!&ndash;将scope为runtime的依赖包打包&ndash;&gt;-->
<!--            <scope>runtime</scope>-->
<!--        </dependencySet>-->
<!--    </dependencySets>-->
    <fileSets>
        <fileSet>
            <directory>${project.build.directory}/lib</directory>
            <outputDirectory>/lib</outputDirectory>
        </fileSet>
    </fileSets>
    <files>
        <file>
            <source>target/${build.finalName}.jar</source>
            <outputDirectory>/</outputDirectory>
        </file>
        <file>
           <!-- 此处将脚本复制两份,分别对应类unix和windows系统,注意,maven过滤之后的文件在target目录下 --> <source>${project.build.directory}/classes/start.sh</source>
            <outputDirectory>/</outputDirectory>
            <destName>start.bat</destName>
        </file>
        <file>
            <source>${project.build.directory}/classes/start.sh</source>
            <outputDirectory>/</outputDirectory>
            <destName>start.sh</destName>
        </file>
    </files>
</assembly>

最终效果

SpringBoot依赖外置

可以很明显的看出两个方式打包的大小差异


完整版的目录结构 SpringBoot依赖外置

点赞
收藏
评论区
推荐文章
红烧土豆泥 红烧土豆泥
2年前
Spring Boot:jar中没有主清单属性
使用SpringBoot微服务搭建框架时,使用IDEA可以正常运行,但是使用MAVEN打包工具打包成jar后运行时,提示错误:未找到主清单目录。查看pom文件,发现已添加SpringBoot的构建插件xmlorg.springframework.bootspringbootmavenplugin2.4.1
Easter79 Easter79
2年前
springboot使用之快捷打包部署
本篇关键知识点:Maven的assembly插件实现自定义打包部署(包含依赖jar包) 目前springboot项目的几种常见的部署方式。1\.使用docker容器去部署,将springboot的应用构建成一个dockerimage,然后通过容器去启动镜像,这种方式在
Easter79 Easter79
2年前
springboot热部署实战
每次代码改动后都需要重新手动Run项目,心累,在网上找了下,发现SpringBoot提供了热部署的方案,改动代码后自动编译打包,现在将热部署的配置方法记下来:第一步:在pom.xml中添加依赖,导入springbootdevtools<dependency<groupIdorg.springframework
Stella981 Stella981
2年前
Maven项目使用打包时使用本地jar包库
在使用maven管理项目时,有时候我们可能会使用一些第三方的jar包依赖库,但是这些jar包依赖库又没有在共有的maven仓库。通常只能下来放到本项目的lib目录下。但是我们打包时如果不做处理,那么打包后的fatjar中不会有lib文件夹中的相关jar包。打包后无法运行起来,因此需要做特殊处理,让maven打包时能够把使用到外部jar打进去。主要就是在
Easter79 Easter79
2年前
SpringBoot打包部署简单说明
SpringBoot项目打包部署1\.jar包部署添加一个插件<!打包插件<build<plugins<plugin<groupId
Stella981 Stella981
2年前
Spring Boot项目使用maven
springbootassembly1.在springboot项目中使用mavenprofiles和mavenassembly插件根据不同环境打包成tar.gz或者zip2.将springboot项目中的配置文件提取到外部config目录中3.将springboot项目中
Easter79 Easter79
2年前
Springboot基于assembly的服务化打包方案
  在使用assembly来打包springboot微服务项目前,我想说一说,目前springboot项目的几种常见的部署方式。1. 使用docker容器去部署,将springboot的应用构建成一个dockerimage,然后通过容器去启动镜像,这种方式在需要部署大规模的应用和应用扩展时是非常方便的,属于目前工业级的部署方案,但是需要掌握d
Stella981 Stella981
2年前
SpringBoot打包部署简单说明
SpringBoot项目打包部署1\.jar包部署添加一个插件<!打包插件<build<plugins<plugin<groupId
Stella981 Stella981
2年前
Linux下部署springboot项目的步骤及过程
最近在研究将springboot项目部署到Linux服务器上,由于springboot是内嵌了tomcat,所以可以直接将项目打包上传至服务器上。我是在idea上的项目,所以我就基于此说下过程。(一)打包你的项目1、在pom文件中添加springboot的maven插件 !(https://img2018.cnblogs.com/blog/1
Stella981 Stella981
2年前
SpringBoot依赖外置
背景近期有个SpringBoot的项目需要频繁更新,但是每次上传到服务器上几十MB,实在是花时间,所以打算优化打包方案,将第三方依赖外置流程首先使用SpringBoot打包插件将第三方排除,但是一些版本号同步更新的本地模块依赖需要放到一个jar中使用mavendependency插件将第三方依赖复制到构建目录中
Easter79
Easter79
Lv1
今生可爱与温柔,每一样都不能少。
文章
2.8k
粉丝
5
获赞
1.2k