SpringBoot部署的jar包瘦身

发布时间 2023-11-10 11:48:46作者: 小尼

pom文件打包插件更换

参考连接:https://www.jb51.net/program/293676eog.htm

参考连接:https://blog.csdn.net/meng_9543/article/details/121329834

<build>
        <finalName>xxx-xxx</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <!--换成你自己项目的启动类(path)-->
                    <mainClass>xxx.xxx.xxx.xxxApplication</mainClass>
                    <layout>ZIP</layout>
                    <includes>
                        <!--排除lib包,nothing任何依赖项都不进行打包-->
                        <include>
                            <groupId>nothing</groupId>
                            <artifactId>nothing</artifactId>
                        </include>
                    </includes>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <!--拷贝依赖到jar外面的lib目录-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <type>jar</type>
                            <includeTypes>jar</includeTypes>
                            <includeScope>runtime</includeScope>
                            <!--指定拷贝依赖项存放的目录位置-->
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                            <overWriteReleases>false</overWriteReleases>
                            <overWriteSnapshots>false</overWriteSnapshots>
                            <overWriteIfNewer>true</overWriteIfNewer>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

启动命令

java -Dloader.path=./lib -jar xxx.jar
# loader.path:  引用的目录(你提取存放依赖的目录位置)