SpringBoot Pom文件没有父依赖问题(scope = import)

博客解释了Spring Boot项目中,为何可以不写依赖版本。问题在于POM文件中缺少`<parent>`标签,但实际上依赖管理通过`<dependencyManagement>`标签和`<dependency>`的`<scope>import</scope>`实现。`import`作用是将指定POM的依赖管理导入,使得在当前项目中引用相关依赖时可以省略版本号。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题引入:

都说SpringBoot创建时会默认继承一个 spring-boot-starter-parent依赖,但是我就没有… 难道因为我是生在新时代的人吗? 可能是吧。。。那么SpringBoot是如何可以不写依赖版本的呢?

默认Pom文件如下:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>test-pom</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>test-pom</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.3.7.RELEASE</spring-boot.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.3.7.RELEASE</version>
                <configuration>
                    <mainClass>com.example.testpom.TestPomApplication</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

压根没有 parent 标签好不好嘛

问题解决:

我一开始的想法是难道pom文件时默认继承那个父依赖的?答案是:漏,大漏特漏。
然后又开始怀疑难道是这个 < dependencyManagement > 作祟嘛,按道理来说不是呀,依赖管理标签不是用来声明什么依赖的吗,实际不导入,而是作为父类时什么依赖可以省略版本号。

dependencyManagement 组件

作用:用来申明依赖,但不导入。dependencies 组件用于导入依赖,注意两者区别特性:

  1. 子项目不会继承 dependencyManagement 组件中声明的依赖。
  2. 但如果子项目想导入某个父pom 中 dependencyManagement 中的依赖,只需要填写 groupId 和 artifactId ,不需要填写版本号,maven会自动去父pom 的 dependencyManagement 中找对应的 version,包括scope、exclusions等
<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
</dependencyManagement>

搜着搜着发现 pom 中 < scpoe > import < /scope > 的作用

但tm就是理解不了,草

有import

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

所有不声明版本的依赖均可导入。

无import

在这里插入图片描述
报错版本不能为空

关于import作用:

导入依赖范围,该依赖范围只能与 dependencyManagement 元素配合使用,其功能是将目标 pom.xml 文件中 dependencyManagement 的配置导入合并到当前 pom.xml 的 dependencyManagement 中。

总结:

import 说明继承了dependencyManagement中所有的依赖,从而导致当前pom能够导入不写版本的依赖,而不写import就是正常功能了,在当前pom文件中或者子类pom中,能够不提供版本的导入该依赖,和 dependencyManagement就没有任何关系。

本文参考链接:
Maven依赖传递,排除依赖和可选依赖
Maven依赖中scope的含义
官方文档scope含义

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值