项目的目录结构:

Maven多模块管理

一、创建父工程的必须遵循以下两点:

1、packaging标签的文本内容必须设置为pom

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <modelVersion>4.0.0</modelVersion>
 6 
 7     <groupId>com.example</groupId>
 8     <artifactId>maven-parent</artifactId>
 9     <version>1.0.0</version>
10 
11     <packaging>pom</packaging>
12 
13 </project>

2、把父工程中的src目录删除掉

二、创建子工程

1、新建module,勾选模块maven-parent为父模块

Maven多模块管理

2、子模块的pom文件中,parent标签关联了父工程。父工程中新增modules标签,关联了子模块

子模块的pom文件

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <parent>
 6         <artifactId>maven-parent</artifactId>
 7         <groupId>com.example</groupId>
 8         <version>1.0.0</version>
 9     </parent>
10 
11     <modelVersion>4.0.0</modelVersion>
12 
13     <artifactId>maven-child</artifactId>
14 
15 </project>

父工程的pom文件

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <modelVersion>4.0.0</modelVersion>
 6 
 7     <groupId>com.example</groupId>
 8     <artifactId>maven-parent</artifactId>
 9     <version>1.0.0</version>
10 
11     <modules>
12         <module>maven-child</module>
13     </modules>
14 
15     <packaging>pom</packaging>
16 
17 </project>

三、父工程管理依赖

由于子工程是无条件继承父工程的所有依赖,使用<dependencyManagement>标签统一进行依赖管理,<properties>标签管理依赖的版本号。子工程使用声明式方式实现依赖的有需要继承,防止子模块中的依赖冗余。

 1 <!--统一管理依赖的版本号-->
 2     <properties>
 3         <mysql.version>8.0.25</mysql.version>
 4         <junit.version>4.13.2</junit.version>
 5     </properties>
 6 
 7     <!--统一进行依赖管理-->
 8     <dependencyManagement>
 9         <dependencies>
10             <dependency>
11                 <groupId>mysql</groupId>
12                 <artifactId>mysql-connector-java</artifactId>
13                 <version>${mysql.version}</version>
14             </dependency>
15             <dependency>
16                 <groupId>junit</groupId>
17                 <artifactId>junit</artifactId>
18                 <version>${junit.version}</version>
19                 <scope>test</scope>
20             </dependency>
21         </dependencies>
22     </dependencyManagement>

四、子工程按需要声明式继承依赖

<dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
</dependencies>
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。