【问题标题】:Why you don't need to specify the version for some dependencies in Spring Boot project?为什么 Spring Boot 项目中某些依赖项不需要指定版本?
【发布时间】:2020-03-06 12:20:33
【问题描述】:

我正在学习Spring Boot,我看到您不需要为某些依赖项指定版本,但您需要为其他依赖项指定版本。这个依赖的版本来自哪里?

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.dgs</groupId>
    <artifactId>n-tier-and-jackson</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>n-tier-and-jackson</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <paypal.rest.easy.version>2.3.3-RELEASE</paypal.rest.easy.version>
    </properties>

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

        <dependency>
            <groupId>com.paypal.springboot</groupId>
            <artifactId>resteasy-spring-boot-starter</artifactId>
            <version>${paypal.rest.easy.version}</version>
        </dependency>

...

例如在这段代码中你不需要为 spring-boot-starter-jersey 指定版本,但你需要为 resteasy-spring-boot-starter 指定版本。 spring-boot-starter-jersey 的版本来自哪里?谢谢!

【问题讨论】:

  • 对于我们未提及版本的所有其他 spring starter depdencies,使用在文件的 pom.xml 顶部定义的父 spring starterdependecy 的版本来选择版本
  • 我强烈建议使用最新版本的 Spring Boot,这意味着 2.2.1.RELEASE 并且不再使用 1.X,因为它的生命周期已经结束。
  • 你了解父项目的工作原理吗?

标签: java maven spring-boot dependencies


【解决方案1】:

依赖版本在您在 pom 文件的 parent 部分中指定的 pom 文件中声明

【讨论】:

  • 感谢您的回复,我在父节的pom文件中找到了spring-boot-starter-jersey依赖的版本。那我为什么要在 pom 文件的依赖部分声明依赖呢?那么如果存在依赖,为什么我不能直接从父级的pom文件中导入一个类呢?
  • @elvis 因为 Maven 依赖管理是如何工作的。父 pom 在 标记中声明了依赖项。当你在dependencyManagement中声明一个依赖时,你实际上并没有添加依赖,而是说如果有任何子项目使用该依赖,那么就使用父pom中声明的版本。所以简而言之,dependencyManagement 就是要确保你所有的子项目一致地使用相同的版本。
猜你喜欢
  • 2022-01-09
  • 1970-01-01
  • 2020-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-03
相关资源
最近更新 更多