Maven vs. Gradle Explained: How Java Projects Are Built
Java & Spring Boot for Beginners · Part 6 of 14
- #Java
- #Spring Boot
- #Beginners
- #Tooling
Part 5 closed out the
fundamentals arc — variables, objects, collections, everything you need to
read and write a single Java file. But you never actually opened
GroupBooking.java on its own. It sat next to a pom.xml or a
build.gradle you probably scrolled past without reading closely, because
nothing in it looked like the Java you were learning. Part 7 starts building
a real Spring Boot service, and it's going to assume you know what that file
is for. So before we get there: what a build tool actually does, and how to
read one you didn't write.
Maven vs. Gradle: the short answer
Both do the same three jobs: fetch your dependencies (usually from Maven
Central), run a fixed compile → test → package lifecycle, and produce a
.jar you can ship. The difference is how you describe the build.
- Maven uses a declarative
pom.xml. It's more verbose, but there's no logic in it, so a build you've never seen behaves exactly as it reads. It's still the default in a lot of established Java shops. - Gradle uses a build script that is real code (Groovy or Kotlin). It's faster on incremental builds and more flexible, and that flexibility is also how a Gradle build becomes hard to debug. It's the default for Android and for new projects from Spring Initializr.
If you're starting out, learn Maven first, because you'll read more of it in existing codebases. The concepts carry over directly. The rest of this post explains each piece. If you don't have a JDK installed yet, start with Setting Up Your Java Development Environment.
What javac won't do for you
javac compiles Java source into bytecode. That's it — that's the whole
job. It doesn't know your project depends on Jackson for JSON or JUnit for
tests. It doesn't fetch anything. It doesn't run your tests, and it
definitely doesn't produce an executable .jar with a manifest that knows
which class has main. Every one of those is a separate, tedious step you'd
otherwise do by hand — and a Spring Boot app easily pulls in a hundred jars
once you count what your direct dependencies themselves depend on. Getting
the classpath right for that by hand, in the right order, is not a task a
person should be doing in 2026.
A build tool exists to solve three problems at once: figure out and fetch everything your code depends on, run a repeatable sequence of steps in the right order, and package the result into something you can actually ship. Maven and Gradle are the two you'll meet in a Java job. They solve the same three problems with different philosophies, which is most of what makes switching between them feel harder than it should.
Dependencies have addresses
You don't download a .jar file and drop it in a folder anymore — you
declare what you need and let the build tool find it. Every published Java
library has a coordinate: groupId:artifactId:version. For example,
org.springframework.boot:spring-boot-starter-web:3.3.4 is unambiguous —
one organization, one library, one exact version, forever. You write that
coordinate in your build file, and the build tool resolves it: downloads
that artifact from a repository (almost always Maven Central by
default, sometimes a company's private mirror on top of it), reads its
declared dependencies, downloads those too, and keeps going until the whole
tree is resolved. Everything lands in a local cache on your machine — ~/.m2
for Maven, ~/.gradle for Gradle — so the second project that needs the
same artifact doesn't refetch it.
This is also where most of the pain in a Java project actually lives:
transitive dependencies. You ask for one starter, and it drags in
things it needs, which drag in things they need, and two of your direct
dependencies can end up wanting different versions of the same
transitive one. Both build tools have a resolution strategy for picking a
winner (Maven takes the nearest declaration in the tree; Gradle takes the
highest version by default), and both let you override the choice
explicitly when the automatic pick is wrong. You don't need to master this
today — just know the term "transitive dependency" and that dependency tree (Maven) or dependencies (Gradle) are the commands that show you
what actually got resolved when something behaves strangely.
The lifecycle: compile, test, package
Once dependencies are sorted, both tools run your code through a lifecycle — a fixed sequence of stages, each one assuming the previous ones already ran. The three you'll touch constantly:
- compile — turn your source into bytecode (this is the step that's
just
javac, wired up with the right classpath for you). - test — compile and run your test sources, fail the build if a test fails.
- package — bundle the compiled classes, resources, and (for a Spring
Boot app) the dependencies themselves into a single runnable
.jar.
Maven models this as a strict, ordered lifecycle: validate →
compile → test → package → verify → install → deploy. Running
mvn package runs validate, compile, and test first, automatically —
you can't package something you haven't compiled and tested. Plugins hook
into specific phases (the Spring Boot Maven plugin binds its repackage
goal onto package to turn a plain jar into an executable one with an
embedded server inside it).
Gradle models the same idea as a graph of tasks instead of a straight
line: compileJava, test, jar, bootJar, and so on, each declaring
which other tasks it depends on. ./gradlew build runs the whole graph in
dependency order, same practical result as Maven's package. The
difference matters once you go past the basics — Gradle's task graph is
something you can extend with real code (it's Groovy or Kotlin under the
hood), where Maven's lifecycle is something you configure through XML and
plugin declarations. Neither is "the build system does more" — they're two
different answers to the same problem, and you'll meet both in real jobs.
The same project, two build files
Here's a minimal Spring Boot web app dependency, declared both ways.
Maven's pom.xml:
<project>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.4</version>
</parent>
<groupId>dev.josealencar</groupId>
<artifactId>booking-service</artifactId>
<version>1.0.0</version>
<properties>
<java.version>21</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Gradle's build.gradle (the Groovy DSL — a Kotlin DSL version,
build.gradle.kts, does the same thing with stricter typing):
plugins {
id 'org.springframework.boot' version '3.3.4'
id 'io.spring.dependency-management' version '1.1.6'
id 'java'
}
group = 'dev.josealencar'
version = '1.0.0'
java.sourceCompatibility = JavaVersion.VERSION_21
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
}
Notice what's not in either file: no version number next to
spring-boot-starter-web. That's the "starter parent" (Maven) or the
dependency-management plugin (Gradle) doing its job — Spring Boot publishes
a curated list of dependency versions known to work together, called a
bill of materials, and both build files just inherit it. You only specify
a version yourself when you're pulling in something outside that curated
set, or deliberately overriding it.
Maven or Gradle — which do you actually learn
Honestly, both, eventually, because you don't get to choose which one a job's existing codebase uses. But if you're picking where to start:
Maven's XML is more verbose to read and write, but it's also more
predictable — there's no logic in it, just declarations, so a pom.xml
from a project you've never seen behaves exactly as it looks. It's the
default in a lot of established enterprise Java shops, and it's what I'd
point a beginner at first for that reason — it's harder to write something
surprising by accident.
Gradle is faster on incremental builds (it only rebuilds what actually changed, aggressively), and because the build file is real code, it's more flexible for anything custom — which is also exactly how a Gradle build can turn into something only the one engineer who wrote it can debug. It's the default for Android and it's gaining ground in newer Spring projects too, partly because Spring Initializr defaults new projects to it.
My honest take after years of both: learn Maven first because you'll read more of it in existing codebases, but don't be precious about it — the concepts (coordinates, lifecycle, dependency scope) transfer directly, and the syntax is the only thing that's actually different.
Always use the wrapper
Open a real Java project's root folder and you'll find mvnw and
mvnw.cmd, or gradlew and gradlew.cmd, checked into version control
right next to pom.xml. That's the wrapper, and it exists to solve
one specific, recurring headache: "it builds on my machine but not on
yours" because you have Maven 3.9 installed globally and your teammate has
3.6. The wrapper is a small script that downloads the exact Maven or
Gradle version the project was built against, the first time you run it,
and uses that from then on — so the build tool's version is pinned in the
repo the same way your dependency versions are.
Run ./mvnw package or ./gradlew build, not a globally installed mvn
or gradle, even if you have one. It's a small habit that saves you from
a whole category of "works for me" bug reports.
Reading a build file you didn't write
When you open an unfamiliar pom.xml or build.gradle for the first
time, you don't need to understand every line. Look for, in this order:
the Java version (a java.version property or sourceCompatibility line
— tells you which language features are actually available), the
dependency list (tells you what the project actually does — a
spring-boot-starter-data-jpa means there's a database somewhere), and
any plugin configuration that deviates from the defaults (that's usually
where the interesting, project-specific decisions live). Everything else
is plumbing you can safely skim past until you have a reason not to.
That's enough to walk into an unfamiliar Java repository, open its build file, and know roughly what you're looking at — which dependencies it pulls in, what Java version it targets, and what happens when you run a build. You don't need to write one from scratch yet. Part 8 will have you generate a real one through Spring Initializr in Your First Spring Boot Application and watch every piece of this post show up in the generated file. Next, Part 7 finally answers the question this whole series has been building toward: what Spring Boot actually is under the annotations, and why it replaced plain Java EE as the default way to build a backend.