SpringBoot的依赖管理

以下是Maven工程:helloworld的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>helloworld</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.1</version>
    </parent>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

在这里插入图片描述
可以看到,pom.xml中我们并没有指定依赖spring-boot-starter-web、插件spring-boot-maven-plugin的版本,但实际却安装了2.6.1版本。难道是海螺姑娘默默地帮我们做了这些?恩,父项目spring-boot-dependencies做了版本管理。

进入spring-boot-starter-parent,再进入spring-boot-dependencies,我们来探探究竟。
在这里插入图片描述
在这里插入图片描述
spring-boot-dependencies中的

  • properties,指定了所有常用依赖的默认版本。
  • dependencyManagement,几乎声明了所有常用的依赖(及其版本)。当然了,只有当前项目pom.xml在depencencies中引入了对应依赖,才会下载依赖,即实现了按需下载。
  • pluginManagement,声明了常用的插件(及其版本)。只有当前项目pom.xm在plugins中引入了对应插件,才会下载插件,即实现了按需下载。

因此,在helloworld这个项目中,即使我们没有指定依赖spring-boot-starter-web、插件spring-boot-maven-plugin的版本,但依然能够从父项目spring-boot-dependencies中获取它们的默认版本。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-dependencies</artifactId>
  <version>2.6.1</version>
  <packaging>pom</packaging>
  <name>spring-boot-dependencies</name>
  <description>Spring Boot Dependencies</description>
  <!-- ...... -->
  <properties>
 	<!-- ...... -->
  	<mysql.version>8.0.27</mysql.version>
  	<!-- ...... -->
  </properties>
  
  <dependencyManagement>
 	 <!-- ...... -->
      <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>${mysql.version}</version>
        <exclusions>
          <exclusion>
            <groupId>com.google.protobuf</groupId>
            <artifactId>protobuf-java</artifactId>
          </exclusion>
        </exclusions>
      </dependency>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>2.6.1</version>
      </dependency>
      <!-- ...... -->
  </dependencyManagement>
  <build>
  	<pluginManagement>
  	    <!-- ...... -->
        <plugin>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-maven-plugin</artifactId>
          <version>2.6.1</version>
        </plugin>
        <!-- ...... -->
  	</pluginManagement>
  </build>
 </project>

如果不想使用spring-boot-dependencies指定的默认版本号,想自己指定版本号,可以在当前项目的pom.xml的dependencies中引入依赖,并在properties中重新指定版本号,maven会根据就近原则(或最短路径原则)覆盖默认设置。
比如,引入mysql驱动:mysql-connector-java。

  • 使用spring-boot-dependencies指定的默认版本号:8.0.27。
    在这里插入图片描述
<dependencies>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
</dependencies>
  • 使用自己指定的版本号,比如5.1.49。
    在这里插入图片描述
<properties>
    <mysql.version>5.1.49</mysql.version>
</properties>

<dependencies>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
</dependencies>

如果引入的某个依赖,spring-boot-dependencies中没有指定默认版本,我们需要使用version明确指定依赖的版本号。

综上,SpringBoot使用父项目做依赖管理,引入依赖时可以不写版本号,将使用默认版本号,(当然了,可以修改默认的版本号,自己另外指定),但引入非版本仲裁jar时,要写版本号。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值