我看到许多人pom文件里面dependency没有写version,但是我不行会报错

比如著名的start.spring.io
默认的生成的就没有,但是我电脑不行

<dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-devtools</artifactId>
      <scope>runtime</scope>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>

是不是maven也是看脸的啊! 还有说一句,如果我的dependency添加version,编译后,把version删除。 也是可以编译通过的

阅读 5.8k
1 个回答

没写 version 的,是通过 dependencyManagement 来完成版本管理的。你可以仔细看一下 start.spring.io 生成的项目的父项目,pom.xml 里一定有 dependencyManagement项,
比如,生成项目的父项目:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

打开这个 spring-boot-starter-parent, 你会看到:

...
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.4.1</version>
</parent>

再打开这个 spring-boot-dependencies

你会看到:

<dependencyManagement>
<dependencies>
    <dependency>
    <groupId>org.apache.activemq</groupId>
    <artifactId>activemq-amqp</artifactId>
    <version>${activemq.version}</version>
    </dependency>
<dependency>
...
</dependencyManagement>       

相信你已经看明白了。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题