Maven in depth
On this page 8 topics
Overview
Topics
- Dependency scopes
- transitive dependencies
- BOMs
- plugins
- multi-module basics
- reproducible builds
Learn first
88 min of reading · 3 h of practice (estimate)
Learning outcomes
- Choose the correct dependency scope (
compile,provided,runtime,test) for a dependency. - Inspect the dependency tree, explain transitive dependencies, and resolve a version conflict.
- Import a BOM to align versions and explain why Spring Boot uses one.
- Configure common plugins (compiler, surefire, failsafe) and split a project into a multi-module build.
- Explain what makes a build reproducible and use the Maven Wrapper.
Lessons
- 01Dependency scopes11 min read
- 02Transitive dependencies and dependency:tree11 min read
- 03Version conflicts and dependency management11 min read
- 04BOMs11 min read
- 05Plugins and the build lifecycle revisited11 min read
- 06Multi-module projects11 min read
- 07Reproducible builds and the Maven Wrapper11 min read
- 08Demo: untangling a dependency conflict with dependency:tree11 min read
Lab and exercises
Lab
JNR-002-LAB1 · Shop multi-module
Outcomes: 1–5 · Time: ~2 h Split the project into shop-core and shop-cli with a parent POM, central dependency management, and the Maven Wrapper. A version conflict is planted in the starter; diagnose it with dependency:tree and fix it. Acceptance: ./mvnw verify passes on a clean clone; no version is hard-coded in a child module; the README explains the conflict cause.
Exercises (assessment: fix a broken multi-module build and explain the cause, plus six exercises)
| ID | Outcome | Task |
|---|---|---|
| JNR-002-EX01 | 1 | Pick a scope for each: JUnit, Postgres JDBC driver, Servlet API (server provides it), Guava used in your code. |
| JNR-002-EX02 | 2 | Run dependency:tree on a project and name one transitive dependency and the dependency that brought it. |
| JNR-002-EX03 | 2 | Two libraries need commons-lang3 3.9 and 3.14. Which does Maven choose by default? Show two ways to control it. |
| JNR-002-EX04 | 3 | Import a BOM and add one dependency without a version. Explain why this is safer. |
| JNR-002-EX05 | 4 | Configure the compiler plugin for the Java release, and say which plugin runs *Test and which runs *IT. |
| JNR-002-EX06 | 4 | Create a parent POM with two modules, where one depends on the other. Show the build order. |
| JNR-002-EX07 | 5 | Add the wrapper, remove Maven from your PATH, and run the build. List three things that make a build reproducible. |
Show solutions
- EX01 JUnit
test; JDBC driverruntime; Servlet APIprovided; Guavacompile. - EX02 Example:
failureaccessbrought byguava. Read it from the tree indentation. - EX03 The nearest declaration wins; if equal distance, the first declared. Control with
dependencyManagement(pin) or a direct dependency or exclusion. - EX04
<scope>import</scope><type>pom</type>independencyManagement; the version comes from the BOM, so libraries match. - EX05
<release>inmaven-compiler-plugin; Surefire runs*Test; Failsafe runs*IT. - EX06 The parent lists
<modules>; the dependency module builds first (Maven sorts by dependencies). - EX07
mvn wrapper:wrapper, then./mvnw verify. Reproducible: fixed versions (no ranges), wrapper-pinned Maven, same Java release. - All outcomes are also covered by the lab; outcome 5 is covered by quiz Q10.
Quiz
Outcomes: O1 scopes · O2 dependency tree and conflicts · O3 BOMs · O4 plugins and multi-module · O5 reproducible builds.
A self-check for you: nothing is graded, saved or sent anywhere. Choose an answer, then check it. You can try again as often as you like.
Practical part
Seven exercises JNR-002-EX01 to EX07 plus the lab; for the assessment, fix a broken multi-module build and explain the cause. Pass: at least 8 of 10 quiz answers, six exercises correct, and ./mvnw verify green on a clean clone.
Common mistakes
Hard-coded versions in every module; compile scope for test libraries; ignoring dependency convergence.
Assessment
Fix a broken multi-module build and explain the cause.
Career relevance
Every Spring Boot project; dependency security (MID-053).