maven版本管理

只有一两个项目的情况下,模块版本管理还算容易,但是当模块多了之后,那么就复杂的让人抓狂了

例子

module-a – 1.0
– 1.1

module-b – 1.0 dependon module-a_1.0

module-c – 1.0 dependon module-a_1.1
dependon module-b_1.0

那么, module-c中的module-a使用的是哪个版本?


maven顺序扫描pom文件,对于同一个module,会使用最先扫描到的版本号作为最终使用的版本号
所以,在上例中,答案是不确定的,依赖于pom文件中依赖的顺序

解决方案

方案一:

dependencyManagement 的使用

1
2
3
4
5
6
7
8
9
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.simpletour.common</groupId>
<artifactId>common-core</artifactId>
<version>${simpletour.common.version}</version>
</dependency>
<dependencies>
<dependencyManagement>

通过dependencyManagement可以指定所有模块的版本号,在此处即可唯一确定一个模块的最终版本号是多少

问题:
当存在多个类似C这样的模块时,在每一个pom文件中都需要写dependencyManagement,太过麻烦
并且当存在更多模块,更复杂的引用的情况下(比如间接引用), 则很难完整的写出dependencyManagement

方案二:

依然采用dependencyManagement, 只不过将所有模块的版本全部写在一个名为 类似 versionManager的模块当中
该模块仅包含一个或多个pom文件,然后其他模块全部作为 versionManager的子模块
前提:在同一开发时期,大项目依赖的其他模块时相同的

1
2
3
4
5
<parent>
<artifactId>simpletour-version-manager</artifactId>
<groupId>com.simpletour</groupId>
<version>1.0-SNAPSHOT</version>
</parent>

这样,在方案一中存在的两个问题都可以得到解决.

一个例子

只需要看下结构就好了,具体的dependency不需要看

versionManager pom.xml

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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?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>com.simpletour</groupId>
<artifactId>simpletour-version-manager</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>

<properties>
<!--versions-->
<common.version>1.1-SNAPSHOT</common.version>
<complex.version>2.0-SNAPSHOT</complex.version>

</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.simpletour</groupId>
<artifactId>simpletour-transport-domain</artifactId>
<version>${complex.version}</version>
</dependency>
<dependency>
<groupId>com.simpletour</groupId>
<artifactId>simpletour-transport-dao</artifactId>
<version>${complex.version}</version>
</dependency>
<dependency>
<groupId>com.simpletour</groupId>
<artifactId>simpletour-transport-biz</artifactId>
<version>${complex.version}</version>
</dependency>
<dependency>
<groupId>com.simpletour</groupId>
<artifactId>simpletour-transport-service</artifactId>
<version>${complex.version}</version>
</dependency>

<dependency>
<groupId>com.simpletour</groupId>
<artifactId>simpletour-product-domain</artifactId>
<version>${complex.version}</version>
</dependency>
<dependency>
<groupId>com.simpletour</groupId>
<artifactId>simpletour-product-dao</artifactId>
<version>${complex.version}</version>
</dependency>
<dependency>
<groupId>com.simpletour</groupId>
<artifactId>simpletour-product-biz</artifactId>
<version>${complex.version}</version>
</dependency>
<dependency>
<groupId>com.simpletour</groupId>
<artifactId>simpletour-product-service</artifactId>
<version>${complex.version}</version>
</dependency>

<dependency>
<groupId>com.simpletour</groupId>
<artifactId>simpletour-order-domain</artifactId>
<version>${complex.version}</version>
</dependency>
<dependency>
<groupId>com.simpletour</groupId>
<artifactId>simpletour-order-dao</artifactId>
<version>${complex.version}</version>
</dependency>
<dependency>
<groupId>com.simpletour</groupId>
<artifactId>simpletour-order-biz</artifactId>
<version>${complex.version}</version>
</dependency>
<dependency>
<groupId>com.simpletour</groupId>
<artifactId>simpletour-order-service</artifactId>
<version>${complex.version}</version>
</dependency>

<dependency>
<groupId>com.simpletour</groupId>
<artifactId>simpletour-resources-domain</artifactId>
<version>${complex.version}</version>
</dependency>
<dependency>
<groupId>com.simpletour</groupId>
<artifactId>simpletour-resources-dao</artifactId>
<version>${complex.version}</version>
</dependency>
<dependency>
<groupId>com.simpletour</groupId>
<artifactId>simpletour-resources-biz</artifactId>
<version>${complex.version}</version>
</dependency>
<dependency>
<groupId>com.simpletour</groupId>
<artifactId>simpletour-resources-service</artifactId>
<version>${complex.version}</version>
</dependency>

<dependency>
<groupId>com.simpletour</groupId>
<artifactId>simpletour-inventory-domain</artifactId>
<version>${complex.version}</version>
</dependency>
<dependency>
<groupId>com.simpletour</groupId>
<artifactId>simpletour-inventory-dao</artifactId>
<version>${complex.version}</version>
</dependency>
<dependency>
<groupId>com.simpletour</groupId>
<artifactId>simpletour-inventory-biz</artifactId>
<version>${complex.version}</version>
</dependency>
<dependency>
<groupId>com.simpletour</groupId>
<artifactId>simpletour-inventory-service</artifactId>
<version>${complex.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

<repositories>
<repository>
<id>nexus</id>
<name>nexus</name>
<url>http://192.168.2.231/nexus/content/groups/public/</url>
</repository>
</repositories>
<distributionManagement>
<repository>
<id>releases</id>
<name>Internal Releases</name>
<url>http://192.168.2.231/nexus/content/repositories/releases/</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<name>Internal Snapshots</name>
<url>http://192.168.2.231/nexus/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>
</project>

submodule pom.xml

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
<?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>

<parent>
<artifactId>simpletour-version-manager</artifactId>
<groupId>com.simpletour</groupId>
<version>1.0-SNAPSHOT</version>
</parent>

<groupId>com.simpletour</groupId>
<artifactId>simpletour-portal</artifactId>
<version>2.1.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>Web-Amin-Server</name>

<dependencies>
<dependency>
<groupId>com.simpletour</groupId>
<artifactId>simpletour-version-manager</artifactId>
</dependency>
<dependency>
<groupId>com.simpletour</groupId>
<artifactId>simpletour-transport-service</artifactId>
</dependency>
<dependency>
<groupId>com.simpletour</groupId>
<artifactId>simpletour-inventory-service</artifactId>
</dependency>
<dependency>
<groupId>com.simpletour</groupId>
<artifactId>simpletour-product-service</artifactId>
</dependency>
<dependency>
<groupId>com.simpletour</groupId>
<artifactId>simpletour-order-service</artifactId>
</dependency>
<dependency>
<groupId>com.simpletour</groupId>
<artifactId>simpletour-assistant-service</artifactId>
</dependency>
</dependencies>
</project>

当存在一个新的submodule时,只需要像submodule的pom文件一样,仅指定需要依赖的模块,而不需要再指定版本号

文章目录
  1. 1. 例子
  2. 2. 解决方案
    1. 2.0.1. 方案一:
    2. 2.0.2. 方案二:
  • 3. 一个例子
  • ,