前言

maven多模块管理其实就是让其子模块的pom文件继承父工程的pom文件。

maven父工程必须遵循以下两点:

  1. packageing 标签的文本内容必须设置为pom
  2. src 目录删除

多模块管理

首先创建一个maven父工程(包名为maven),删除src目录,只有pom文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?xml version="1.0" encoding="UTF-8"?>
<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.0</modelVersion>

<groupId>org.example</groupId>
<artifactId>maven</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>child</module>
</modules>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>

</project>

图片

然后创建一个新的子模块(包名为child),New–>Module;

创建项目时,指定父模块(Parent),GroupId和父工程保持一致即可:

图片

创建完成后,可以看到子模块的pom文件如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?xml version="1.0" encoding="UTF-8"?>
<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">
<!-- 指向父工程的GAV坐标-->
<parent>
<artifactId>maven</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>child</artifactId>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>

</project>

图片

子模块成为父工程

同理,删除src路径,将package标签内容设为pom

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?xml version="1.0" encoding="UTF-8"?>
<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">
<!-- 指向父工程的GAV坐标-->
<parent>
<artifactId>maven</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>child</artifactId>

<packaging>pom</packaging>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>

</project>

创建一个(child模块的)子项目(包名为child-web),创建项目时,指定父模块(Parent),GroupId和父工程保持一致即可:

图片

此时可以看到child模块的pom多了一个modules 标签:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?xml version="1.0" encoding="UTF-8"?>
<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">
<!-- 指向父工程的GAV坐标-->
<parent>
<artifactId>maven</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>child</artifactId>

<packaging>pom</packaging>
<!-- 父工程包含的所有子模块-->
<modules>
<module>child-web</module>
</modules>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>

</project>

子模块继承父模块依赖

在父模块中添加一个依赖:

1
2
3
4
5
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>

可以看到所有子模块及子模块的子模块都会拥有此依赖:

图片

父工程管理子模块依赖

由于可能出现部分子模块不需要引入父工程依赖的情况,所以需要在父工程中进行管理。

使用dependencyManagement标签加强管理子模块所有依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
<!--    父模块加强管理子模块依赖-->
<dependencyManagement>
<dependencies>
<!-- 在父工程添加依赖,所以子模块都会继承此依赖-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<!-- scope标签用于标识maven依赖,test表示当前依赖仅参与测试环境,不参与打包-->
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>

图片

当子工程需要父工程的某个依赖时,只需要声明此依赖即可,无需指定版本,示例(child模块)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?xml version="1.0" encoding="UTF-8"?>
<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">
<!-- 指向父工程的GAV坐标-->
<parent>
<artifactId>maven</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>child</artifactId>

<packaging>pom</packaging>
<!-- 父工程包含的所有子模块-->
<modules>
<module>child-web</module>
</modules>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!-- 声明式依赖-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<!--也可以选择不用父工程的版本
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
<version>4.11</version>
</dependency>
-->
</dependencies>

</project>

图片

优化

同样也可以在父项目(maven)中这样指明版本:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?xml version="1.0" encoding="UTF-8"?>
<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.0</modelVersion>

<groupId>org.example</groupId>
<artifactId>maven</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<!-- 父工程包含的所有子模块-->
<modules>
<module>child</module>
<module>maven-java</module>
</modules>

<!-- 父工程管理依赖的版本号-->
<properties>
<!-- maven项目源文件的版本-->
<maven.compiler.source>8</maven.compiler.source>
<!-- maven项目编译时的版本-->
<maven.compiler.target>8</maven.compiler.target>

<!-- 父工程管理的版本标签名称支持自定义
通常为:项目名称+字段veersion-->
<junit-version>4.13.2</junit-version>
<!-- 也可以用下面这种写法:-->
<!-- <junit.version>4.13.2</junit.version>-->

<mysql-connector-java-version>5.1.47</mysql-connector-java-version>
</properties>

<!-- 父模块加强管理子模块依赖-->
<dependencyManagement>
<dependencies>
<!-- 在父工程添加依赖,所以子模块都会继承此依赖-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<!-- 将版本号替换为自定义的标签-->
<version>${junit-version}</version>
<!-- scope标签用于标识maven依赖,test表示当前依赖仅参与测试环境,不参与打包-->
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-connector-java-version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>

图片

父工程管理子模块插件

和管理依赖同理,不在赘述。

在父模块的pom中管理插件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?xml version="1.0" encoding="UTF-8"?>
<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.0</modelVersion>

<groupId>org.example</groupId>
<artifactId>maven</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<!-- 父工程包含的所有子模块-->
<modules>
<module>child</module>
<module>maven-java</module>
</modules>

<!-- 父工程管理依赖的版本号-->
<properties>
<!-- maven项目源文件的版本-->
<maven.compiler.source>8</maven.compiler.source>
<!-- maven项目编译时的版本-->
<maven.compiler.target>8</maven.compiler.target>

<!-- 父工程管理的版本标签名称支持自定义
通常为:项目名称+字段veersion-->
<junit-version>4.13.2</junit-version>
<!-- 也可以用下面这种写法:-->
<!-- <junit.version>4.13.2</junit.version>-->

<mysql-connector-java-version>5.1.47</mysql-connector-java-version>


</properties>
<!-- 父模块加强管理子模块依赖-->
<dependencyManagement>
<dependencies>
<!-- 在父工程添加依赖,所以子模块都会继承此依赖-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<!-- 将版本号替换为自定义的标签-->
<version>${junit-version}</version>
<!-- scope标签用于标识maven依赖,test表示当前依赖仅参与测试环境,不参与打包-->
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-connector-java-version}</version>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<finalName>child-web</finalName>
<!-- 和dependencyManagement同理-->
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>


</project>

在子模块中(child)引用继承插件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?xml version="1.0" encoding="UTF-8"?>
<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">
<!-- 指向父工程的GAV坐标-->
<parent>
<artifactId>maven</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>child</artifactId>

<packaging>pom</packaging>

<modules>
<module>child-web</module>
</modules>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!-- 声明式依赖-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 声明式插件-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>