【问题标题】:Why both spring-boot-starter-parent and spring-boot-starter-web are needed for Spring Boot application pom?为什么 Spring Boot 应用程序 pom 同时需要 spring-boot-starter-parent 和 spring-boot-starter-web?
【发布时间】:2020-05-23 01:10:46
【问题描述】:

我正在关注有关 Spring Boot 的视频教程(来自 javabrains.io)。示例项目的 pom 文件包含一个父块,其中groupIdorg.springframework.bootartifactIdspring-boot-starter-parent

此外,它还包含一个依赖块,其中groupIdorg.springframework.bootartifactIdspring-boot-starter-web

Q) 为什么我们的 pom.xml 中需要两个元素(即父元素和依赖元素)?

我想既然项目pom继承自spring-boot-starter-parent,那么所有的依赖也会自动继承。


示例项目pom.xml文件如下:

<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>io.javabrains.springbootquickstart</groupId>
    <artifactId>course-api</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>Java Brains Course API</name>

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

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

    <properties>
        <java.version>1.8</java.version>
    </properties>
</project>

【问题讨论】:

    标签: spring spring-boot maven


    【解决方案1】:

    如果您查看spring-boot-starter-parent pom 文件,您会看到它提供了默认属性和 maven 插件配置,而spring-boot-starter-web 提供了与 web 相关的 spring 依赖项,无需任何额外配置。此外,两个 starter 都继承自 spring-boot-dependencies,它定义了 spring 支持的依赖项列表。这允许您在构建配置中省略任何这些依赖项的版本。您可以阅读the official documentation了解更多信息。

    总而言之,spring-boot-starter-parent 提供

    • 默认 maven 插件设置
    • 默认的 Maven 属性
    • 依赖管理

    spring-boot-starter-web 引入与网络相关的依赖项。

    【讨论】:

      猜你喜欢
      • 2022-07-30
      • 1970-01-01
      • 2016-01-29
      • 1970-01-01
      • 2017-02-19
      • 2019-10-28
      • 2014-04-07
      • 2015-07-07
      • 1970-01-01
      相关资源
      最近更新 更多