美文网首页
Springboot 依赖jar包分离打包,部署

Springboot 依赖jar包分离打包,部署

作者: c608 | 来源:发表于2020-07-09 00:33 被阅读0次

    问题说明

    springboot构建jar部署,通过使用 java -jar xxx.jar 命令启动服务,非常方便,但是通过maven构建的jar包含 \BOOT-INF\lib\下的所有依赖jar包,导致jar包文件太大。

    构建思路

    在maven构建springboot项目jar时候,将lib文件夹分离出来。
    在运行jar的时候,能够应用到分离的lib包

    解决步骤分一共三步,只需要修改pom文件即可

    1.修改前pom文件,注意要点 <mainClass>com.xxx.xxx.xxxApplication</mainClass>记得指定为自己项目入口函数

    
          <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                        <mainClass>com.xxx.xxx.xxxApplication</mainClass>
                        <layout>ZIP</layout>
                        <!--<includes>-->
                            <!--<include>-->
                                <!--<groupId>nothing</groupId>-->
                                <!--<artifactId>nothing</artifactId>-->
                            <!--</include>-->
                        <!--</includes>-->
                    </configuration>
                </plugin>
    

    执行打包命令mvn clean install生成瘦身前jar包,可以看到有145M

    瘦身前jar包.png

    进入到jar包所在目录解压jar包,命令:jar -xvf xxx.jar,拷贝lib文件到目标目录下

    解压后jar包目录

    2.修改后pom文件,注意要点 <mainClass>com.xxx.xxx.xxxApplication</mainClass>记得指定为自己项目入口函数

          <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                        <mainClass>com.xxx.xxx.xxxApplication</mainClass>
                        <layout>ZIP</layout>
                          <includes>
                            <include>
                                <groupId>nothing</groupId>
                                <artifactId>nothing</artifactId>
                            </include>
                        </includes>
                    </configuration>
                </plugin>
    

    执行打包命令mvn clean install生成瘦身后jar包,可以看到只有9.2M

    瘦身后jar包

    3.把瘦身后jar包和lib文件放到相同的目录下

    执行以下命令

    相对路径:java -Dloader.path=\lib -jar xxx.jar
    绝对路径:java -Dloader.path=/Users/apple/Desktop/testpdf/tes/lib -jar xxx.jar
    

    其中springboot对于配置文件读取有如下方式:

    1、读取jar包同级目录下的config目录中的properties文件,优先级最高;
    2、读取jar包同级目录下的properties文件,优先级次之;
    3、读取classpath下的config文件夹中的properties文件,优先级第三;
    4、在classpath下直接放配置文件,优先级最低。
    

    参考链接

    相关文章

      网友评论

          本文标题:Springboot 依赖jar包分离打包,部署

          本文链接:https://www.haomeiwen.com/subject/aydlcktx.html