前言
随着Spring
全家桶越做越大,和其他框架的结合的机会也越来越多,现在做Web
基本是起手就是一个Spring
。这就会出现版本号冲突的问题,比如新的Spring
和旧的Quartz
不兼容之类的情况。
Spring framework bom
为了解决这个问题,Spring
推出了spring-framework-bom
。Spring 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
声明的版本号,可以被引用了该dependencyManagement
的pom.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>
|
参考资料