一文轻松解决dependencyManagement和dependencies

605 阅读1分钟

1.dependencyManagement和dependencies的关系

dependencyManagement主要是用来锁定版本号的,在父模块中指定依赖的版本,然后如果想要子模块中使用,直接依赖即可,不需要指定版本号,如果不想使用父模块版本,也可以在子模块中自己进行指定。

parent模块

<properties>
    <spring.boot.starter.version>2.4.0</spring.boot.starter.version>
</properties>

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

order模块

<parent>
    <artifactId>demo-parent</artifactId>
    <groupId>com.angel.item</groupId>
    <version>1.0-SNAPSHOT</version>
    <relativePath/> <!--这个地方必须指定,要不然无法引用父模块的版本号-->
</parent>
<modelVersion>4.0.0</modelVersion>

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

区别

  • dependencyManagement指定以后,如果子模块不依赖则子模块中不会继承
  • dependencies:当在父模块中使用dependencies引用依赖,那么子模块会默认继承父模块中的依赖,不管有没有引用。

2.optional

optional有两个值
true: 不准被继承依赖
false: 运行被继承依赖(默认)

3.scope

compile: 默认值,会在编译,运行,测试,打包
provided:编译,测试,不会出现在打包中
runtime:测试,运行需要,编译不需要,比如说MySQL的驱动,只有在运行的时候需要,编译不需要
test:测试需要
system: 一般不建议使用