Command Palette

Search for a command to run...

Back to the lesson: Topic 15.4 — Maven and Gradle Basics
Core Java · Example 1 of 3 Java 16+

Maven's "nearest wins" conflict resolution

commons-text 1.12 was newer but lost: both were at depth 2 and 1.9 was met first. Gradle would pick 1.12 (highest version wins). If json-lib 1.4 calls a method added in 1.12, Maven's choice fails at runtime with NoSuchMethodError.

Maven and Gradle are build tools: from one file (pom.xml or build.gradle.kts) they download your libraries and the libraries those need, compile your code, run the tests and package a JAR with one command. Maven uses a fixed lifecycle of phases and XML configuration; Gradle uses a task graph, a Kotlin or Groovy script, and skips work whose inputs haven't changed.

Change the code and press Run (Ctrl+Enter). Try to predict the output first, then break it on purpose and read the error. Your edits are saved and match the lesson page.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Extend the "nearest wins" program so it also prints the Gradle choice (highest version) for each artifact. Compare versions numerically, part by part.

02

Write a program that prints the Maven coordinates group:artifact:version split into their three parts, and says whether the version is a SNAPSHOT.

03

Write the Gradle dependencies { } block for: Jackson databind 2.17.2 for main code, the PostgreSQL JDBC driver 42.7.4 only at runtime, and Mockito 5.12.0 for tests.

Explain it without notes

01

What does a build tool do for a Java project?

02

Explain the Maven build lifecycle and the relationship between phases, plugins and goals.

03

How do Maven and Gradle resolve version conflicts between transitive dependencies?

04

What are dependency scopes or configurations, and why do they matter?

05

Why is Gradle often faster than Maven on large projects?

Maven's "nearest wins" conflict resolution Java 16+
Sign in to run this example in your browser.

Expected output

resolving orders-service:
  + http-client:2.1 (depth 1, via orders-service)
  + json-lib:1.4 (depth 1, via orders-service)
  + commons-text:1.9 (depth 2, via http-client)
  - json-lib:1.2 (depth 2, via http-client) omitted for conflict with 1.4
  - commons-text:1.12 (depth 2, via json-lib) omitted for conflict with 1.9
class path: {commons-text=1.9, http-client=2.1, json-lib=1.4}