结合Winsw基于SpringBoot项目的瘦身部署

在实际业务过程中,我遇到了迭代更新jar包传输的问题,虽然SpringBoot内置插件可以帮我们直接打包成为Jar包运行,但是大多数项目需要部署到公网的服务器或者通过idcvpn跳转到实体服务器上,而这种传输一般都会被限制速度,因为每次打包的完整jar非常大,所以需要进行一次瘦身。

而大多数情况下,我们的项目开发后,依赖的jar基本不会有太大变动,主要变动的是一些业务代码和逻辑,这部分的变更不会影响到依赖jar的变动,于是将数据量较大的jar打包到外部公共目录,每次更新迭代只需要上传小的业务代码形成的jar包的可能也就得以实现。

由于大多数的介绍都是直接用cmd启动SpringBoot的,我们这里介绍一下用了Winsw注册服务后该怎么配合进行瘦身部署。

软件界面

一般测试运行成功后,我们会点击上面的"package"来打包生成整个项目的jar包,也即下面这个:

显然我们可以看到整个项目的jar包文件还是比较大的,这对于我们需要急需发布相关迭代版本来说是比较麻烦的。

瘦身方式

首先对整个项目打包生成的jar文件进行解压,用任意的压缩工具都可以。

解压后我们进入看看目录

可以看到大部分项目的数据都在这个BOOT-INF里面的lib目录下,把这个lib目录提取出来,复制到其他文件夹,待用。

修改项目pom

打开pom文件,将原本的打包插件

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

修改为:

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <!--换成你自己项目的启动类(path)-->
                    <mainClass>x.xx.xxx.xxxxxxApplication</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>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <!--外部jar位置,这里表示从同级目录的lib包下加载-->
                            <classpathPrefix>./lib</classpathPrefix>
                            <mainClass>x.xx.xxx.xxxxxxApplication</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>

这样就可以了,重新打包,我们可以看到现在生成的项目文件大小为:

直接降低到不到1MB左右,这样我们可以很快把这些东西上传!

服务器部署

其实到这一步已经可以完成了,仅需把lib目录和重新生成的jar包上传到服务器上,即可通过命令行的方式来启动了

命令行:

java -Dloader.path=lib -jar MatchStudent-0.0.1-SNAPSHOT.jar

但是由于有些时候,我们宁愿注册成为服务去运行,这样的话该应用程序不会一直显示在前台占用windowsserver的窗口,于是使用了winsw的方法。

winsw仅需下载最新的exe包,并且创建xml文件,仅需配置即可,这里就不详细说明,主要说一下这个瘦身后需要xml文件调整的配置项。

也即对上面的XsMatch.xml需要改为:

<service>
  <!-- ID of the service. It should be unique across the Windows system-->
  <id>XsMatch</id>
  <!-- Display name of the service -->
  <name>XsMatch</name>
  <!-- Service description -->
  <description>This service is a service created from a minimal configuration</description>
  <executable>C:/remove/jdk-21/bin/java.exe</executable>
  <arguments>-Dloader.path=lib -jar MatchStudent-0.0.1-SNAPSHOT.jar</arguments>
  <!-- Path to the executable, which should be started -->
</service>

主要是arguments的配置,需要配置的情况如上。更改后注册服务和启动服务即可

XsMatch.exe install
net start XsMatch
Last modification:June 28th, 2024 at 08:39 am