SpringBoot 项目jar 包瘦身部署

在pom中引入依赖

 <build>
        <finalName>poney</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <dependencies>
                    <dependency>
                        <groupId>org.springframework.boot.experimental</groupId>
                        <artifactId>spring-boot-thin-layout</artifactId>
                        <version>1.0.25.RELEASE</version>
                    </dependency>
                </dependencies>
                <configuration>
                    <executable>true</executable>
                    <mainClass>com.sipingsoft.gzxf.PoneyGzxfPortalApplication</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.springframework.boot.experimental</groupId>
                <artifactId>spring-boot-thin-maven-plugin</artifactId>
                <version>1.0.25.RELEASE</version>
                <executions>
                    <execution>
                        <!-- Download the dependencies at build time -->
                        <id>resolve</id>
                        <goals>
                            <goal>resolve</goal>
                        </goals>
                        <inherited>false</inherited>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-antrun-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>compile</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <tasks>
                                <move file="${project.build.directory}/classes/application.yml"
                                      todir="${project.build.directory}/thin/root/config"/>
                                <move file="${project.build.directory}/classes/application-dev.yml"
                                      todir="${project.build.directory}/thin/root/config"/>
                                <move file="${project.build.directory}/classes/application-prod.yml"
                                      todir="${project.build.directory}/thin/root/config"/>
                            </tasks>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

第一次加载依赖需要将下面两个重新下载,以防依赖没有下载成功

  • 将下面框中依赖加入dependencies

  • 刷新maven即可删除

  • 打包之后在thin/root 目录下有3个文件

  • config: 配置文件

  • repository: 所有依赖

  • .jar : 压缩分离的jar包

部署项目

  • 将root目录压缩到服务器任意目录
  • 解压root目录,使用命令行测试jar包是否能成功启动
  • cmd 进入root目录 执行以下命令
java "-Dthin.root=." "-Dthin.offline=true" -jar poney.jar
  • 第一次加载需要等待几秒
  • 如果项目报错显示循环引用,请注释代码中swagger
上一篇 下一篇