MVN Dependency: Tree (MVN Dependency: Tree) is one of the tools I use most often to find a package reference problem. But in my work, I found that there was a problem with printing.

In the case of multiple dependencies + level dependencies, Maven will only print dependencies in order. If a dependency has been printed before, no subsequent dependencies will be printed.

That’s a little abstract, but let me give you an example

graph LR
A -- scope=test --> B -- scope=compile --> C
A -- scope=compile --> B-1 -- scope=compile --> C

Maven as follows

<dependencies>

    <dependency>
        <groupId>org.example</groupId>
        <artifactId>maven-b</artifactId>
        <version>1.0 the SNAPSHOT</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.example</groupId>
        <artifactId>maven-b-1</artifactId>
        <version>1.0 the SNAPSHOT</version>
    </dependency>
</dependencies>
Copy the code

If the above dependency is followed, we executemvn dependency:treeWhat do you think will be printed?

To solve the above problem, let’s start by disassembling the problem

Maven Scope passes dependencies

Here I will not go into the scope of the detailed functions and values, forget the brother can see here the dependency scope scope is introduced. Scope’s delivery dependency is emphasized here. Let’s take a look at the official screenshot:

Summarize the dependencies of the figure above

  1. B depends on CprovidedortestSo A can’t depend on C
  2. B depends on CcompileSo what A depends on C directly inherits from what A depends on Bscope
  3. B depends on CruntimeSo in addition to the fact that A depends on BcompileThe case of PI is PIruntimeEverything else is inherited from A and depends on Bscope

From the conclusions on the previous page, it is easy to draw the dependency diagram below

graph LR
A -- scope=test --> B -- scope=compile --> C

MVN dependency:tree (MVN dependency:tree

Problem resolution

After we figure out the scope pass dependency, is it subjective enough to judge the question we originally asked? The dependencies shown below

graph LR
A -- scope=test --> B -- scope=compile --> C
A -- scope=compile --> B-1 -- scope=compile --> C

To tell you the truth, I was thinking the same thing. I think the result

[the INFO] org. Example: maven - a: jar: 1.0 the SNAPSHOT [INFO] + - org. Example: maven - b: jar: 1.0 the SNAPSHOT: test | \ [INFO] - Org. Example: maven - c: jar: 1.0 the SNAPSHOT: the test \ [INFO] - org. Example: maven - b - 1: jar: 1.0 the SNAPSHOT: compile | \ [INFO] - Org. Example: maven - c: jar: 1.0 the SNAPSHOT: the compileCopy the code

But I executemvn denpendency:treeAnd here’s what happened.

\- Represents the last node in the same layer

Then I adjusted the order in which A depends on B and B minus 1 as follows

<dependency>
    <groupId>org.example</groupId>
    <artifactId>maven-b-1</artifactId>
    <version>1.0 the SNAPSHOT</version>
    <scope>compile</scope>
</dependency>
<dependency>
    <groupId>org.example</groupId>
    <artifactId>maven-b</artifactId>
    <version>1.0 the SNAPSHOT</version>
    <scope>test</scope>
</dependency>
Copy the code

The results are as follows

MVN Dependency: Tree: Dependency Tree: MVN Dependency: Tree: MVN Dependency: Tree: MVN Dependency: Tree: MVN Dependency: Tree: MVN Dependency: Tree: MVN Dependency: Tree: MVN Dependency: Tree

conclusion

MVN Dependency: Tree (MVN Dependency :tree

  1. Dependency executes the print order as we write itdependencyThe order about
  2. Dependency is not as complete as we might think when printingtreeStructure of the print
  3. When the same layer depends on the same resource, only the first dependency will be printed and printedscopeSubject to the final dependencecompile>runtime > provided > testOrder is optimal.

subsequent

In the future, I hope to have the opportunity to study the source code of the Maven-Dependency – Plugin to prove the above conclusion. If you have a study of the old iron welcome to leave a message, thank you!

Thank you

Welcome to comment! Content is updated continuously!