SpringBoot分离打Jar包的两种方式
方式一:基于maven-jar-plugin
此方式基于这个小伙伴的配置改的:https://www.jb51.net/article/188606.htm
注意
- 这种方式打包出来的Jar基于插件提供的类加载器启动:
org.springframework.boot.loader.PropertiesLauncher
- 所有依赖包(包括systemScope),会通过插件
maven-dependency-plugin
自动复制到lib
目录 - 所有资源文件,会通过插件
maven-resources-plugin
自动复制到config
目录 - 此方式打包后,需要指定参数启动
-Dloader.path=lib路径,config路径
- 打包完后部署需要的文件清单:(在
target/
目录下都可以看到)config/**
:所有resources下的资源文件lib/**
:所有lib包,包括本地依赖xxx.jar
:应用Jar
- 运行:
java -Dloader.path=lib,config -Dspring.profiles.active=dev -jar main.jar
简略版配置
lib config xxx org.jeecg.JeecgSystemApplication ${final-name} org.apache.maven.plugins maven-compiler-plugin 1.8 UTF-8 org.apache.maven.plugins maven-surefire-plugin true org.springframework.boot spring-boot-maven-plugin true org.apache.maven.plugins maven-jar-plugin true ${lib-path}/ false ${main-class} ./${config-path}/ lib/zwdd-1.2.0.jar lib/spire-10.jar ${project.build.directory} org.apache.maven.plugins maven-dependency-plugin copy-dependencies package copy-dependencies ${project.build.directory}/${lib-path}/ org.apache.maven.plugins maven-resources-plugin copy-resources package copy-resources false src/main/resources **/* true src/main/resources *.yml ${project.build.directory}/${config-path} src/main/java false **/*.xml **/*.json **/*.ftl src/main/resources false **/* src/main/resources true *.yml
完整配置(带部分注释)
org.jeecgframework.boot jeecg-boot-parent 2.4.0 4.0.0 jeecg-boot-module-system aliyun aliyun Repository http://maven.aliyun.com/nexus/content/groups/public false com.spire spire 10 system ${project.basedir}/../lib/Spire.Doc.jar com.zwdd.api zwdd 1.2.0 system ${project.basedir}/../lib/zwdd-sdk-java-1.2.0.jar lib jar config xxx org.jeecg.JeecgSystemApplication ${final-name} org.apache.maven.plugins maven-compiler-plugin 1.8 UTF-8 org.apache.maven.plugins maven-surefire-plugin true org.springframework.boot spring-boot-maven-plugin repackage true ${main-class} true true ${profile.name} ${project.build.directory}/${jar-path} org.apache.maven.plugins maven-jar-plugin true ${lib-path}/ false ${main-class} ./${config-path}/ lib/zwdd-1.2.0.jar lib/spire-10.jar ${project.build.directory} org.apache.maven.plugins maven-dependency-plugin copy-dependencies package copy-dependencies ${project.build.directory}/${lib-path}/ org.apache.maven.plugins maven-resources-plugin copy-resources package copy-resources false src/main/resources **/* true src/main/resources *.yml ${project.build.directory}/${config-path} src/main/java false **/*.xml **/*.json **/*.ftl src/main/resources false **/* src/main/resources true *.yml
方式二:基于spring-boot-maven-plugin
注意
- 这种方式打包出来的Jar基于插件提供的类加载器启动:
org.springframework.boot.loader.PropertiesLauncher
- 所有依赖包(包括systemScope),会通过插件
maven-dependency-plugin
自动复制到lib
目录 - 所有资源文件,会通过插件
maven-resources-plugin
自动复制到config
目录 - 此方式打包后,需要指定参数启动
-Dloader.path=lib路径,config路径
- 打包完后部署需要的文件清单:(在
target/
目录下都可以看到)config/**
:所有resources下的资源文件lib/**
:所有lib包,包括本地依赖xxx.jar
:应用Jar
- 运行:
java -Dloader.path=lib,config -Dspring.profiles.active=dev -jar main.jar
配置参考
main org.apache.maven.plugins maven-compiler-plugin 1.8 UTF-8 org.mapstruct mapstruct-processor 1.4.1.Final org.projectlombok lombok 1.18.12 org.springframework.boot spring-boot-maven-plugin org.jeecg.JeecgSystemApplication false false ZIP nothing nothing repackage org.apache.maven.plugins maven-dependency-plugin copy-dependencies package copy-dependencies ${project.build.directory}/lib/ org.apache.maven.plugins maven-resources-plugin copy-resources package copy-resources false src/main/resources **/* src/main/resources true *.xml *.yml *.properties ${project.build.directory}/config src/main/java false **/*.xml **/*.json **/*.ftl src/main/resources true *.yml *.txt
附录:参考链接
- SpringBoot项目分离打包
- maven-jar-plugin插件对scope="system"依赖的处理
- https://docs.spring.io/spring-boot/docs/current/maven-plugin/reference/htmlsingle/
- https://www.kancloud.cn/zhangdaiscott/jeecg-boot/3043463
- Springboot jar包外指定配置文件及原理
- Spring Boot之application.properites的failed to convert java.lang.String to java.lang.Integer问题解决
- Maven maven-dependency-plugin包含本地依赖包
- Spring Boot 分离资源文件打包
- https://blog.csdn.net/weixin_40461281/article/details/115905734
- https://www.jianshu.com/p/0277b6a17892
到此这篇关于SpringBoot分离打Jar包的两种方式的文章就介绍到这了,更多相关SpringBoot打Jar包内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!