springboot多模块项目(微服务项目)正确打包(jar)方式 | 您所在的位置:网站首页 › 服务器源码打包了怎么办 › springboot多模块项目(微服务项目)正确打包(jar)方式 |
大致步骤
新建一个springboot项目名称为父亲 添加父快捷方式。新建子模块,子模块同时插入新建springboot的项目,依次创建enty和web模块(关键是并配置好pom文件) web模块依赖于entiy模块中的实体类,创建测试控制器,先测试项目没问题再开始打包(jar) 开始打包 测试jar是否有用 创建项目注意点 :子模块需要保留xx.iml,xx.mvn文件,父模块保留.idea,.mvn文件 。如果删除了这些可能会报发现主类的错误 要打包项目大致的目录结构如下 : 第一级别:father 第二级别:service、web、entiy 第三级别:eduService 其中web、eduService是web项目可以独立运行,且依赖entiy( 学会了这个,以后所有的多模块项目都能学会打包(jar)) 配置父模块注意点一: 修改打包为pom(一般父级的打包方式为pom,所以father、service的打包方式为pom)。 pom配置父模块注意点二: 记得指定该父模块下面有哪些子模块 entiy web service配置父模块注意点三: 记得指定java的版本号 1.8配置父模块注意点四: 只需在father配置apache的maven打包插件,service的其他父模块不需要配置这个 org.apache.maven.plugins maven-compiler-plugin 3.1 ${java.version} ${java.version} org.apache.maven.plugins maven-surefire-plugin 2.19.1 true完整father的pom文件如下: 4.0.0 org.springframework.boot spring-boot-starter-parent 2.4.0 entiy web service com.zzh father 0.0.1-SNAPSHOT pom father 1.8 org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-test test org.apache.maven.plugins maven-compiler-plugin 3.1 ${java.version} ${java.version} org.apache.maven.plugins maven-surefire-plugin 2.19.1 true 配置子模块的pom文件配置子模块注意点一 : 在parent节点下面加上relativePath,作用是指明依赖哪个父模块pom文件(如果是依赖直接父级就是…/规则和写html那种引用绝对路径一样)。 ../pom.xml配置子模块注意点二 : 修改打包方式为jar jar配置子模块注意点三 : 加上springBoot的maven打包插件,并且指定运行的主入口类(springboot的maven插件,用这个插件打包的Jar包可以直接运行,但是不可依赖!),如果此子模块需要被依赖,那么还需加上这句代码(不加会报找不到类的错误)。(entiy模块需要加,其他子模块不要加) 感谢大佬的文章 exec org.springframework.boot spring-boot-maven-plugin exec com.zzh.demo.EntiyApplication ZIP repackage配置子模块注意点四 : 指定以下bulid的编码规则 UTF-8 UTF-8 1.8完整的web模块pom文件 4.0.0 org.springframework.boot spring-boot-starter-parent 2.4.0 ../pom.xml com.zzh web 0.0.1-SNAPSHOT web jar UTF-8 UTF-8 1.8 com.zzh entiy 0.0.1-SNAPSHOT org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin com.zzh.demo.WebApplication ZIP repackage完整的eduService模块pom文件(由于service中配置了springboot的打包插件,由于可以依赖传递,这里可以不用配置打包插件) service com.zzh 0.0.1-SNAPSHOT ../pom.xml 4.0.0 eduService jar eduService UTF-8 UTF-8 1.8 编写测试代码entiy模块 编写entiyTest类,并且自己调用自己的entiyTest public class entiyTest { public void showEntiyTest() { System.out.println("调用showEntiyTest成功!!"); } } @RestController @RequestMapping("/entiy") public class entiyController { @RequestMapping("/test") public String testEntiy() { System.out.println("entiySucess"); return "entiySucess"; } }web模块(前提:我们引入了entiy模块的) @RestController @RequestMapping("/web") public class controller { @RequestMapping("/test") public String testWeb() { entiyTest entiyTest = new entiyTest(); entiyTest.showEntiyTest(); return "webSucess"; } }eduService模块(前提:我们引入了entiy模块的) @RestController @RequestMapping("/eduService") public class controller { @RequestMapping("/test") public String testWeb() { entiyTest entiyTest = new entiyTest(); entiyTest.showEntiyTest(); return "eduService"; } }直接点clean接着点package或者install一键打包就ok了?
在需要对外提供依赖的项目的pom里设置(如本项目的xxx-a、xxx-b),这样设置会让项目生成两个jar:一个可执行jar,一个可依赖的jar; org.springframework.boot spring-boot-maven-plugin exec
关注不迷路,日后分享更多技术干货,B站、CSDN、微信公众号同名,名称都是(小咸鱼的技术窝)更多详情在主页 |
今日新闻 |
推荐新闻 |
专题文章 |
CopyRight 2018-2019 实验室设备网 版权所有 |