Spring IO Platform介绍

为什么要用Spring IO Platform

今天无意间看到了一个关键词:”Spring IO Platform”,第一直觉是不是有关于IO方面的框架或者包呢,查了一下,居然是为了解决包冲突而生的。做了半年Java Web开发,经常遇到问题,编译没有报错,启动时报一些奇奇怪怪的问题,90%的都是包冲突导致的,虽然有一些包冲突解决插件,比如”Dependency Analyzer”等,也能很容易解决,但是如果能防范于未然,那是多么美好的一件事情。
好了,废话不多说了,简单介绍一下这个包吧。

什么是Spring IO Platform

Spring IO Plat是一个附带包,不会编译到项目中,它只是将核心Spring API框架内聚集成到一个现代应用程序的平台中。它提供了已经测试完毕能很好协同工作的许多项目的Spring组合版本以及它们的依赖项。
举个例子说,如果你的包引入了spring-boot-starter-web和spring-boot-starter-test这两个包,不用手动填写包的版本,Spring IO Plat会下载这两个包最适合的版本下来,避免因为版本的问题,导致的这样那样的问题。

Maven使用Spring IO Platform

我平时用的是Maven来继承依赖包,所以这里介绍一下Maven怎么使用这个包,有两种使用方式,一种是直接在依赖里引入Spring IO Platform,另外一种方式通过依赖继承Spring IO Platform。

依赖管理引入Spring IO Platform

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>io.spring.platform</groupId>
                <artifactId>platform-bom</artifactId>
                <version>1.1.2.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

注意type和scope标签。
看下下载的包版本:
这里写图片描述
对比一下maven库里的版本,发现它拉下来的不是最新的版本,而是它认为合适的版本。
当然我们也可以指定依赖的包版本,会优先下载依赖的包版本的。

继承方式引入

    <parent>
        <groupId>io.spring.platform</groupId>
        <artifactId>platform-bom</artifactId>
        <version>1.1.2.RELEASE</version>
        <relativePath/>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

同样可以做到。
不过官方推荐前者。
不只是依赖包,插件也是可以用这种方式确定和依赖想匹配的组合的,比如Spring Boot的Maven插件,我们可以利用这个特性,在代码块中添加如下代码即可使用合适版本的插件:

    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
原文地址:https://www.cnblogs.com/xiang--liu/p/9710166.html