Transitive dependencies and dependency:tree

One line in a pom can bring a dozen jars into a build. A dependency declares its own dependencies, those declare theirs, and Maven resolves the whole graph before it compiles anything. The graph is not hidden: dependency:tree prints it as a tree and dependency:resolve prints the files it resolved to. This article reads both for the calculator, whose one test dependency brings seven more artifacts.

The project

One dependency declared, one test that uses it.

pom.xml

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>calc</artifactId> <version>1.0.0</version> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.release>21</maven.compiler.release> <junit.version>5.14.3</junit.version> </properties> <dependencies> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> </dependencies> </project>

src/main/java/com/example/calc/Calc.java

package com.example.calc; public class Calc { public static int add(int a, int b) { return a + b; } }

src/test/java/com/example/calc/CalcTest.java

package com.example.calc; import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.Test; class CalcTest { @Test void addsTwoNumbers() { assertEquals(4, Calc.add(1, 3)); } }

dependency:tree

The dependency plugin is one of the two standalone plugins in the local repository of this image, with the help plugin; neither is part of a lifecycle, so both are run by their coordinates:

mvn -B dependency:tree

[INFO] Scanning for projects...
[INFO]
[INFO] --------------------------< com.example:calc >--------------------------
[INFO] Building calc 1.0.0
[INFO] from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- dependency:3.7.0:tree (default-cli) @ calc ---
[INFO] com.example:calc:jar:1.0.0
[INFO] \- org.junit.jupiter:junit-jupiter:jar:5.14.3:test
[INFO] +- org.junit.jupiter:junit-jupiter-api:jar:5.14.3:test
[INFO] | +- org.opentest4j:opentest4j:jar:1.3.0:test
[INFO] | +- org.junit.platform:junit-platform-commons:jar:1.14.3:test
[INFO] | \- org.apiguardian:apiguardian-api:jar:1.1.2:test
[INFO] +- org.junit.jupiter:junit-jupiter-params:jar:5.14.3:test
[INFO] \- org.junit.jupiter:junit-jupiter-engine:jar:5.14.3:test
[INFO] \- org.junit.platform:junit-platform-engine:jar:1.14.3:test
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: <TIME>
[INFO] Finished at: <TIMESTAMP>
[INFO] ------------------------------------------------------------------------

The indentation is the graph: junit-jupiter is declared here, the three modules under it are what it declares, and the four under those are what they declare. Each line ends with the scope, and every one of them is test, because a scope is inherited down the graph: a test dependency's dependencies are test dependencies too.

The eight artifacts

What each one is, from the group and artifact names:

Three of them are compiled against by the test (the API and the two modules it needs), and two run without being compiled against (the engine and the platform engine): that is why the version of the same dependency was runtime in the previous article, where the dependency had no scope of its own. The scopes in a tree are the result of the declarations of every pom on the way down, not of this pom alone.

dependency:resolve

dependency:resolve reads the same graph and reports the file each artifact resolved to, with the Java module name of the jar:

mvn -B dependency:resolve

[INFO] Scanning for projects...
[INFO]
[INFO] --------------------------< com.example:calc >--------------------------
[INFO] Building calc 1.0.0
[INFO] from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- dependency:3.7.0:resolve (default-cli) @ calc ---
[INFO]
[INFO] The following files have been resolved:
[INFO] org.junit.jupiter:junit-jupiter:jar:5.14.3:test -- module org.junit.jupiter
[INFO] org.junit.jupiter:junit-jupiter-api:jar:5.14.3:test -- module org.junit.jupiter.api
[INFO] org.opentest4j:opentest4j:jar:1.3.0:test -- module org.opentest4j
[INFO] org.junit.platform:junit-platform-commons:jar:1.14.3:test -- module org.junit.platform.commons
[INFO] org.apiguardian:apiguardian-api:jar:1.1.2:test -- module org.apiguardian.api
[INFO] org.junit.jupiter:junit-jupiter-params:jar:5.14.3:test -- module org.junit.jupiter.params
[INFO] org.junit.jupiter:junit-jupiter-engine:jar:5.14.3:test -- module org.junit.jupiter.engine
[INFO] org.junit.platform:junit-platform-engine:jar:1.14.3:test -- module org.junit.platform.engine
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: <TIME>
[INFO] Finished at: <TIMESTAMP>
[INFO] ------------------------------------------------------------------------

Eight lines, the declared dependency included. The module name after the dashes is what a module-info.java would require, and it is the reason a jar can be on the module path as well as on the class path. dependency:resolve stops there; its sibling dependency:resolve-plugins does the same for plugins, and the plugin article runs it.

What the graph is for

Those eight jars are the test class path. The compile of the test finds @Test and assertEquals in them, and the run finds the engine that executes the method:

mvn -B test > build.log 2>&1; echo "status: $?"; sed "s#$PWD#<project>#g" build.log

status: 0
[INFO] Scanning for projects...
[INFO]
[INFO] --------------------------< com.example:calc >--------------------------
[INFO] Building calc 1.0.0
[INFO] from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- resources:3.4.0:resources (default-resources) @ calc ---
[INFO] skip non existing resourceDirectory <project>/src/main/resources
[INFO]
[INFO] --- compiler:3.15.0:compile (default-compile) @ calc ---
[INFO] Recompiling the module because of <REASON>.
[INFO] Compiling 1 source file with javac [debug release 21] to target/classes
[INFO]
[INFO] --- resources:3.4.0:testResources (default-testResources) @ calc ---
[INFO] skip non existing resourceDirectory <project>/src/test/resources
[INFO]
[INFO] --- compiler:3.15.0:testCompile (default-testCompile) @ calc ---
[INFO] Recompiling the module because of <REASON>.
[INFO] Compiling 1 source file with javac [debug release 21] to target/test-classes
[INFO]
[INFO] --- surefire:3.5.4:test (default-test) @ calc ---
[INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.example.calc.CalcTest
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: <TIME> s -- in com.example.calc.CalcTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: <TIME>
[INFO] Finished at: <TIMESTAMP>
[INFO] ------------------------------------------------------------------------

The local repository of this image holds more than those eight: 22 jars under org/junit, in several versions, because the image was built with more than one archetype and more than one project. A build uses the seven its graph resolves and ignores the rest; a version that is in the repository but not in the graph is not on any class path, and the next article shows what happens when two versions of one artifact are both in a graph.

Where to go next

Version conflicts puts two versions of junit-jupiter-api on one graph and shows which one wins. The local repository article opens the folder these eight jars came from.

Article author: Arthur Isaev