首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring IO Platform 简介

Spring IO Platform 简介

作者头像
乐百川
发布2018-01-09 11:00:15
5650
发布2018-01-09 11:00:15
举报

Spring IO Platform框架简单来说就是一个版本号兼容系统,它将常用第三方类库的兼容的版本组织起来。只要我们在项目中引用了Spring IO Platform,就不需要为这些第三方类库设置版本号了,Spring IO Platform会自动帮我们设置所有兼容的版本号。本文参考自官方文档,如果需要查阅详细信息,请直接看原文即可。

引入类库

使用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>

或者将设置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>

设置完成后,以后添加依赖项就不需要指定版本好了。可以像下面这样添加依赖。

<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节点中修改版本号。

<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文件中设置。

foo.version=1.1.0.RELEASE

已知问题

由于谷歌Guava类库的广泛使用,引用不同的项目时可能存在不兼容情况。这时候需要我们手动指定合适的版本号以保证项目能够正常运行。

如果想详细了解Spring IO Platform的版本号,可以查看官方文档附录

示例程序

其实这篇文章到这里就可以结束了,因为Spring IO Platform实际上确实也没有多少东西要讲。

这是我的一个小小例子,用Spring IO Platform和Gradle构建的一个Spring MVC程序。下面是对应的build.gradle文件。可以看到由于使用了Spring 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正确处理了。

版本号
版本号

完整的例子在这里,虽然我感觉大部分不需要看这个。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017年08月26日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 引入类库
    • 使用Maven
      • 使用Gradle
        • 覆盖版本号
          • 已知问题
          • 示例程序
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档