为什么 spring-boot-dependencies 在 dependencyManagement 中?

Why is spring-boot-dependencies in dependencyManagement?

Spring 文档 Using Spring Boot without the parent POM 显示 spring-boot-dependencies 的依赖项已添加到 dependencyManagement 部分。这真的正确吗?

spring-boot-dependencies 指定所有依赖项的版本属性。但是,这些属性在使用 spring-boot-dependencies 的 POM 中不可用。据推测,这是因为 spring-boot-dependenciesdependencyManagement.

spring-boot-dependencies 仅包括 dependencyManagementpluginManagement。因此,似乎可以将 spring-boot-dependencies 作为依赖项(而不是 dependencyManagement)包括在内,而无需添加不必要的依赖项。

那么为什么 spring-boot-dependencies 包含在 dependencyManagement 中?

绝对正确。请参阅 Using Spring Boot without the parent POM!

So why is spring-boot-dependencies to be included as dependencyManagement?

假设您有一个名为 projectA 的项目,您将 spring-boot-dependencies 添加到 pom.xml.

dependencyManagement 部分
<project>
  <groupId>com.iovation.service</groupId>
  <artifactId>projectA</artifactId>
  <version>1.0.0-SNAPSHOT</version>

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <type>pom</type>
        <version>1.5.8.RELEASE</version>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

  <dependencies>
    <!-- Spring Boot Dependencies -->
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
      </dependency>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
      </dependency>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
      </dependency>
  ...
</project>

如果您仔细观察,您会发现在dependencies 部分下声明的所有Spring 引导依赖项不需要指定version。它从 dependencyManagement 部分中指定的 spring-boot-dependencies 版本派生 version

依赖管理的优势

  • 它通过在一处指定 Spring 引导版本来集中依赖信息。它在从一个版本升级到另一个版本的过程中确实很有帮助。

  • Spring 引导依赖项的后续声明仅提及库名称而没有任何版本。在多模块项目中特别有用

  • 避免项目中不同版本的spring引导库不匹配。

  • 没有冲突。

  • 首先,让我们了解一下什么是依赖。因此,在开发应用程序时,您可能需要许多库(通常是 jar 文件)。这意味着您的应用程序依赖于这些库。因此名称依赖。
  • 现在您需要一种方法 assemble 所有这些库并以集中方式管理它们。这也意味着这些库将在需要时在编译时或运行时可用。这就是依赖管理所做的。
  • 所以依赖管理的过程涉及定位这些依赖并将它们添加到类路径中。
  • Maven 是一种流行的依赖管理工具,它将集中所有依赖信息。