Maven的继承和聚合
Maven的继承和聚合
1. 继承
-
应用场景
项目分为多个模块,重复的依赖比较多,需要提出来公有的依赖来统一管理。
-
使用方法
子模块中引入父模块:
<parent> <groupId>com.xsyz</groupId> <artifactId>springboot-review</artifactId> <version>0.0.1-SNAPSHOT</version> <relativePath>../pom.xml</relativePath> <!-- 父模块相对路径,如果不写会自动查找 --> </parent>
-
两种方式
-
父项目统一管理版本号,子项目需要显式说明自己的依赖(父项目加上dependencyManagement)
父模块:
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <version>2.4.1</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </dependencyManagement>
子模块:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> </dependencies>
子模块依赖:
-
全部继承父项目的依赖(父项目不加dependencyManagement)
父模块:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <version>2.4.1</version> <scope>test</scope> </dependency> </dependencies>
子模块:
<dependencies> </dependencies>
子模块依赖:
-
2. 聚合
-
应用场景
可以帮助我们把项目的多个模块聚合在一起,使用一条命令进行构建,即一条命令实现构建多个项目
-
使用方法
- 构建聚合模块:聚合模块的坐标GAV后面需要声明打包形式为pom
<groupId>com.xsyz</groupId> <artifactId>springboot-review</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging>
-
声明需要聚合的子模块:
<modules> <module>review-aop</module> <module>review-jwt</module> </modules>
-
在聚合模块下打包,打包的时候就会先打包聚合模块,然后自动打包子模块
聚合和继承经常一起使用