This article is participating in the Java Theme Month – Java Debug Notes Event, see the event link for details

dependencies

We usually see dependencies used in projects managed by Maven. They are used to manage dependencies.

<dependencies>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
      	<version>3.17</version>
    </dependency>
</dependencies>
Copy the code

But there are some disadvantages:

1. In a multi-module project, if a dependency is introduced in the parent POM, the child module will accept all the dependencies introduced in the parent POM, even if it does not need the dependency itself. If the submodules are started or packaged separately, the speed is slow and the jar is bloated.

2. To avoid the first problem, make the required dependencies in the submodule. However, if other submodules also need this dependency, it is easy to cause version conflicts.

Hence the dependencyManagement.

dependencyManagement

This tag is especially useful in multi-module projects.

Use location

This is typically used in the parent POM.

Use the sample

The parent module pom
<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <version>2.1.9. RELEASE</version>
    <scope>test</scope>
    <exclusions>
      <exclusion>
        <groupId>com.vaadin.external.google</groupId>
        <artifactId>android-json</artifactId>
      </exclusion>
    </exclusions>
  </dependency>
</dependencies>

<dependencyManagement>
  <dependencies>
      <dependency>
          <groupId>org.apache.poi</groupId>
          <artifactId>poi-ooxml</artifactId>
          <version>3.17</version>
      </dependency>
  </dependencies>
</dependencyManagement>
Copy the code

Declaring the above dependencies in the parent POM does not actually import the dependencies directly. It’s just a declarative function.

Take a look at the parent POM using Maven Helper.

As you can see, there are only dependencies introduced through Dependencies.

The child module pom

The submodule POM can introduce dependencies by declaring only the corresponding groupId and artifactId as needed. The omitted version and scope look up the groupId and artifactId of the most recent dependencyManagement declaration along the tree to the upper POM and use their declared version and scope.

<dependencies>
    <! -- external -->
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
    </dependency>
</dependencies>
Copy the code

Submodule coverage

Allows submodules to declare version and scope to override configurations inherited from their parent.

<dependencies>
    <! -- external -->
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
         <version>3.18</version>
    </dependency>
</dependencies>
Copy the code

Explicitly deliver dependent versions

When using dependencyManagement, if there are multiple versions of the transfer dependency in a project, the version of the dependencyManagement declaration will override it. There are advantages and disadvantages.

If the version is compatible, then we are left to eliminate the version of the trouble.

The downside is that version conflicts are not easily noticed when they occur.

Dependency priority

DependencyManagement in the parent POM allows the quilt module’s dependencyManagement to override.

Look up the tree to the upper POM for the groupId and artifactId of the most recent dependencyManagement declaration. DependencyManagement specifies the latest groupId and artifactId if the subclass dependencyManagement specifies the same groupId and artifactId as the parent dependencyManagement. If the levels are the same, the priority declared first is higher.

Therefore, in conjunction with dependencies:

1. Dependencies have a higher priority than dependencyManagement.

2. In the dependency tree, the closer the hierarchy is to the import position, the higher the priority is.

3. If the hierarchy is the same, the priority of the first declaration is higher.