This article is part of the Maven source Code Parsing series: How does dependency mediation work? The first chapter, mainly do a beginning introduction. And make some preparation for the follow-up experiment.

Please read the rest of the series, in order, at juejin.cn/post/703292…

preface

Maven is a tool familiar to Java developers. Maven’s dependency mediation principle is a frequent topic of conversation. It is intended to solve the problem of which version number should be taken for the groupId and artifactId dependencies that are exactly the same. Here are some principles:

  • Principle 1: When transferring dependencies, the closest path takes precedence.
  • Principle 2: Pass dependencies, the first declarator takes precedence.
  • Principle 3: in the same file declaration, the latter overwrites the former. Also called: Homonymous overlay.
  • Principle 4: dependencyManagement Version locking. \

You know all of these principles, but do you know how Maven’s source code implements them? Without further ado, let’s get ready to implement and explore the source world of Maven!

Experimental environment

  • JDK 1.8
  • Download apache maven – 3.6.3 (source: archive.apache.org/dist/maven/…).
  • Maven3.6.3 companion core dependency Dependency: Tree, which includes two projects:
    • Maven – dependency – plugin – 2.8 (source code download: archive.apache.org/dist/maven/…).
    • Maven — the dependency tree – 2.1 (source code download: archive.apache.org/dist/maven/…).

A quick explanation:

  • Apache-maven-3.6.3 contains maven’s core code, as well as a portal to call various plug-ins.
  • Dependency: Tree is Maven’s core dependency plug-in. It plays an important role in dependency resolution. MVN Dependency :tree outputs the directory tree. This plugin consists of two parts: maven-dependency plugin and maven-dependency tree. The former contains a variety of dependencies related plug-ins, including the Tree plugin; The latter is a concrete implementation of Tree.

To experiment, we need to build a Demo project and manage dependency packages using Maven. This project is named mavenDependencyDemo, in which we create five modules A, B, C, D, and X. The structure is as follows: . ├ ─ ─ A │ ├ ─ ─ pom. The XML ├ ─ ─ B │ ├ ─ ─ pom. The XML ├ ─ ─ C │ ├ ─ ─ pom. The XML ├ ─ ─ D │ ├ ─ ─ pom. The XML ├ ─ ─ X │ ├ ─ ─ pom. The XML ├ ─ ─ pom. The XML We will modify the dependencies of different modules later in the analysis of each principle.