Maven dependencies 和 DependencyManagement 区别

之前使用PandoraBoot + HSF 构建应用的时候出现了一个诡异问题

package com.taobao.ase;

import com.taobao.pandora.boot.PandoraBootstrap;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * ServerApplication类 用于启动 pandora 和 spring 应用
 */
@SpringBootApplication
public class ServerApplication {
    public static void main( String[] args ) {
        PandoraBootstrap.run(args);
        SpringApplication.run(ServerApplication.class,args);
        PandoraBootstrap.markStartupAndWait();
    }
}

为什么会出现这个问题呢,看了一下 pom 文件

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>com.taobao.pandora</groupId>
      <artifactId>pandora-boot-starter-bom</artifactId>
      <version>2017-09-stable</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  <dependency>
      <groupId>com.alibaba.boot</groupId>
      <artifactId>pandora-hsf-spring-boot-starter</artifactId>
  </dependency>
  <dependency>
     <groupId>com.taobao.pandora</groupId>
     <artifactId>taobao-hsf.sar</artifactId>
  </dependency>
</dependencies></dependencyManagement>

命名已经引入依赖了为什么还会出现问题呢?

后来查看类似项目的pom文件,将上面文件改成:

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>com.taobao.pandora</groupId>
      <artifactId>pandora-boot-starter-bom</artifactId>
      <version>2017-09-stable</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

<dependencies>
  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>3.8.1</version>
    <scope>test</scope>
  </dependency>
  <dependency>
    <groupId>com.alibaba.boot</groupId>
    <artifactId>pandora-hsf-spring-boot-starter</artifactId>
  </dependency>
  <dependency>
    <groupId>com.taobao.pandora</groupId>
    <artifactId>taobao-hsf.sar</artifactId>
  </dependency>
</dependencies>

然后代码正常,idea不报红,这是为什么呢?上网特意搜了一下dependencyManagement 和 dependencies 区别,如下:dependencyManagement:只是声明依赖,并不实现引入,因此子项目需要显示的声明需要用的依赖。如果不在子项目中声明依赖,是不会从父项目中继承下来的;只有在子项目中写了该依赖项,并且没有指定具体版本,才会从父项目中继承该项,并且versionscope都读取自父pom;另外如果子项目中指定了版本号,那么会使用子项目中指定的jar版本

dependencies:相对于dependencyManagement,所有生命在dependencies里的依赖都会自动引入,并默认被所有的子项目继承。

原来dependencyManagement 只是申明依赖,并不实现引入。问题就在这里了,把依赖放到dependencies里面,问题解决。

猜你喜欢

转载自blog.csdn.net/diu_brother/article/details/79579674