这篇文章主要介绍了SpringBoot+Maven 多模块项目的构建、运行、打包实战,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
本篇文章主要介绍了SpringBoot+Maven 多模块项目的构建、运行、打包,分享给大家,具体如下:
项目使用的工具:
- IntelliJ IDEA
- JDK 1.8
- apache-maven-3.3.9
项目的目录:
- 主项目 springboot-multi
- 子模块 entity、dao、service、web
一、使用IDEA创建一个SpringBoot项目 : File -> new -> Project 项目名称为springboot-multi
二、删除项目中的src目录,把pom.xml中的项目打包方式改为pom,如下:
com.examplespringboot-multi0.0.1-SNAPSHOT<!-- 此处改为pom -->pom
三、创建springboot-multi项目的子模块,在项目上右键单击,选择:new -> Module。
四、创建四个子模块后,删除子模块中 src/main/java、src/main/java下的所有文件(如果没有文件跳过此操作),只保留web子模块的Sp来源gaodaimacom搞#^代%!码&网ringBoot的Application主启动类。
五、主项目pom.xml (注意标签是否指定了子模块)
4.0.0com.examplespringboot-multi0.0.1-SNAPSHOT<!-- 此处改为pom -->pomspringboot-multiDemo project for Spring Boot webservicedaoentity org.springframework.bootspring-boot-starter-parent1.5.10.RELEASE<!-- lookup parent from repository --> UTF-8UTF-81.8 org.springframework.bootspring-boot-starter-web<!--指定使用maven打包--> org.apache.maven.pluginsmaven-compiler-plugin3.1 ${java.version}${java.version} org.apache.maven.pluginsmaven-surefire-plugin2.19.1 true<!--默认关掉单元测试 -->
六、web子模块pom.xml(依赖service、dao、entity子模块)
4.0.0com.exampleweb0.0.1-SNAPSHOTjarwebDemo project for Spring Boot com.examplespringboot-multi0.0.1-SNAPSHOT../pom.xml com.exampleservice0.0.1-SNAPSHOT com.exampledao0.0.1-SNAPSHOT com.exampleentity0.0.1-SNAPSHOT<!--spring boot打包的话需要指定一个唯一的入门--> org.springframework.bootspring-boot-maven-plugin <!-- 指定该Main Class为全局的唯一入口 -->com.example.WebApplicationZIP repackage<!--可以把依赖的包都打包到生成的Jar包中-->
七、service子模块pom.xml(依赖 dao 、entity子模块)
4.0.0com.exampleservice0.0.1-SNAPSHOTjarserviceDemo project for Spring Boot com.examplespringboot-multi0.0.1-SNAPSHOT../pom.xml com.exampledao0.0.1-SNAPSHOT com.exampleentity0.0.1-SNAPSHOT
八、dao子模块pom.xml (依赖entity子模块)
4.0.0com.exampledao0.0.1-SNAPSHOTjardaoDemo project for Spring Boot com.examplespringboot-multi0.0.1-SNAPSHOT../pom.xml com.exampleentity0.0.1-SNAPSHOT
九、entity子模块
4.0.0com.exampleentity0.0.1-SNAPSHOTjarentityDemo project for Spring Boot com.examplespringboot-multi0.0.1-SNAPSHOT../pom.xml
十、pom.xml文件中需要注意的就是:
- 主项目的modules标签指定的子模块是否正确
- 子模块之间的依赖
- 子模块的parent标签
十一、web子模块的Application启动类:
@RestController @SpringBootApplication public class WebApplication { public static void main(String[] args) { SpringApplication.run(WebApplication.class, args); } @RequestMapping(value = "/test",method = RequestMethod.GET) public String test(){ return "test success"; } }
十二、执行main方法启动项目,访问localhost:8080/test,出现如下页面表示项目搭建成功:
十三、项目打包命令: mvn clean package 或者 使用右边工具栏的图形化界面打包也可以:
十四、打包成功日志:
以上就是SpringBoot+Maven 多模块项目的构建、运行、打包实战的详细内容,更多请关注gaodaima搞代码网其它相关文章!