Spring Boot Jar 包瘦身

Spring Boot Maven About 3,569 words

概念

Uber Jar

Spring Boot单一可执行的文件。直接以java -jar app.jar启动。

Thin Jar

Spring Boot提供了一种打包时的插件,依赖将在打包时通过网络下载。

参考链接:https://github.com/spring-projects-experimental/spring-boot-thin-launcher

Separated Jar

分离式Jar,主jar包仅包含业务逻辑代码,依赖jar同一放在另外文件夹下,通过loader.path加载。

主要配置

spring-boot-maven-plugin

打包成ZIP方式启动(Main-Class: org.springframework.boot.loader.launch.PropertiesLauncher)。

includes中可以指定一些一起打包进去的jar,比如经常变动的jar,或内部更新比较频繁的公共类。

nothing表示不打包任何依赖,只打包手写的代码。

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <layout>ZIP</layout>
        <includes>
            <include>
                <groupId>nothing</groupId>
                <artifactId>nothing</artifactId>
            </include>
        </includes>
    </configuration>
</plugin>

maven-dependency-plugin

将依赖拷贝到target目录下的lib文件夹。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <id>copy</id>
            <phase>package</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
            <configuration>
                <outputDirectory>
                    ${project.build.directory}/lib
                </outputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>

启动

java -Dloader.path=./lib -jar  app.jar

java -jar -Dloader.path=./lib app.jar

说明

Windows系统上的PowerShell中执行启动命令可能会得到一下错误。

Error: Could not find or load main class .path=..lib
Caused by: java.lang.ClassNotFoundException: /path=//lib

解决方法:将-D参数使用双引号括起来。

java -jar "-Dloader.path=./lib" app.jar

完整插件配置

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <annotationProcessorPaths>
                    <path>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok</artifactId>
                    </path>
                </annotationProcessorPaths>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <excludes>
                    <exclude>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok</artifactId>
                    </exclude>
                </excludes>
                <layout>ZIP</layout>
                <includes>
                    <include>
                        <groupId>nothing</groupId>
                        <artifactId>nothing</artifactId>
                    </include>
                </includes>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>copy</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>
                            ${project.build.directory}/lib
                        </outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
Views: 2 · Posted: 2026-01-22

———         Thanks for Reading         ———

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓
Prev Post
Today In History
Browsing Refresh