我正在观看有关 Spring Boot 的视频教程(来自 javabrains.io)。示例项目的 pom 文件包含一个父 block ,其 groupId
为 org.springframework.boot
和 artifactId
为 spring-boot-起始父级
。
此外,它还包含一个依赖 block ,groupId
为 org.springframework.boot
和 artifactId
为 spring-boot-starter-网页
。
问)为什么我们的 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-boot-starter-parent
pom 文件,您会看到它提供了默认属性和 maven 插件配置,而 spring-boot-starter-web
提供web相关的spring依赖,无需额外配置。此外,两个启动器都继承自 spring-boot-dependencies
,它定义了 spring 支持的依赖项列表。这允许您在构建配置中省略任何这些依赖项的版本。您可以通过阅读了解更多信息 the official documentation .
总而言之,spring-boot-starter-parent
提供
- 默认的 maven 插件设置
- 默认的 maven 属性
- 依赖管理
而 spring-boot-starter-web
引入了与网络相关的依赖项。
关于spring - 为什么Spring Boot应用pom需要spring-boot-starter-parent和spring-boot-starter-web?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60104282/