一般springboot项目我们不做任何配置的话,打包会包含很多的依赖,生成的包过大,动辄100M以上,往生产环境上传很慢。所以把所有用的jar包打到外部,这样生成的小包中只有自已开发的程序,基本可以控制在1M以内,效果甚好。
首先把springboot自动生成的plugin删掉:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
然后添加以下内容:
<build>
<resources>
<!--打包时添加有用前端资源和配置文件,如resource下还有一些无关紧要的文件,该配置就可以尽量减小打包的jar的大小-->
<resource>
<directory>src/main/resources</directory>
<includes>
<include>static/</include>
<include>templates/</include>
<include>*.properties</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<!--主类: 使用清单文件 -->
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.zhbr.gs_sparklauncher.GsSparkLauncherApplication</mainClass>
<classpathPrefix>lib/</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>
<!-- 将依赖jar包统一拷贝到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>
</plugins>
</build>
打包的效果:
项目部署:
config目录存放配置文件,lib目录存放依赖jar包。部署后的效果如下图所示: