SpringBoot是一套脚手架,默认打包方式会把依赖打进最终的包里面,所以执行java -jar xxx.jar
就可以很方便的运行,但是这样的问题是造成整个jar的体积变得非常大。
即便是有完整的ci/cd的情况下,在做deploy的时候会也会发生较大的文件传输,像如果是做数据服务类场景,会引入大量hadoop生态圈的client,最终的jar轻松上500MB+,造成整个ci的周期很长。
仔细看大部分的标准二进制发现版,他们的程序结构其实是这样的:
/app /bin /sbin /lib /config
|
这样的目录看起来就很整洁,那么有没有办法把spring的项目打包最终的结构变成这样呢:
/app /bin /lib /config 你的业务jar
|
config放配置文件,lib放第三方依赖,包括pom的依赖,bin写一些启动脚本,这样的话业务jar通常就只有几十kb了,这样的话在做ci的时候可以通过变量控制,只需要传递几十kb就好了,速度会很快。
<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>${project.parent.version}</version> <configuration> <fork>true</fork> <includeSystemScope>true</includeSystemScope> <includes> <include> <groupId>nothing</groupId> <artifactId>nothing</artifactId> </include> </includes> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin>
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>${java.version}</source> <target>${java.version}</target> <encoding>UTF-8</encoding> <compilerArguments> <verbose/> <bootclasspath>${java.home}/lib/rt.jar:${java.home}/lib/jce.jar</bootclasspath> </compilerArguments> </configuration> </plugin>
<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> <overWriteReleases>false</overWriteReleases> <overWriteSnapshots>false</overWriteSnapshots> <overWriteIfNewer>true</overWriteIfNewer> </configuration> </execution> </executions> </plugin>
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> <mainClass>xxx.xxx.xxxx.XXX</mainClass> </manifest> </archive> <excludes> <exclude>application.yml</exclude> </excludes> </configuration> </plugin>
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <executions> <execution> <id>copy-jar-file</id> <phase>package</phase> <goals> <goal>run</goal> </goals> <configuration> <target> <copy todir="${project.build.directory}/config"> <fileset dir="${project.basedir}/src/main/resources"> <include name="*" /> <include name="*/*" /> <include name="*/*/*" /> </fileset> </copy> </target> </configuration> </execution> </executions> </plugin>
|
从功能上来说,maven有很好的插件,可以直接通过配置上面的插件,达到想要的效果。
扫码手机观看或分享: