SpringBoot构建jar包与依赖包分离

1.分离原由

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

2.正常构建

  • pom.xml文件

 

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
  • target目录

     

    正常构建target目录

  • 构建后的jar包结构:

 

|--META-INF
|--BOOT-INF
    |--classes
    |--lib
|--org
  • 测试是否正常运行

 

java -jar demo-0.0.1-SNAPSHOT.jar

3.分离构建

  • pom.xml文件修改

 

<build>
        <plugins>
            <!--设置应用 Main 参数启动依赖查找的地址指向外部 lib 文件夹-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>

            <!--设置 SpringBoot 打包插件不包含任何 Jar 依赖包-->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <includes>
                        <include>
                            <groupId>nothing</groupId>
                            <artifactId>nothing</artifactId>
                        </include>
                    </includes>
                </configuration>
            </plugin>

            <!--设置将 lib 拷贝到应用 Jar 外面-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
  • target目录

     

    分离依赖包构建target目录

  • 构建后的jar包结构:

 

|--META-INF
|--BOOT-INF
    |--classes
|--org
  • 测试是否正常运行

 

java -jar demo-0.0.1-SNAPSHOT.jar



作者:hangzhi
链接:https://www.jianshu.com/p/938e3fc5c140
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值