Command Palette

Search for a command to run...

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

Running a phase runs every phase before it

That's why "mvn install" also runs your tests: install comes after test in the lifecycle. The real default lifecycle has 23 phases; this shows the ones with goals bound for a JAR project.

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?

Running a phase runs every phase before it Java 9+
Sign in to run this example in your browser.

Expected output

mvn compile runs 2 goals: resources:resources -> compiler:compile
mvn test runs 5 goals: resources:resources -> compiler:compile -> resources:testResources -> compiler:testCompile -> surefire:test
mvn package runs 6 goals: resources:resources -> compiler:compile -> resources:testResources -> compiler:testCompile -> surefire:test -> jar:jar
mvn install runs 7 goals: resources:resources -> compiler:compile -> resources:testResources -> compiler:testCompile -> surefire:test -> jar:jar -> install:install
mvn build: Unknown lifecycle phase "build"