常見的spring boot打包方式分兩種, war和fat jar, 這裏主要針對fat jar在部署中遇到的jar包過大問題,jar包過大導致在每次升級時需要很長時間上傳jar包,尤其是網速受限時,問題尤其突出,那麼主要思想是將不常變化的依賴包單獨部署到lib文件夾,每次只上傳常變的項目代碼。
雖然在JHipster項目中並沒有分離依賴,但是我們在spring-swagger2markup-demo中找到方法,下面介紹一下具體做法。
第一步,利用maven-jar-plugin代替原有生成fat jar的插件spring-boot-maven-plugin,例如:
<!-- specify the main class for the manifest -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.Application</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
第二步,利用maven-dependency-plugin將所有依賴拷貝到lib文件夾,例如:
<!-- copy dependencies to the lib directory -->
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
部署方式上和fat jar沒有任何區別。
Good Luck,
Cheers!