版本衝突現狀
在使用Spring的時候,經常會使用到第三方庫,一般大家都是根據經驗挑選一個版本號或挑選最新的,隨意性較大,其實這是有問題的,除非做過完整的測試,保證集成該版本的依賴不會出現問題,且後續集成其它第三方庫的時候也不會出現問題,否則風險較大,且後續擴展會越來越困難,因爲隨着業務複雜度的增加,集成的第三方組件會越來會多,依賴之間的關聯也會也來越複雜。
好消息是,Spring IO Platform能很好地解決這些問題,我們在添加第三方依賴的時候,不需要寫版本號,它能夠自動幫我們挑選一個最優的版本,保證最大限度的擴展,而且該版本的依賴是經過測試的,可以完美的與其它組件結合使用。
什麼是Spring IO Platform
Spring IO platform 包括 Foundation Layer modules和Execution Layer domain-specific runtimes(DSR)。
- Foundation Layer modules:代表了核心Spring模塊和相關的第三方依賴關係,這些模塊和依賴關係已經得到協調,以確保開發過程的順利進行。
- Execution Layer: 提供的DSR極大地簡化了構建基於JVM的生產準備工作負載。
簡而言之,Spring IO Platform,簡單的可以認爲是一個依賴維護平臺,該平臺將相關依賴匯聚到一起,針對每個依賴,都提供了一個版本號;這些版本對應的依賴都是經過測試的,可以保證一起正常使用。
Spring IO Platform中維護了哪些依賴
完整的依賴列表:http://docs.spring.io/platform/docs/current/reference/html/appendix-dependency-versions.html
Spring 相關BOM
當然SpringSource爲了解決這些Jar衝突,推出了各種BOM,當然最著名的就是spring platform io bom,其中最核心的三個是:spring-framework-bom、spring-boot-dependencies、platform-bom。
對於Spring工程來說,直接在pom.xml文件中添加如下配置代碼,即可免去管理版本衝突的難題。
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-framework-bom</artifactId>
<version>4.2.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.3.0.M2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>io.spring.platform</groupId>
<artifactId>platform-bom</artifactId>
<version>1.1.3.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
在Maven中使用Spring IO Platform
有兩種方式,一種是使用import導入,另一種是繼承parent。
import導入:將Platform的bom導入到應用程序的pom
<?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.example</groupId>
<artifactId>your-application</artifactId>
<version>1.0.0-SNAPSHOT</version>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.spring.platform</groupId>
<artifactId>platform-bom</artifactId>
<version>Brussels-SR6</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- Dependency declarations -->
</project>
繼承parent:將它用作pom的父項,而不是導入平臺的bom:
<?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.example</groupId>
<artifactId>your-application</artifactId>
<version>1.0.0-SNAPSHOT</version>
<parent>
<groupId>io.spring.platform</groupId>
<artifactId>platform-bom</artifactId>
<version>Brussels-SR6</version>
<relativePath/>
</parent>
<!-- Dependency declarations -->
</project>
使用繼承的話,除了從父pom中引入Spring IO Platform之外,我們的應用還會引入一些插件管理的配置,如Spring Boot的Maven插件,我們可以利用這一點,然後只需要在<plugins>代碼塊中添加如下代碼即可使用插件:
<build>
<plugins>
<plugin>
<groupId> org.springframework.boot </ groupId>
<artifactId> spring-boot-maven-plugin </ artifactId>
</ plugin>
</ plugins>
</ build>
另外,使用繼承的話,還可以重寫Maven中的屬性,你可以用你<properties>想要的值在你的pom的部分聲明屬性。
如下所示--覆蓋父類提供的依賴版本號:
<properties>
<foo.version> 1.1.0.RELEASE </foo.version>
</ properties>
如果你想一起使用Platform和Spring Boot,你不用必須使用Platform的pom作爲父級(參考:http://blog.csdn.net/fly910905/article/details/79420326)。
相反,您可以按照上面所述導入平臺的pom(import方式),然後手動執行其餘配置。Spring Boot的Maven使用它的文檔將告訴你如何。
無論您選擇哪種方法,都不會在您的應用程序中添加任何依賴項。但是,如果確實聲明瞭對平臺中某些內容的依賴關係,則現在可以省略版本號。例如:
<dependencies>
<dependency>
<groupId> org.springframework </ groupId>
<artifactId> spring-core </ artifactId>
</ dependency>
</ dependencies>
Spring IO Platform應用示例:
<?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.example</groupId>
<artifactId>helloworld</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.spring.platform</groupId>
<artifactId>platform-bom</artifactId>
<version>Athens-SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.4.3.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- Additional lines to be added here... -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.4.3.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
有幾點注意,這裏我們沒有繼承Spring Boot的父Pom,也沒繼承Spring IO Platform的父POM,都是選擇導入的方式,所以使用spring-boot-maven-plugin插件的時候,就不能像上面那樣自動繼承父POM的配置了,需要自己添加配置,綁定repackage Goal;
另外,當你想要修改依賴版本號的時候,由於不是繼承,所以不能使用直接覆蓋properties屬性的方法,其實也很簡單,如果不想繼承Spring IO Platform中的依賴版本號的話,自己直接寫上版本號即可,Spring Boot的話,可採用如下方式,來對Spring Data release train進行升級(注意要放在spring-boot-dependencies的前面):
<dependencyManagement>
<dependencies>
<!-- Override Spring Data release train provided by Spring Boot -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-releasetrain</artifactId>
<version>Fowler-SR2</version>
<scope>import</scope>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.4.3.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
參考來源:https://spring.io/blog/2014/06/26/introducing-the-spring-io-platform