SpringBoot构建jar包与依赖包分离
1.分离原由
SpringBoot项目构建jar部署,通过使用 java -jar xxx.jar 命令启动服务非常方便,但是通过maven构建的jar包含 \BOOT-INF\lib\下的所有依赖jar包,导致jar包文件太大
2.正常构建
pom.xml文件
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
target目录
正常构建target目录
构建后的jar包结构:
|--META-INF
|--BOOT-INF
|--classes
|--lib
|--org
测试是否正常运行
java -jar demo-0.0.1-SNAPSHOT.jar
3.分离构建
pom.xml文件修改
<build>
<plugins>
<!--设置应用 Main 参数启动依赖查找的地址指向外部 lib 文件夹-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>
<!--设置 SpringBoot 打包插件不包含任何 Jar 依赖包-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includes>
<include>
<groupId>nothing</groupId>
<artifactId>nothing</artifactId>
</include>
</includes>
</configuration>
</plugin>
<!--设置将 lib 拷贝到应用 Jar 外面-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
target目录
分离依赖包构建target目录
构建后的jar包结构:
|--META-INF
|--BOOT-INF
|--classes
|--org
测试是否正常运行
java -jar demo-0.0.1-SNAPSHOT.jar
作者:hangzhi
链接:https://www.jianshu.com/p/938e3fc5c140
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。