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

Where Gradle is the better fit

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

Article author: Arthur Isaev