Springboot father-son project packaging comparison: maven-shade-plugin and spring-boot-maven-plugin

Table of Contents

spring-boot-maven-plugin packaging

Install plug-in: spring-boot-maven-plugin

Run the packaging command jar

maven-shade-plugin packaging

Install the plugin maven-shade-plugin

Run the packaging command jar

Compare shade and spring-maven-plugin


Tool chain: ideal, springboot, maven, parent-child module, shade, spring-boot-maven-plugin

spring-boot-maven-plugin packaging

Install plug-in: spring-boot-maven-plugin

Configure the plug-in in the maven project pom file, the configuration is as follows

<build>
    <plugins>
    
        <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.7.4</version>
                <executions>
                    <execution>
                        <!-- Bind the package phase of the Maven life cycle -->
                        <phase>package</phase>
                        <goals>
                            <!-- When the package phase is executed, let it call the repackage target of the plug-in and take over maven-jar-plugin-->
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
                <!-- Modify plug-in properties -->
                <configuration>
                    <!-- Specify the entry class. If there is only one main function class startup class, no configuration is required -->
                    <!--<mainClass>XXX</mainClass>-->
                </configuration>
            </plugin>

    </plugins>
</build>
  • After the installation is complete, you can see in the ideal maven plug-in view that a spring-boot plugin has been added.

  • Run packaging command jar

Run command:

mvn package

or double click

The effect after packaging:

Open the jar package and view the jar package information file Manifest.MF file. This file contains the following information:

  1. jar package name
  2. class file address
  3. Depends on lib file address
Manifest-Version: 1.0
Created-By: Maven JAR Plugin 3.2.2
Build-Jdk-Spec: 11
Implementation-Title: boundary-XXX
Implementation-Version: 0.0.1-SNAPSHOT
Main-Class: org.springframework.boot.loader.JarLauncher
Start-Class: com.XXX.XXX.XXXsApplication
Spring-Boot-Version: 2.7.4
Spring-Boot-Classes: BOOT-INF/classes/
Spring-Boot-Lib: BOOT-INF/lib/
Spring-Boot-Classpath-Index: BOOT-INF/classpath.idx
Spring-Boot-Layers-Index: BOOT-INF/layers.idx

Open the package and observe that the dependent jar form is an overall package ending with jar

maven-shade-plugin packaging

Install plug-in maven-shade-plugin

The configuration information is as follows

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.2</version>
                <dependencies>
                    <dependency>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-maven-plugin</artifactId>
                        <version>2.0.1.RELEASE</version>
                    </dependency>
                </dependencies>
                <configuration>
                    <artifactSet>
                        <excludes>
                            <!-- Exclude logback dependency -->
                        </excludes>
                    </artifactSet>
                    <filters>
                        <filter>
                            <artifact>*:*</artifact>
                            <excludes>
                                <exclude>META-INF/*.SF</exclude>
                                <exclude>META-INF/*.DSA</exclude>
                                <exclude>META-INF/*.RSA</exclude>
                            </excludes>
                        </filter>
                    </filters>
                </configuration>
                <executions>
                    <execution>
                        <id>shade-my-jar</id>
                        <!-- Bind the package phase of the Maven life cycle -->
                        <phase>package</phase>
                        <!-- When the package phase is executed, let it call the plug-in's shade target -->
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>

                            <transformers>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                    <resource>META-INF/spring.handlers</resource>
                                </transformer>
                                <transformer implementation="org.springframework.boot.maven.PropertiesMergingResourceTransformer">
                                    <resource>META-INF/spring.factories</resource>
                                </transformer>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                    <resource>META-INF/spring.schemas</resource>
                                </transformer>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>

                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>com.Application</mainClass>
                                </transformer>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                    <resource>reference.conf</resource>
                                </transformer>
                            </transformers>
                            <createDependencyReducedPom>false</createDependencyReducedPom>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

The effect after installation

Run the packaging command jar

Run the packaging command with spring-boot-maven-plugin

Rendering of the finished package

Open the jar package and check that all dependent jars exist in the form of class files.

View package configuration file

Manifest-Version: 1.0
Created-By: Maven JAR Plugin 3.2.2
Build-Jdk-Spec: 11
Implementation-Title: boundary-XXX
Implementation-Version: 0.0.1-SNAPSHOT
Main-Class: com.XXX.XXX.GoodsApplication

Compare shade and spring-maven-plugin

Package Whether fatjar Can be run directly Dependent package form Pack size
shade class file is slightly smaller, and the self-test is about 2M smaller on average
springboot-maven-plugin jar Slightly larger

The jar package generated by springboot-maven-plugin, the isolation of project classes and dependent jars is relatively clear and intuitive, personally recommended;