Automatic analytical slimming

The final build of Spring Boot projects deals with the size of JAR packages, which has always been a problem. You need to build all dependencies into the final output of a runnable JAR. It is possible to separate dependent jars from runnable jars using other plug-in extensions, such as slot-maven-plugin, but this approach does not reduce the size of the dependent JARS.

Spring Boot 2.4 provides automatic slimming for build output JAR analysis, automatically removing empty Starter Dependencies when build output runnable jars

Results show

Build a running JAR based on Spring Boot 2.4.0 and Spring Boot 2.3.6. What is empty Starter

  • Use start.spring. IO to create an empty Spring Boot project without introducing any dependencies
  • MVN Clean Install builds the associated runnable JARS

  • Unzip the two jars separately into two different directories
Tar -zxvf demo-2.3.6. Jar -c demo-2.3.6/



Tar -zxvf demo-2.4.0.jar -c demo-2.4.0/

Copy the code
  • According to the statistics on the number of dependent jars, there are 19 dependent jars in 2.3.6 while there are only 18 dependent jars in 2.4.0, which lacks spring-boot-starter
cdDemo - 2.3.6 / BOOT - INF/lib && ll - h | wc -l

19



cdDemo - 2.4.0 / BOOT - INF/lib && ll - h | wc -l

18

Copy the code

What is Empty Starter

As mentioned above, the Empty Starter Dependencies Jar of this class is automatically removed in Spring Boot 2.4 when we create projects based on start.spring. IO

    <dependency>

      <groupId>org.springframework.boot</groupId>

      <artifactId>spring-boot-starter</artifactId>

    </dependency>

Copy the code

What’s special about spring-boot-stater?

  • ① The empty JAR contains no code
  • ② There are references to other jars, only batch import other jars

So this type of JAR doesn’t make sense when building into a runnable JAR, because all the dependent jars that are bulk imported can be imported. At present, most of the starter provided by Spring Boot, such as Redis and AMQP, are such jars, so they will be automatically deleted after construction.

Custom JARS achieve automatic slimming

  • createMANIFEST.MFJar package meta information, add a lineSpring-Boot-Jar-Type: dependencies-starterCan be
resources

├ ─ ─ meta-inf

└ ─ ─ the MANIFEST. MF



Copy the code