【问题标题】:How to determine the <parent> dependency for a set of springframework dependencies如何确定一组 springframework 依赖项的 <parent> 依赖项
【发布时间】:2018-03-02 05:13:03
【问题描述】:

我想知道以下是否可行以及如何。

我正在关注 Spring Boot 的教程,其中提到我们可以拥有父依赖项。

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

然后定义不带版本号的依赖。

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

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

这会将 spring-boot-starter 和 spring-boot-starter-web 的依赖版本 1.5.6.RELEASE 添加到项目依赖项中。

就像我想找到我需要添加到新项目中的以下依赖项的&lt;parent&gt; 代码sn-p 是什么。 &lt;groupId&gt;org.springframework&lt;/groupId&gt; 中的依赖项。我需要使用 4.3.9.RELEASE 版本。

  • 弹簧上下文
  • spring-jdbc
  • 弹簧测试

谢谢!

【问题讨论】:

    标签: java spring maven dependencies


    【解决方案1】:

    如果您使用的是 Spring Boot,那么这三个依赖项将由以下启动器为您提供:

    • spring-test 将由 spring-boot-starter-test 提供
    • spring-context 将由 spring-boot-starter-data-jpa 提供
    • spring-jdbc 将由 spring-boot-starter-jdbc 提供

    因此,使用以下父级:

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

    ...如果添加这些依赖项:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    

    ...然后你会得到

    • 弹簧上下文
    • spring-jdbc
    • 弹簧测试

    但是,Spring Boot 1.5.6.RELEASE 依赖于这些核心 Spring 库的 v4.3.10.RELEASE not 4.3.9.RELEASE 如您的问题中所建议的那样。通常,您会接受 Spring 的依赖管理,因此如果 Sping 提供 4.3.10.RELEASE,那么 (a) 您应该使用该版本或 (b) 将 Spring Boot 降级为提供 4.3.9.RELEASE 的版本。

    继续阅读以了解如何为给定的精选库识别正确的启动器...

    spring-boot-starter-parent 是一个特殊的启动器,它提供有用的 Maven 默认值和一个依赖管理部分,它定义了您可能希望在 POM 中使用的大量依赖项。这些依赖通常被称为“curated”或“blessed”,因为它们是在 maven 层次结构中某处的依赖管理部分中定义的,您可以在没有版本标签的 POM 中引用它们(即它们从依赖管理部分条目。)

    您可以看到spring-boot-starter-parent POM here 并查看内部您可以看到它引用了spring-boot-dependencies POM here

    查看您提到的问题,您可以像这样声明依赖项......

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

    ...这是因为spring-boot-dependencies POM 声明了以下内容:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>${revision}</version>
    </dependency>
    

    因此,parent 和 starters 只是封装依赖声明并让应用程序开发人员更容易使用它们的一种方式。 Spring docs 总结为:

    Starters 是一组方便的依赖描述符,您可以将它们包含在您的应用程序中。您可以获得所需的所有 Spring 和相关技术的一站式商店,而无需搜索示例代码和复制粘贴加载的依赖描述符。例如,如果您想开始使用 Spring 和 JPA 进行数据库访问,请在项目中包含 spring-boot-starter-data-jpa 依赖项。

    然而,这 not 意味着所有依赖项都必须通过 parent 或 starters 声明,因此,如果您 使用 Spring Boot,那么您可以在不使用的情况下声明依赖项父母或初学者以及您在问题中描述的内容(声明对 3 个核心 Spring 库的依赖项)可以通过明确地依赖这 3 个库来安全地涵盖。例如,只需将以下内容添加到您的pom.xml

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.3.9.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>4.3.9.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>4.3.9.RELEASE</version>
        <scope>test</scope>
    </dependency>
    

    【讨论】:

    【解决方案2】:

    由于您正在阅读教程,因此我假设您是 Spring 新手。

    春天的人们非常好,可以设置一个生成项目的site。 这是非常容易使用。我建议在学习的同时尝试。下载一些具有所需依赖项的应用,然后查看它们的设置方式。

    一旦你感到舒服并想更深入地潜水,请再次阅读@glytching的答案,非常好。

    【讨论】:

      【解决方案3】:

      如果您不使用 Spring Boot 并且只需要 Spring Framework 依赖项,请使用 spring-framework-bom

      <dependencyManagement>
          <dependencies>
              <dependency>
                  <groupId>org.springframework</groupId>
                  <artifactId>spring-framework-bom</artifactId>
                  <version>4.3.9.RELEASE</version>
                  <type>pom</type>
                  <scope>import</scope>
              </dependency>
          </dependencies>
      </dependencyManagement>
      

      在这种情况下,依赖项将没有指定版本:

      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-jdbc</artifactId>
      </dependency>
      

      此外,如果您使用 Spring Boot 但又不想使用 spring-boot-starter-parent 作为父工件,则还有另一种选择:

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

      请参阅Spring Boot docs 了解更多详情。来自文档的重要说明:

      Spring Boot 的每个版本都与 Spring Framework 的基本版本相关联,因此我们强烈建议您不要自行指定其版本。

      这意味着你应该使用为 Spring Boot 定义的 Spring Framework 版本。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-29
        • 1970-01-01
        • 2020-07-24
        • 2014-03-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多