Maven vs Gradle
You have now built the same kinds of project with both tools. This lesson puts them side by side, so you can choose one for a new project, or argue for (or against) a migration with facts.
The concepts, mapped
| Concept | Maven | Gradle |
|---|---|---|
| Build description | pom.xml (declarative XML) |
build.gradle.kts (Kotlin code configuring a model) |
| Module list | <modules> in an aggregator POM |
include(...) in settings.gradle.kts |
| Shared configuration | parent POM (single inheritance) | convention plugins (composable) |
| Work units | goals bound to lifecycle phases | tasks in a graph computed per build |
| Dependency scopes | compile, provided, runtime, test |
api, implementation, compileOnly, runtimeOnly, test* |
| Conflict rule | nearest declaration wins | highest version wins |
| Central versions | dependencyManagement, BOM import |
version catalog, platform(), constraints |
| Hard rules | enforcer plugin | strictly, failOnVersionConflict, custom checks |
| Skipping work | per-plugin (compiler's stale check) | up-to-date checks for every task, build cache, configuration cache |
| Local install / remote publish | install / deploy |
publishToMavenLocal / publish (maven-publish) |
| Fat JAR | shade plugin | application distribution, or the community Shadow plugin |
| Version pinning of the tool | mvnw |
gradlew |
| Long-lived process | Maven Daemon mvnd (optional) |
Gradle Daemon (default) |
The same build, twice
The clearest practical difference is what happens on the second build. The session builds one project with both tools, twice each, and counts the work done:
mkdir -p both/src/main/java/demo both/src/test/java/demo && cd both
cat > src/main/java/demo/Calc.java <<'SRC'
package demo;
public class Calc { public static int add(int a, int b) { return a + b; } }
SRC
cat > src/test/java/demo/CalcTest.java <<'SRC'
package demo;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
class CalcTest { @Test void adds() { assertEquals(4, Calc.add(2, 2)); } }
SRC
cat > pom.xml <<'POM'
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId><artifactId>both</artifactId><version>1.0</version>
<properties>
<maven.compiler.release>21</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter</artifactId><version>5.14.4</version><scope>test</scope></dependency>
</dependencies>
<build><pluginManagement><plugins>
<plugin><artifactId>maven-compiler-plugin</artifactId><version>3.16.0</version></plugin>
<plugin><artifactId>maven-surefire-plugin</artifactId><version>3.6.0</version></plugin>
</plugins></pluginManagement></build>
</project>
POM
echo 'rootProject.name = "both"' > settings.gradle.kts
cat > build.gradle.kts <<'KTS'
plugins { java }
repositories { mavenCentral() }
dependencies {
testImplementation(platform("org.junit:junit-bom:5.14.4"))
testImplementation("org.junit.jupiter:junit-jupiter")
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
}
tasks.test { useJUnitPlatform() }
KTS
for run in 1 2; do
echo "maven run $run: $(mvn verify | grep -E 'Nothing to compile|Compiling|Tests run:.*in demo' | sed 's/,* Time elapsed.*//;s/\[INFO\] //' | paste -sd'|' -)"
done
for run in 1 2; do
echo "gradle run $run: $(gradle build | grep -E 'actionable tasks')"
donemaven run 1: Compiling 1 source file with javac [debug release 21] to target/classes|Compiling 1 source file with javac [debug release 21] to target/test-classes|Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
maven run 2: Nothing to compile - all classes are up to date.|Nothing to compile - all classes are up to date.|Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
gradle run 1: 4 actionable tasks: 4 executed
gradle run 2: 4 actionable tasks: 4 up-to-date
Maven noticed the sources were unchanged ("Nothing to compile") but ran the tests again. Gradle skipped every task. In a project whose tests take ten minutes, that's the difference between a ten-minute and a two-second no-op build, and the build cache extends it to clean checkouts and CI.
Choosing
Maven is a good fit when
- the build is conventional (libraries, services, standard plugins), and you value a build that every Java developer can read without learning a language;
- a strict, fixed lifecycle is a feature: there is one way to do things, and auditors and newcomers like that;
- the organisation already has parent POMs, archetypes and plugins.
Gradle is a good fit when
- the build is large (dozens or hundreds of modules) and incremental speed matters; the build and configuration caches pay for themselves;
- the build does unusual things (code generation, several languages, custom packaging) that would need custom Maven plugins;
- it's an Android project (the Android Gradle Plugin makes Gradle the only practical choice; see Mobile Build Pipelines) or a Kotlin Multiplatform project.
Costs to weigh
- Gradle builds are programs. Without discipline (convention plugins, no
allprojects, lazy APIs) they become hard to understand; Maven's rigidity prevents that. - Gradle upgrades more often and deprecates APIs; plugins must keep up (configuration cache compatibility, for example).
- Maven's no-op builds are slow, and its parallel builds (
-T) are coarse, per module.
For a new small or medium service, either is fine; follow what your team knows. For a growing monorepo, Gradle (or Bazel) is usually where teams end up.
Test yourself
On a second build with no changes, what typically happens?
- Both tools skip everything
- Maven re-runs tests and packaging; Gradle reports every task UP-TO-DATE
- Gradle re-runs tests, Maven skips them
- Both re-run everything
Which Gradle feature corresponds to Maven's parent POM for shared build configuration?
- settings.gradle.kts
- Convention plugins
- The version catalog
- The wrapper
Which project type effectively requires Gradle?
- A Spring Boot service
- An Android app
- A Java library
- A command-line tool