spring-boot經過maven的依賴管理爲咱們寫好了不少依賴項及其版本,咱們可拿來使用。spring-boot文檔介紹了兩種使用方法,一是繼承,二是導入。html
經過<parent>繼承:spring
<project> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.1.9.RELEASE</version> </parent>
</project>
或者在<dependencyManagement>中導入:tomcat
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>1.1.9.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency>
</dependencies> </dependencyManagement>
此外,在其文檔裏說到,繼承時可簡單地經過屬性定製依賴項版本。好比,改成使用較新的spring-4.1.6.RELEASE版本:安全
<properties> <spring.version>4.1.6.RELEASE<spring.version> </properties>
不過,此法只對繼承有效,導入無效。如下摘自其文檔說明:maven
This only works if your Maven project inherits (directly or indirectly) from spring-boot-dependencies. If you have added spring-boot-dependencies in your own dependencyManagement section with <scope>import</scope> you have to redefine the artifact yourself instead of overriding the property.
這裏說到,如要導入後修改依賴項版本就只能本身從新去定義了。spring-boot
須要注意的是,導入後再從新定義既繁瑣又不安全。好比,spring-boot定義了8個tomcat依賴項,若是你導入後只重定義了部分tomcat依賴項及其版本,則將形成版本不一致的問題。spa
那麼導入時有沒有既簡單又安全的方法呢?咱們可先繼承後導入!code
一、先建一個過渡性工程,繼承後定製依賴項的版本。htm
<project> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>1.1.9.RELEASE</version> </parent> <groupId>mycomp</groupId> <artifactId>myproject-spring-boot-bom</artifactId> <version>1.1.9</version> <packaging>pom</packaging> <properties> <spring.version>4.1.6.RELEASE</spring.version> </properties> </project>
二、而後導入到本身的工程裏。blog
<project> <modelVersion>4.0.0</modelVersion> <groupId>mycomp</groupId> <artifactId>myproject</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencyManagement> <dependencies> <dependency> <groupId>mycomp</groupId> <artifactId>myproject-spring-boot-bom</artifactId> <version>1.1.9</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> </project>
這樣,雖然多建了一個過渡性工程,但定製依賴項版本同繼承時同樣簡單。