java脑洞 springboot 瘦身之路

脑洞的由来

场景一:微服务部署,每个微服的包体都几十M,而且每个微服包体之间都有大量重复jar包

功能需求

比如当前项目是lp-spring-web,打包出来的包体是lp-spring-web.jar

  1. lp-spring-web.jar 只包含项目的字节码文件,也就只有class文件,而没有lib文件
  2. lp-sping-web项目依赖的jar包需要一份完整copy在lp-spring-web.jar外

Git地址

https://gitee.com/wqrzsy/lp-demo/tree/master/lp-spring-web

更多demo请关注

springboot demo实战项目
java 脑洞
java 面试宝典
开源工具

功能实现

    <build>
        <plugins>

            <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>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <layout>ZIP</layout>
                    <includes>
                        <include>
                            <groupId>non-exists</groupId>
                            <artifactId>non-exists</artifactId>
                        </include>
                    </includes>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                        <configuration>
                            <classifier>classes</classifier>
                            <attach>false</attach>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>

执行 mvn package 后

image.png

image.png

我们打开lib文件夹就会看到相关依赖的jar包都在里面
image.png

测试

我们写一个bat来测试下是否成功

java -jar -Dloader.path=./target,./target/lib  ./target/lp-spring-web-0.0.1-SNAPSHOT.jar

PS: 路径可自行修改
image.png

运行run.bat 后就会看到项目已正常启动
image.png

demo项目导入

参考: https://www.jianshu.com/p/cd0275a2f5fb

#公众号
五分钟了解前沿技术,大数据,微服务,区域链,提供java前沿技术干货,独立游戏制作技术分享

五分钟技术

如果这篇文章对你有帮助请给个star
image.png

发布了26 篇原创文章 · 获赞 0 · 访问量 424

猜你喜欢

转载自blog.csdn.net/wqr503/article/details/104246232