dependencyManagement和dependencies的不同

前言

随着Spring全家桶越做越大,和其他框架的结合的机会也越来越多,现在做Web基本是起手就是一个Spring。这就会出现版本号冲突的问题,比如新的Spring和旧的Quartz不兼容之类的情况。

Spring framework bom

为了解决这个问题,Spring推出了spring-framework-bomSpring Boot有对应的spring-boot-dependencies
只需要将以下pom.xml加入我们项目的pom.xml,就不用在dependencies标签中写版本号。dependencyManagement会自动帮我们引入版本号。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<project>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-framework-bom</artifactId>
<version>${spring.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
</dependencies>
</project>

我们可以看下spring-framework-bom的源码,可以看到它已经默认为我们填好了版本号。

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
148
149
150
151
<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.springframework</groupId>
<artifactId>spring-framework-bom</artifactId>
<version>4.2.0.RELEASE</version>
<packaging>pom</packaging>
<name>Spring Framework (Bill of Materials)</name>
<description>Spring Framework (Bill of Materials)</description>
<url>
https://github.com/spring-projects/spring-framework
</url>
<organization>
<name>Spring IO</name>
<url>http://projects.spring.io/spring-framework</url>
</organization>
<licenses>
<license>
<name>The Apache Software License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
</license>
</licenses>
<developers>
<developer>
<id>jhoeller</id>
<name>Juergen Hoeller</name>
<email>jhoeller@pivotal.io</email>
</developer>
</developers>
<scm>
<connection>
scm:git:git://github.com/spring-projects/spring-framework
</connection>
<developerConnection>
scm:git:git://github.com/spring-projects/spring-framework
</developerConnection>
<url>
https://github.com/spring-projects/spring-framework
</url>
</scm>
<issueManagement>
<system>Jira</system>
<url>https://jira.springsource.org/browse/SPR</url>
</issueManagement>
<!--===================注意这里===================-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>4.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-instrument</artifactId>
<version>4.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-instrument-tomcat</artifactId>
<version>4.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>4.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-messaging</artifactId>
<version>4.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>4.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc-portlet</artifactId>
<version>4.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-websocket</artifactId>
<version>4.2.0.RELEASE</version>
</dependency>
</dependencies>
</dependencyManagement>
<!--===================注意这里===================-->
</project>

总结

dependencyManagement是一个版本号管理器,官方说法是中央依赖版本管理
dependencyManagement子标签中的dependency声明的版本号,可以被引用了该dependencyManagementpom.xml继承,但是需要在pom.xml中声明dependency而不必声明版本号。
简单地说,就是自己的pom.xml不用再写dependencyManagement中拥有的dependency的版本号了,但是要写dependency名称(这里的名称只是形象的说法)

!!注意!!
dependencyManagement并不会产生继承关系,引用了dependencyManagement的新项目的pom.xml,仍需要手动声明引用的dependency,只是不用再写版本号。

如果需要继承的话,就使用parent标签,这里不赘述。

1
2
3
4
5
<parent>
<artifactId>ahao-all</artifactId>
<groupId>com.ahao.core</groupId>
<version>1.0</version>
</parent>

参考资料

点击进入云乞讨模式!
0%