Springboot多模块项目搭建打包jar运行打包war运行一个启动类(有的项目是多模块多个启动类),为后续的SpringCloud项目作准备。父工程:father子模块:sun1、sun2、web(启动模块)1、创建父工程 勾选SpringWeb和lombok 如图所示 然后删除src文件夹,如图: 然后修改pom文件,添加一行打包类型packagingpompackaging2、创建子模块 创建3个子模块,分别为sun1、sun2、web,不勾选任何依赖。 3、father的pom。xml修改 创建好三个模块之后,在father项目父工程pom。xml添加依赖modulesmodulesun1modulemodulesun2modulemodulewebmodulemodules 更换pom。xml插件,准备打包使用buildpluginsplugingroupIdorg。apache。maven。pluginsgroupIdmavencompilerpluginartifactIdversion3。1versionconfigurationsource{java。version}sourcetarget{java。version}targetconfigurationpluginplugingroupIdorg。apache。maven。pluginsgroupIdmavensurefirepluginartifactIdversion2。19。1versionconfigurationskipTeststrueskipTests!默认关掉单元测试configurationpluginpluginsbuild4、子模块pom。xml修改 更换继承:sun1、sun2、web三个子模块分别继承father父工程,这样就同样拥有了lombok以及web的依赖parentgroupIdcom。oscgroupIdfatherartifactIdversion0。0。1SNAPSHOTversionparent 删除:因为web是启动类项目,不需要所以相关的打包插件,但是sun1、sun2需要删除下面的,因为是多余的,如下:buildpluginsplugingroupIdorg。springframework。bootgroupIdspringbootmavenpluginartifactIdpluginpluginsbuild 删除:删除共同的配置propertiesjava。version1。8java。versionproperties 添加:打包类型packagingjarpackaging5、编码 sun1项目上创建entity层,这个时候就可以使用lombok插件了,因为继承了父工程 DataAllArgsConstructorToStringpublicclassUserEntity{privateStringname;privateintage;privateStringaddress;} sun2定义为service层,需要使用sun1的实体类,然后依赖sun1,在sun2的pom。xml中配置dependencygroupIdcom。oscgroupIdsun1artifactIdversion0。0。1SNAPSHOTversiondependency 编写service ServicepublicclassHelloService{publicUserEntitygetUser(){returnnewUserEntity(jiutian,23,beijing);}} 这个时候我们就是引用sun1的实体类 最后我们创建web层,这个不需要删除启动类,然后都依赖sun1,sun2,把他们引入到pom。xml文件中,添加sun1,sun2依赖。dependencygroupIdcom。oscgroupIdsun1artifactIdversion0。0。1SNAPSHOTversiondependencydependencygroupIdcom。oscgroupIdsun2artifactIdversion0。0。1SNAPSHOTversiondependency 最后在启动类上添加扫描,因为多个模块,web启动类运行的话默认只运行扫描自己这个模块,导致找不到其他模块,所以添加一个扫描。ComponentScan(basePackages{com。osc。sun2。service,com。osc。web。controller}) 然后web项目上创建一个controller层 RestControllerpublicclassHelloController{AutowiredHelloServicehelloService;GetMapping(valuetest)publicStringtest(){returnhelloService。getUser()。toString();}}6、IDEA运行、测试 7、jar打包 在web项目pom。xml中插件更换为build!打包之后的名字finalNamedemomanyfinalNamepluginsplugingroupIdorg。springframework。bootgroupIdspringbootmavenpluginartifactIdconfigurationmainClasscom。osc。web。WebApplicationmainClasslayoutZIPlayoutconfigurationexecutionsexecutiongoalsgoalrepackagegoalgoals!!可以生成不含依赖包的不可执行Jar包configurationclassifierexecclassifierconfigurationexecutionexecutionspluginpluginsbuild 然后使用IDEA右侧maven,当然,这个maven一定是root的,也就是父工程下面才可以,如下: 然后jar包打包成功,如图: 然后jar包就此成功,然后我们去命令行启动 这个时候启动了,我们继续刚刚访问, 这个和本地测试一样,说明jar包成功。8、war包 将web项目的pom文件packagingwarpackaging 然后删掉指向类配置,剩下的如下pluginsplugingroupIdorg。springframework。bootgroupIdspringbootmavenpluginartifactIdexecutionsexecutiongoalsgoalrepackagegoalgoalsexecutionexecutionspluginplugins 如图: 关掉之前启动的jar命令,用kill9关掉,然后启动war,如图: 接下来,继续测试, 还是之前的结果,说明无论是我们在IDEA上测试,还是通过打包jar或者war,都可以得到相同结果,这就说明我们的多模块项目单启动类配置完成了。 注意:并不是只有web项目才可以写controller层,如果你再创建一个模块,把它依赖引入,然后在启动类上添加扫描位置,其他子模块的controller也是可以执行的。一般情况下我们企业做项目也往往是这样子的,一个模块的controller层用来后台管理系统的,另外一个模块的controller层用来前台系统的。