SoFunction
Updated on 2025-05-19

Springboot project slimming: How to package jar package and lib dependencies separately

Package jar package separately from lib dependencies

Method 1: Complete separation of project and dependency

maven-jar-plugin is responsible for generating jar files (how dependencies are not included in the jar file) and configuring related content for the jar file's files; the maven-dependent-plugin plugin is used to copy the project's runtime dependencies to the specified directory at build time.

When deploying a project, the produced jar file must be in the same directory as the lib dependency package.

<build>
        <!-- Generatedjarname -->
        <finalName>${}</finalName>
        <plugins>
            <plugin>
                <groupId></groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <!-- GeneratedjarDo not include these two files -->
                        <addMavenDescriptor>false</addMavenDescriptor>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <!--Here you need to modify the main startup class of your project-->
                            <mainClass>Your startup classpath</mainClass>
                            <!-- Whether to use a unique version number,control  Version format of classpath;If not added,Reliance may be followed by a timestamp-->
                            <useUniqueVersions>false</useUniqueVersions>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>

            <!--Copy dependencyjarOutsidelibTable of contents-->
            <plugin>
                <groupId></groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-lib</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <!-- 指定rely拷贝的输出Table of contents -->
                            <outputDirectory>${}/lib</outputDirectory>
                            <!-- Transitive dependencies are not ruled out -->
                            <excludeTransitive>false</excludeTransitive>
                            <!-- No dependent version number removal -->
                            <stripVersion>false</stripVersion>
                            <!-- Included only runtime Dependency of scope -->
                            <includeScope>runtime</includeScope>
                            <!-- exclude common and coo rely -->
                            <excludeArtifactIds>common,coo</excludeArtifactIds>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

Method 2: Partial dependencies are entered into the jar file

  • maven-jar-pluginPlugin is used to generate the main JAR file and configure the relevant information in the file;
  • maven-shade-pluginPlugins are used to package some specific dependencies (such as common and coo) into the main JAR file, and are usually used to create a "uber JAR" (i.e., a JAR containing all dependencies);
  • maven-dependency-pluginPlugins are used to copy all runtime dependencies except specified dependencies (such as common and coo) to the lib/ directory during the build process;

Because maven-jar-plugin will generate a dependency-free jar file, it can be deleted if it is not needed. The maven-antrun-plugin plugin is used to perform some additional tasks, such as deleting unwanted JAR files.

When deploying a project, the produced jar file must be in the same directory as the lib dependency package.

<build>
        <plugins>
            <!-- maven-jar-plugin Used to generate the master JAR document,And configure  document指定libdocument -->
            <plugin>
                <groupId></groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <executions>
                    <execution>
                        <id>default-jar</id>
                        <phase>package</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <archive>
                        <!-- Included in the package Maven Descriptor(like  and  document) -->
                        <addMavenDescriptor>true</addMavenDescriptor>
                        <manifest>
                            <!-- exist  Add classpath to -->
                            <addClasspath>true</addClasspath>
                            <!-- The classpath prefix specified by the dependent is lib/ -->
                            <classpathPrefix>lib/</classpathPrefix>
                            <!-- Specify the main startup class -->
                            <mainClass>Your startup classpath</mainClass>
                            <!-- Use a non-unique version(不existrely路径中添加版本号) -->
                            <useUniqueVersions>false</useUniqueVersions>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>

            <!-- maven-shade-plugin Used to define a specified dependency(like common and coo)Package into the main JAR -->
            <plugin>
                <groupId></groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <executions>
                    <execution>
                        <!-- exist package Phase execution -->
                        <phase>package</phase>
                        <goals>
                            <!-- use shade Target -->
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <artifactSet>
                                <!-- 指定需要包含exist主 JAR Dependencies in -->
                                <includes>
                                    <include>:common</include>
                                    <include>:coo</include>
                                </includes>
                            </artifactSet>
                            <!-- Disable the generation of additional  document -->
                            <shadedArtifactAttached>false</shadedArtifactAttached>
                            <!-- Disable generation  document -->
                            <createDependencyReducedPom>false</createDependencyReducedPom>
                            <!-- Customize the final generated JAR Package name -->
                            <finalName>${}</finalName>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <!-- maven-dependency-plugin Used to remove common and coo Copy other dependencies to lib document夹 -->
            <plugin>
                <groupId></groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <!-- Set the executed ID -->
                        <id>copy-lib</id>
                        <!-- exist package Phase execution -->
                        <phase>package</phase>
                        <goals>
                            <!-- use copy-dependencies Target -->
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <!-- Specify the output directory that depends on the copy -->
                            <outputDirectory>${}/lib</outputDirectory>
                            <!-- Transitive dependencies are not ruled out -->
                            <excludeTransitive>false</excludeTransitive>
                            <!-- No dependent version number removal -->
                            <stripVersion>false</stripVersion>
                            <!-- Included only runtime Dependency of scope -->
                            <includeScope>runtime</includeScope>
                            <!-- exclude common and coo rely -->
                            <excludeArtifactIds>common,coo</excludeArtifactIds>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <!-- maven-antrun-plugin Used to remove redundant JAR document -->
            <plugin>
                <groupId></groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <executions>
                    <execution>
                        <!-- exist package Phase execution -->
                        <phase>package</phase>
                        <configuration>
                            <tasks>
                                <!-- Delete by maven-jar-plugin The generated default JAR document -->
                                <delete file="${}/${}-${}.jar" />
                            </tasks>
                        </configuration>
                        <goals>
                            <!-- use run Target -->
                            <goal>run</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

jar file and lib package command

The maven-assembly-plugin configuration is used for the construction process of a Maven project, generating a compressed package (tar or zip format) and outputting it to the specified directory

			<!--maven-assembly-plugin Used to package projects to generate compressed files-->
            <plugin>
                <!-- Specify the plugin to use:maven-assembly-plugin -->
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <!-- Will it beassemblyofID添加到生成包of名称中。Set asfalsehour,Will not be added in the package nameID。 -->
                    <appendAssemblyId>false</appendAssemblyId>

                    <!-- 指定最终生成oftarorzip包of文件名,Here is set asdjys-business。 -->
                    <finalName>build-jar</finalName>

                    <!-- Output directory,生成oftarorzipThe bag will be stored intargetIn the directory。 -->
                    <outputDirectory>target/</outputDirectory>

                    <descriptors>
                        <!-- 指定引用ofassemblyConfiguration File,Quote heresrc/main/resources/ -->
                        <descriptor>src/main/resources/</descriptor>
                    </descriptors>
                </configuration>
                <executions>
                    <execution>
                        <!-- implementID,Can be named at will,Used heremake-assembly。 -->
                        <id>make-assembly</id>

                        <!-- Bind the plugin topackageLife cycle phase,当implementmvn packagehour会调用该插件。 -->
                        <phase>package</phase>

                        <goals>
                            <!-- Set plugin targets,Used heresingleTarget,It creates a single compressed package(tar/zip)。 -->
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

File content

<assembly xmlns="/ASSEMBLY/3.3.0"
          xmlns:xsi="http:///2001/XMLSchema-instance"
          xsi:schemaLocation="/ASSEMBLY/3.3.0 /xsd/assembly-3.3.">

    <id>package</id>
    <formats>
        <format>zip</format>
    </formats>
    <includeBaseDirectory>true</includeBaseDirectory>
    <fileSets>
<!--        <!–Copy the file tojarExternal packageconfigBelow the directory–>-->
<!--        <fileSet>-->
<!--            <directory>${basedir}/src/main/resources</directory>-->
<!--            <includes>-->
<!--                <include>*.yml</include>-->
<!--            </includes>-->
<!--            <filtered>true</filtered>-->
<!--            <outputDirectory>${}config</outputDirectory>-->
<!--        </fileSet>-->

        <!--copylibPacked injarExternal packagelibunder-->
        <fileSet>
            <directory>${}/lib</directory>
            <outputDirectory>${}lib</outputDirectory>
            <!-- Package the files that need to be included -->
            <includes>
                <include>*.jar</include>
            </includes>
        </fileSet>

        <!--If necessary,可以配置多个需要copy的文件即可-->
        <fileSet>
            <directory>${}</directory>
            <outputDirectory>${}</outputDirectory>
            <includes>
                <include>*.jar</include>
            </includes>
        </fileSet>
    </fileSets>
</assembly>

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.