Maven & Gradle
Manage dependencies, plugins and multi-module builds.
By Invicly Tech, , in Backend Java Development. About 10 minutes to read.
In short
Maven and Gradle download dependencies, compile code, run tests and package your app. They make builds repeatable on every machine and in CI.
Explained simply
A real Java project uses dozens of libraries: a web framework, a JSON library, a database driver, test tools. Downloading each jar by hand and keeping versions straight would be a nightmare. A build tool does it for you.
You list what you need in a project file, pom.xml for Maven or build.gradle.kts for Gradle. The tool downloads those libraries, plus the libraries they need, compiles your code, runs your tests and packages everything into a jar you can run or deploy.
Both tools expect the same folder layout: your code in src/main/java, config files in src/main/resources and tests in src/test/java. Because every project looks the same, a new developer can find their way around in minutes.
The real benefit is repeatability. The same command gives the same result on your laptop, your colleague's laptop and the CI server. That is why every team commits the build file and the wrapper script (mvnw or gradlew) to Git.
Press Play on a diagram to walk through it one step at a time.
- 1validate
Check the project and pom.xml are correct
- 2compile
Compile src/main/java
- 3test
Run unit tests with Surefire
- 4package
Build the jar
- 5verify
Run integration tests and checks
- 6install
Copy the jar to the local ~/.m2 repository
- 7deploy
Publish to a remote repository
- XML pom.xml
- Fixed, conventional lifecycle
- Very predictable builds
- Huge plugin ecosystem
- Kotlin or Groovy scripts
- Flexible task graph
- Incremental builds and build cache
- Common for Android and large builds
- pom.xml or build.gradle.kts
- Coordinates, dependencies, plugins
- mvnw or gradlew
- Wrapper scripts, committed to Git
- src/main
- java: application code
- resources: config, SQL, templates
- src/test
- java: tests
- resources: test-only config
- target/ or build/
- Compiled output and jars
- Never commit it
In depth
Where dependencies come from
A library is identified by its coordinates: groupId, artifactId and version, for example org.postgresql:postgresql:42.7.4. When you build, the tool first looks in a local cache. Maven uses ~/.m2/repository and Gradle uses ~/.gradle/caches. If the jar is missing, it is downloaded from a remote repository, by default Maven Central, and cached for next time.
Each library also has its own list of dependencies, called transitive dependencies, and the tool pulls those in too. That is how one Spring Boot starter brings in dozens of jars. Versions ending in -SNAPSHOT mean work in progress and may change; release versions never change once published. Companies often run their own repository, such as Nexus or Artifactory, as a proxy and a place to publish internal libraries.
- 1Declared in build file
groupId, artifactId, version
- 2Check local cache
~/.m2 or ~/.gradle/caches
- 3Download if missing
From Maven Central or a company mirror
- 4Resolve transitive deps
Read its pom, repeat for each dependency
- 5Settle conflicts
Pick one version per library
- 6Build the classpath
Used to compile, test and run
Scopes and configurations
Not every library is needed everywhere. Maven uses scopes and Gradle uses configurations to say where a jar belongs. Maven's compile scope (the default) means needed to compile and run. provided means needed to compile, but the runtime supplies it, such as a servlet API inside an app server. runtime means only needed when running, like a JDBC driver. test means only for compiling and running tests.
Gradle's names are clearer once you learn them. implementation is for your own code; api (with the java-library plugin) also exposes the library to modules that depend on yours. compileOnly, runtimeOnly, testImplementation and testRuntimeOnly match the other cases. Getting scopes right keeps test tools out of production jars and keeps compile times short.
- Compile and run
- Maven: compile (default)
- Gradle: implementation or api
- Compile only
- Maven: provided
- Gradle: compileOnly
- Run only
- Maven: runtime
- Gradle: runtimeOnly
- Tests only
- Maven: test
- Gradle: testImplementation
Version conflicts and BOMs
Two of your libraries may need different versions of the same jar. Only one can be on the classpath. Maven chooses by nearest definition: the version closest to your project in the dependency tree wins, and on a tie the one declared first. Gradle by default picks the highest requested version. Either way, the winner may not be what one library expects, and you get errors like NoSuchMethodError at run time.
To see what really happened, run mvn dependency:tree or ./gradlew dependencies, and for one library ./gradlew dependencyInsight --dependency jackson-databind. To fix versions centrally, use a BOM (bill of materials): a pom that only lists tested versions. Spring Boot's BOM is why you can write Spring dependencies without versions. In Maven you put pins in the dependencyManagement section; in Gradle you use platform(...).
- mvn dependency:tree -Dincludes=com.fasterxml.jackson.core
- Maven: nearest wins. Gradle: highest wins
- NoSuchMethodError at run time often means a version conflict
Maven phases, goals and plugins
A phase is a step in the lifecycle, like compile or package. A goal is a concrete task provided by a plugin, like compiler:compile or surefire:test. Maven binds goals to phases, so mvn package runs every bound goal from validate up to package. You can also run a goal directly, for example mvn dependency:tree, without running the lifecycle.
The Surefire plugin runs unit tests in the test phase. The Failsafe plugin runs integration tests (classes ending in IT) in integration-test and verify, so mvn verify runs both. Useful flags: -DskipTests skips running tests, -pl api -am builds only the api module and what it needs, -U forces a re-check of snapshots, -o works offline, and -T 1C builds modules in parallel with one thread per core.
- mvn clean verify is the usual CI command
- mvn -q quiet output, mvn -X debug output
- Spring Boot: ./mvnw spring-boot:run
How Gradle thinks: tasks and caching
Gradle builds a graph of tasks such as compileJava, test and jar, and runs only the ones needed for what you asked. ./gradlew build runs assemble and check, which includes compiling and testing. Each task declares inputs and outputs. If nothing changed since the last run, the task is UP-TO-DATE and skipped, which makes repeat builds fast. The build cache can even reuse outputs from other machines or branches.
A multi-project build has a settings.gradle.kts that lists modules with include(...), and modules depend on each other with implementation(project(":domain")). Maven does the same with a parent pom whose packaging is pom and a modules section. Splitting by layer or feature keeps boundaries clear: the domain module cannot accidentally use web classes if it does not depend on them.
Learn it in 4 short steps
About 1h each. Track them with a timer in your plan.
- 1Learn the Maven lifecycle and pom.xml~1h
- 2Manage dependencies and scopes~1h
- 3Write a Gradle build script~1h
- 4Split a project into modules~1h
Key points
- Running a phase runs every earlier phase too: mvn package also compiles and tests.
- Dependency scope controls where a jar is used: compile, test, runtime or provided.
- Use the wrapper (mvnw or gradlew) so everyone builds with the same tool version.
- Multi-module builds split a project into modules with a shared parent build.
Key terms
Tap a card to flip it. 0/5 known
Examples
<!-- pom.xml: add JUnit for tests only -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.10.2</version>
<scope>test</scope>
</dependency>
<!-- Then run: ./mvnw clean package -->
<!-- Gradle equivalent: testImplementation("org.junit.jupiter:junit-jupiter:5.10.2") -->Fixing a NoSuchMethodError caused by a version conflict
The app compiles but crashes at start with NoSuchMethodError inside Jackson. Step 1: run mvn dependency:tree -Dincludes=com.fasterxml.jackson.core and see two versions requested; an old SDK pulls in 2.12 and it wins as the nearest. Step 2: the code was compiled against a newer API that 2.12 lacks. Step 3: pin one version for the whole build with Jackson's BOM in dependencyManagement. Step 4: run the tree again; every Jackson module now shows the same version.
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson</groupId>
<artifactId>jackson-bom</artifactId>
<version>2.18.2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- mvn dependency:tree -Dincludes=com.fasterxml.jackson.core -->A two-module Gradle build
You split a shop into domain (pure business logic) and api (web layer). Step 1: list both modules in settings.gradle.kts. Step 2: domain uses the java-library plugin because other modules consume it. Step 3: api depends on domain with implementation(project(...)). Step 4: JUnit versions come from its BOM via platform(...), and the launcher is added for the test runtime. Now ./gradlew :api:test builds domain first, then tests api, and domain can never import web classes.
// settings.gradle.kts
rootProject.name = "shop"
include("domain", "api")
// domain/build.gradle.kts
plugins { id("java-library") }
// api/build.gradle.kts
plugins { java }
repositories { mavenCentral() }
dependencies {
implementation(project(":domain"))
testImplementation(platform("org.junit:junit-bom:5.11.4"))
testImplementation("org.junit.jupiter:junit-jupiter")
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
}Quick reference
- ./mvnw clean verify runs unit tests (Surefire) and integration tests (Failsafe)
- ./gradlew build runs compile, tests and checks; ./gradlew test runs tests only
- mvn dependency:tree and ./gradlew dependencies show where each jar comes from
- Maven picks the nearest version in the tree; Gradle picks the highest
- mvn -pl api -am package builds one module plus the modules it needs
- Use a BOM (dependencyManagement or platform()) to pin related versions together
- Local caches: ~/.m2/repository (Maven) and ~/.gradle/caches (Gradle)
- Commit mvnw or gradlew and its wrapper folder; never commit target/ or build/
Common mistakes
- Committing build output folders like target or build.
- Declaring test libraries without test scope, bloating the final jar.
- Letting several modules pin different versions of the same library.
Quick quiz
Pick an answer to see if you're right and why.
Practice exercises
Try each one yourself first. Use the hint if you get stuck, then compare with the solution.
- Exercise 1easy
Generate a new Maven project from the quickstart archetype, package it and run the generated App class.
- Exercise 2medium
In a Spring Boot project, find out which version of jackson-databind is used and which dependency brought it in.
- Exercise 3hard
Turn a single Maven project into a parent with two modules, core and web, where web depends on core.
Check yourself
Answer in your head, reveal, then be honest with yourself.
Feel confident about this topic?
Frequently asked questions
- Should I choose Maven or Gradle for a new project?
- Both are fine and both are supported by Spring Initializr. Maven is simpler and more uniform; Gradle is faster on big builds and more flexible. Follow what your team already uses.
- What does SNAPSHOT mean in a version?
- It marks a work-in-progress build that can be replaced by a newer one with the same version. Release versions like 1.4.0 never change once published.
- Why is my first build so slow?
- The tool is downloading every dependency and plugin into its local cache. Later builds reuse the cache and are much faster.
- What is the Maven wrapper (mvnw)?
- A small script plus a properties file that downloads and runs a pinned Maven version, so nobody needs Maven installed and everyone uses the same version.