Gradle versus Maven
Both tools do the same job: read a description of the build, order the work, run it. The difference is the model. Maven describes a project in XML and binds plugins to a fixed lifecycle; Gradle describes it in a script and builds a graph of tasks. This lesson builds one project with both of them, in the same folder, so the two descriptions and the two results can be compared line by line.
One project, two tools
The sources are the two classes of the previous lesson. Maven needs a pom.xml; Gradle needs a settings.gradle and a build.gradle. The two tools write their output to different folders (target/ and build/), so they can share the sources without cleaning up after each other.
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>hello</artifactId>
<version>1.0.0</version>
<properties>
<maven.compiler.release>21</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
settings.gradle
rootProject.name = 'hello'
build.gradle
plugins {
id 'java'
}
version = '1.0.0'
src/main/java/com/example/Greeter.java
package com.example;
public final class Greeter {
public static String greet(String name) {
return "Hello, " + name;
}
}
src/main/java/com/example/App.java
package com.example;
public final class App {
public static void main(String[] args) {
System.out.println(Greeter.greet("Gradle"));
}
}
Maven runs its default lifecycle up to package and says nothing when it succeeds, which is what -q asks for. Gradle runs the build task and prints every task it ran.
mvn -B -q package
ls target
classes
generated-sources
hello-1.0.0.jar
maven-archiver
maven-status
gradle build --console=plain --offline --no-daemon
> Task :compileJava
> Task :processResources NO-SOURCE
> Task :classes
> Task :jar
> Task :assemble
> Task :compileTestJava NO-SOURCE
> Task :processTestResources NO-SOURCE
> Task :testClasses UP-TO-DATE
> Task :test NO-SOURCE
> Task :check UP-TO-DATE
> Task :build
BUILD SUCCESSFUL in 17s
2 actionable tasks: 2 executed
ls build/libs
hello-1.0.0.jar
Two archives with the same name and the same contents, from two descriptions of the same build. The Maven command needs no file name because the packaging decides the name; the Gradle command needs no file name because the java plugin names the jar after the project and its version.
The table
| Maven | Gradle | |
|---|---|---|
| the description | pom.xml, XML with a fixed schema | build.gradle (Groovy) or build.gradle.kts (Kotlin), a program |
| the unit of work | a plugin goal bound to a lifecycle phase | a task, with inputs, outputs and dependencies of its own |
| the order | three fixed lifecycles: clean, default, site | whatever the task graph needs, computed per invocation |
| the second run | phases run again; a plugin decides whether to recompile | a task whose inputs did not change is UP-TO-DATE or comes FROM-CACHE |
| dependencies | scopes: compile, provided, runtime, test | configurations: implementation, api, compileOnly, runtimeOnly, testImplementation |
| several projects | a parent pom with modules and a reactor | include in the settings file, with a project per folder or a composite build |
| shared build logic | a parent pom, a plugin, an archetype | buildSrc or an included build with a convention plugin |
| the wrapper | mvnw, which downloads Maven | gradlew, which downloads Gradle |
| what a beginner sees | a long [INFO] log with the plugin version of every step | a short list of tasks, and a failure that names the task |
Where Maven is the better fit
- A build that should look like every other build. The lifecycle is fixed, so a Maven project of a stranger reads like your own, and a plugin cannot invent a step nobody expects.
- XML that a tool can check. A pom has a schema, and an IDE completes it from the schema, without running the build.
- An organisation that already has Maven. Parent poms, the local repository and the CI templates are in place, and a second build tool is a second thing to maintain.
Where Gradle is the better fit
- A build with work of its own. A code generator, a container image, a publish step: in Gradle each is a task with inputs and outputs, and the whole thing is a program, not a plugin configuration.
- Speed on the second run. Up-to-date checks, the build cache and the configuration cache skip work Maven repeats, and a large build notices.
- Several languages in one build. Java, Kotlin, Groovy and Scala plugins share the same source set model; Android builds are Gradle only.
Neither list is a verdict. The two tools coexist well: a Gradle build can consume a jar from the local Maven repository, and the publishing lesson of this course does exactly that.
Related
- Maven: the other build tool, with a course of the same shape.
- Publish to mavenLocal and use the jar: the local repository as the bridge between the two tools.
- Java: the language both tools build.
Автор статьи: Arthur Isaev