(二)Spring Cloud Alibaba BOM 依赖管理

 依赖管理

Spring Cloud Alibaba BOM 包含了它所使用的所有依赖的版本。

一、版本管理规范

项目的版本号格式为 x.x.x 的形式,其中 x 的数值类型为数字,从 0 开始取值,且不限于 0~9 这个范围。项目处于孵化器阶段时,第一位版本号固定使用 0,即版本号为 0.x.x的格式。

由于 Spring Boot 1 和 Spring Boot 2 在 Actuator 模块的接口和注解有很大的变更,且spring-cloud-commons 从 1.x.x 版本升级到 2.0.0 版本也有较大的变更,因此 Spring CloudAlibaba 采取跟 SpringBoot 版本号一致的版本:

  • 1.5.x 版本适用于 Spring Boot 1.5.x
  • 2.0.x 版本适用于 Spring Boot 2.0.x
  • 2.1.x 版本适用于 Spring Boot 2.1.x
  • 2.2.x 版本适用于 Spring Boot 2.2.x

下表展示了 Spring Cloud Alibaba & Spring Cloud & Spring Boot 兼容关系:

The Spring Cloud Alibaba & Spring Cloud & Spring Boot compatibility table

Spring Cloud VersionSpring Cloud Alibaba VersionSpring Boot Version
---------------------
Spring Cloud Hoxton2.2.x.RELEASE2.2.x.RELEASE
Spring Cloud Greenwich2.1.x.RELEASE2.1.x.RELEASE
Spring Cloud Finchley2.0.x.RELEASE2.0.x.RELEASE
Spring Cloud Edgware1.5.x.RELEASE1.5.x.RELEASE

以后我们的项目选择的版本为:

  • Spring Boot 2.2.3.RELEASE
  • Spring Cloud Hoxton.SR3
  • Spring Cloud Alibaba 2.2.0.RELEASE

二、依赖管理

Spring Cloud Alibaba BOM 包含了它所使用的所有依赖的版本。如果您是 Maven Central用户,请将我们的 BOM 添加到您的 pom.xml 中的 <dependencyManagement> 部分。 这将允许您省略任何 Maven 依赖项的版本,而是将版本控制委派给 BOM。

<dependencyManagement>
	<dependencies>
		<dependency>
			<groupId>com.alibaba.cloud</groupId>
			<artifactId>spring-cloud-alibaba-dependencies</artifactId>
			<version>2.2.0.RELEASE</version>
			<type>pom</type>
			<scope>import</scope>
		</dependency>
	</dependencies>
</dependencyManagement>

在下面的章节中,假设您使用的是 Spring Cloud Alibaba bom,相关 starter 依赖将不包含版本号。

三、父项目的创建

使用父项目能控制所有子项目依赖的全局版本,能有效的去除子项目里面重复的依赖,减少出错的几率。在此,我们将创建一个父 pom,让所有的子模块都继承该父 pom。

3.1 spring-cloud-alibaba-examples  项目的创建

spring-cloud-alibaba-examples 将作为我们子项目的最大父 pom 项目!

注意:在使用 IDEA 之前我们假设您已经设置了相应的 JDK 配置和 Maven 配置

3.1.1 使用 IDEA 创建一个 Maven 项目

创建一个新的 Maven 项目

填写下面的相关信息:

Name:spring-cloud-alibaba-examples

Location:D:\workspace\spring-cloud-alibaba-examples(不要随便放)

GroupId:com.bjsxt

Version:1.0

最后,点击 finish 完成创建的过程!

3.1.2 Spring Boot 版本的控制

我们采用<parent> 的方式来导入 Spriing Boot 的版本号。

打开项目的 pom.xml 文件,添加依赖内容:

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
</parent>

这样,我们的项目就已经规定了 Spring Boot 的版本为 2.2.3.RELEASE 了。

3.1.3 Spring Cloud 版本的控制

我们使用依赖管理的方式来添加 Spring Cloud 的版本信息,在<properties> 里面定义版本信息,这里面我们选择 Hoxton.SR3 这个版本!

<properties>
    <spring-cloud.version>Hoxton.SR3</spring-cloud.version>
</properties>

之后在 <dependencyManagement> 里面添加 spring-cloud 的 bom 信息,这将允许您省略任何 Maven 依赖项的版本,而是将版本控制委派给 BOM。

<dependencyManagement>
     <dependencies>
          <dependency>
               <groupId>org.springframework.cloud</groupId>
               <artifactId>spring-cloud-dependencies</artifactId>
               <version>${spring-cloud.version}</version>
               <type>pom</type>
               <scope>import</scope>
          </dependency>
     </dependencies>
</dependencyManagement>

3.1.4 Spring Cloud Alibaba 版本的控制

同样,我们使用依赖管理的方式来添加 Spring Cloud Alibaba 的版本信息。在<properties>里面定义版本信息,这里面我们选择 2.2.0.RELEASE 这个版本!

<properties>
    ...
    <com-alibaba-cloud.version>2.2.0.RELEASE</com-alibaba-cloud.version>
    ...
</properties>

之后在 <dependencyManagement> 里面添加 spring-cloud 的 bom 信息,这将允许您省略任何 Maven 依赖项的版本,而是将版本控制委派给 BOM。

<dependencyManagement>
	<dependencies>
		...
		<dependency>
		<groupId>com.alibaba.cloud</groupId>
		<artifactId>spring-cloud-alibaba-dependencies</artifactId>
		<version>${com-alibaba-cloud.version}</version>
		<type>pom</type>
		<scope>import</scope>
		</dependency>
		....
	</dependencies>
</dependencyManagement>

3.1.4 设置为 pom 的版本方式

添加项目的打包方式:

<packaging>pom</packaging>

这将保证我们的项目是以 pom 打包的。

3.1.5 完整的 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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.ld</groupId>
    <artifactId>spring-cloud-alibaba-examples</artifactId>
    <version>1.0</version>

    <properties>
        <spring-cloud.version>Hoxton.SR3</spring-cloud.version>
        <com-alibaba-cloud.version>2.2.0.RELEASE</com-alibaba-cloud.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${com-alibaba-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>

3.2  项目的打包

3.2.1 删除项目里面多余的文件夹

我们的项目仅仅是一个父 pom 文件,因此,项目只需要保留 pom.xml 即可,我们在此可以删除 src 目录。

点击 Delete 即可删除。

3.2.2 执行打包

使用 Maven 打包我们的项目。

最后在控制台出现:

代表项目打包成功!

3.2.3 观察打包后的效果

我们打开我们 Maven 设置的本地仓库地址,如图所示:

打开 Maven 里面的 settings.xml 文件,找到该标签

发现我们本地仓库的地址位于 D 盘的 lib\jar 文件夹里面:

打开该文件夹:

找到:

发现该文件已经存在,证明我们的打包成功了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

plenilune-望月

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值