使用dependencyManagement和io.spring.platform管理Maven项目依赖及版本问题
1.io.spring.platform:
在spring的使用过程中,通常要依赖许多第三方库,其中某些依赖版本之间可能会出现不兼容问题(比如说,Spring4要求hibenate版本是hibenate4及以上,如果同时引入Spring4和hibenate3,则会产生其兼容性问题),在不经验证的情况下很容易出现潜在风险,且版本关系难于维护。
Spring IO Platform的产生很好地解决了这些问题,我们在添加第三方依赖的时候,不需要写版本号,其能够自动帮我们挑选一个最优的版本,而且依赖的版本是经过测试的,保证最大限度的扩展的同时可以完美解决依赖的版本兼容性问题。
( 1):如果dependencies里的dependency自己没有声明version元素,那么Maven会沿着父子层次向上走,直到找到一个拥有dependencyManagement元素的项目,然后它就会使用在这个dependencyManagement元素中指定的版本号。
(2):如果dependencies中的dependency声明了version,那么无论dependencyManagement中有无对该jar的version声明,都以dependency里的version为准。
示例:
pom继承关系图:
依赖关系:
spring-parent(pom.xml):
<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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.spring</groupId>
<artifactId>spring-parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.spring.platform</groupId>
<artifactId>platform-bom</artifactId>
<version>Brussels-SR4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
spring-child1(pom.xml):
<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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-child1</artifactId>
<!--继承父类-->
<parent>
<groupId>com.spring</groupId>
<artifactId>spring-child</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<!--依赖关系-->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>
</dependencies>
</project>
这样就可以通过dependencyManagement和io.spring.platform管理Maven项目依赖版本啦,在子模块的依赖中就不需写version版本了。