Spring IO Platform 簡介

Spring IO Platform框架簡單來說就是一個版本號兼容系統(tǒng),它將常用第三方類庫的兼容的版本組織起來。只要我們在項目中引用了Spring IO Platform,就不需要為這些第三方類庫設(shè)置版本號了,Spring IO Platform會自動幫我們設(shè)置所有兼容的版本號。本文參考自官方文檔,如果需要查閱詳細信息,請直接看原文即可。

引入類庫

使用Maven

使用Maven的話,在pom.xml中修改為類似這樣的。

<?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-SR3</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <!-- Dependency declarations -->

</project>

或者將設(shè)置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>your-application</artifactId>
    <version>1.0.0-SNAPSHOT</version>

    <parent>
        <groupId>io.spring.platform</groupId>
        <artifactId>platform-bom</artifactId>
        <version>Brussels-SR3</version>
        <relativePath/>
    </parent>

    <!-- Dependency declarations -->

</project>

設(shè)置完成后,以后添加依賴項就不需要指定版本好了??梢韵裣旅孢@樣添加依賴。

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <!-- 沒有版本號 -->
    </dependency>
</dependencies>

使用Gradle

如果用Gradle的話,就稍微復雜一點了。因為Gradle沒有dependencyManagement這么一個功能,所以還需要額外的插件??傊?,將build.gradle文件修改為類似這樣即可。

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'io.spring.gradle:dependency-management-plugin:1.0.0.RELEASE'
    }
}

apply plugin: 'io.spring.dependency-management'

repositories {
    mavenCentral()
}

dependencyManagement {
    imports {
        mavenBom 'io.spring.platform:platform-bom:Brussels-SR3'
    }
}

然后,聲明依賴就不需要版本號了。

dependencies {
    compile 'org.springframework:spring-core'
}

覆蓋版本號

有時候可能需要覆蓋Spring IO Platform中的版本號,改為使用我們自己指定的版本號。如果使用Maven的話,在pom.xml文件的properties節(jié)點中修改版本號。

<properties>
    <foo.version>1.1.0.RELEASE</foo.version>
</properties>

如果使用Gradle的話,在build.gradle中添加ext屬性即可。

ext['foo.version'] = '1.1.0.RELEASE'

或者

ext {
foo.version = '1.1.0.RELEASE'
}

也可以在gradle.properties文件中設(shè)置。

foo.version=1.1.0.RELEASE

已知問題

由于谷歌Guava類庫的廣泛使用,引用不同的項目時可能存在不兼容情況。這時候需要我們手動指定合適的版本號以保證項目能夠正常運行。

如果想詳細了解Spring IO Platform的版本號,可以查看官方文檔附錄

示例程序

其實這篇文章到這里就可以結(jié)束了,因為Spring IO Platform實際上確實也沒有多少東西要講。

這是我的一個小小例子,用Spring IO Platform和Gradle構(gòu)建的一個Spring MVC程序。下面是對應(yīng)的build.gradle文件??梢钥吹接捎谑褂昧薙pring IO Platform,所以這里的依賴項全部沒有指定版本號。

group 'yitian.study'
version '1.0-SNAPSHOT'

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'io.spring.gradle:dependency-management-plugin:1.0.0.RELEASE'
    }
}

apply plugin: 'java'
apply plugin: 'war'
apply from: 'https://raw.github.com/akhikhl/gretty/master/pluginScripts/gretty.plugin'
apply plugin: 'io.spring.dependency-management'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
    jcenter()
}

dependencies {
    testCompile group: 'junit', name: 'junit'
    compile 'org.springframework:spring-webmvc'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-logging'
}

dependencyManagement {
    imports {
        mavenBom 'io.spring.platform:platform-bom:Brussels-SR3'
    }
}

從IDE的提示可以看到,所有版本號都由Spring IO Platform正確處理了。

版本號

完整的例子在這里,雖然我感覺大部分不需要看這個。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,273評論 6 342
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,555評論 19 139
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,039評論 25 709
  • 文章作者:Tyan博客:noahsnail.com 2.Introduction to the Spring Fr...
    SnailTyan閱讀 5,538評論 7 56
  • 一葉而知秋。 今天的心情是深秋。 回頭看看假裝的努力,忍不住鄙視自己,知道有更好的辦法卻總拖著不做,大概還是壓力不...
    云小5閱讀 285評論 3 1

友情鏈接更多精彩內(nèi)容