众所周知,maven可以管理项目依赖,但很多人忽视了maven的另一项重要功能——使用maven搭建聚合工程。
这里使用maven搭建一个springboot的聚合工程作为案例,整合Lombok、Spring MVC、Druid、Mybatis。
案例下载链接:springboot的maven聚合工程
2.直接构建maven工程,作为一个父工程,用于管理依赖版本,同时作为子模块容器
3.由于父工程只作为子模块的容器,因此不需要src目录,直接删掉。(当然,不删除也不影响)
4.在父工程容器下建立子模块
4.1.1创建common模块,common模块用于保存通用的一些组件。同样选择maven工程
4.1.2创建common模块,这里只需要输入模块名,指定版本号,其他不需要修改,同父工程。
4.2.1创建beans模块,用于保存实体类,实现实体类的统一管理。
4.2.2创建beans模块,输入模块名称即可
4.3.1创建mapper模块,实现对DAO层的统一管理,同样是maven工程
4.3.2创建mapper模块,输入模块名称,直接点完成
4.4.1创建service模块,实现对业务逻辑层的统一管理,同样为maven工程
4.4.2创建service模块,输入模块名称,直接点完成
4.5.1创建controller模块,由于此时我们的controller是Spring MVC,需要Springboot对其进行整合,此处就不再使用maven新建模块了,而是使用spring的初始化工具进行构建。
4.5.2创建controller模块,输入模块名称,选择java版本,点下一步
4.5.3创建controller模块,选择我们需要的所有模块。
①第一个选择Lombok,它可以通过注解的方式为我们实现setter、getter、有参无参构造器和toString()
②因为是控制层,需要整合Spring MVC,在这里选择导入Spring Web模块
③导入mybatis和mysql驱动
5.还有很多组件可供选择,这里只导入我们需要的,导入完成后,项目结构效果图:
6.此时我们打开maven可以看到该工程有两个根,那是因为我们的Spring初始化容器默认使controller的pom文件继承了Springboot的pom文件,同时springboot-parent没有将controller关联为子模块。
7.springboot-parent将controller关联为子模块,实现父容器对子模块的统一管理
7.1将controller的父工程(springboot)转为springboot-parent的父工程。也就是把controller的pom文件中的
标签,挪到springboot-parent的pom里,使得整个父工程为springboot工程。
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.5.8version>
<relativePath/>
parent>
7.2再将controller的pom下的依赖,全部挪到springboot-parent的pom下
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>2.2.1version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<scope>runtimescope>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
7.3将springboot-parent作为controller模块的父工程
修改controller模块的pom文件,将springboot-parent作为父工程
<parent>
<artifactId>springboot-parentartifactId>
<groupId>org.examplegroupId>
<version>1.0-SNAPSHOTversion>
parent>
修改springboot-parent的pom文件,将controller模块作为子模块
<modules>
<module>commonmodule>
<module>beansmodule>
<module>mappermodule>
<module>servicemodule>
<module>controllermodule>
modules>
8.此时查看maven,只有一个root根目录了
9.可是展开依赖关系,发现所有的模块都引入了Springboot的依赖,有点冗余,所以需要整理一下依赖关系
9.根据依赖分析,整理依赖
9.0 父工程springboot-parent的pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.4.4version>
<relativePath/>
parent>
<groupId>org.example.springbootgroupId>
<artifactId>springboot-parentartifactId>
<version>1.0-SNAPSHOTversion>
<packaging>pompackaging>
<modules>
<module>commonmodule>
<module>beansmodule>
<module>mappermodule>
<module>servicemodule>
<module>controllermodule>
modules>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>1.18.18version>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
<version>2.4.4version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<version>2.4.4version>
<scope>testscope>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
<scope>testscope>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.47version>
<scope>runtimescope>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druid-spring-boot-starterartifactId>
<version>1.1.10version>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>2.1.4version>
dependency>
dependencies>
dependencyManagement>
project>
9.1 beans模块的pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springboot-parentartifactId>
<groupId>org.example.springbootgroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>beansartifactId>
<version>1.0-SNAPSHOTversion>
<packaging>jarpackaging>
<dependencies>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
dependencies>
project>
9.2 common模块的pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springboot-parentartifactId>
<groupId>org.example.springbootgroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>commonartifactId>
<version>1.0-SNAPSHOTversion>
<packaging>jarpackaging>
<dependencies>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
dependencies>
project>
9.3 mapper模块的pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springboot-parentartifactId>
<groupId>org.example.springbootgroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>mapperartifactId>
<packaging>jarpackaging>
<dependencies>
<dependency>
<groupId>org.example.springbootgroupId>
<artifactId>beansartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<scope>runtimescope>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druid-spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
dependency>
dependencies>
project>
9.4 service模块的pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springboot-parentartifactId>
<groupId>org.example.springbootgroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>serviceartifactId>
<version>1.0-SNAPSHOTversion>
<packaging>jarpackaging>
<dependencies>
<dependency>
<groupId>org.example.springbootgroupId>
<artifactId>commonartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
<dependency>
<groupId>org.example.springbootgroupId>
<artifactId>mapperartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
dependencies>
project>
9.5 controller模块的pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<artifactId>springboot-parentartifactId>
<groupId>org.example.springbootgroupId>
<version>1.0-SNAPSHOTversion>
parent>
<artifactId>controllerartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>controllername>
<description>Demo project for Spring Bootdescription>
<properties>
<java.version>1.8java.version>
properties>
<packaging>jarpackaging>
<dependencies>
<dependency>
<groupId>org.example.springbootgroupId>
<artifactId>serviceartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
<configuration>
<mainClass>org.example.springboot.ControllerApplicationmainClass>
<fork>truefork>
<excludes>
<exclude>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
exclude>
excludes>
configuration>
plugin>
plugins>
build>
project>