Maven vs Gradle in one lesson
30 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)Gradle builds turn up on GitHub, in Android work and in job ads. You already know Maven, so one side-by-side comparison is enough to read a Gradle project.
The same project as pom.xml and as build.gradle.kts
Gradle is the other big Java build tool. It does the same job as Maven, with the same libraries from Maven Central, but its build file is a short Kotlin program, build.gradle.kts, instead of XML.
Compare the Maven pom.xml of the receipt app with the same project as a Gradle build. Both name the same coordinates, the same three libraries and Java 17.
Example 1 · pom.xml of receipt-app (the Maven version)
Runs in: the receipt-app Maven project
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.college</groupId> <artifactId>receipt-app</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.release>17</maven.compiler.release> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-text</artifactId> <version>1.15.0</version> </dependency> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> <version>26.7.0</version> <scope>runtime</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter</artifactId> <version>5.14.4</version> <scope>test</scope> </dependency> </dependencies></project>(Compare it with the Gradle build in Example 2: same coordinates, same three libraries.)
What this code does
- Thirty-seven lines of XML: coordinates, two properties and three dependencies with their scopes.
- No repository is listed, because Maven Central is Maven's built-in default.
Example 2 · settings.gradle.kts and build.gradle.kts for the same project
Runs in: the receipt-app project built with Gradle 9.8.1
// settings.gradle.ktsrootProject.name = "receipt-app"// build.gradle.ktsplugins { application}group = "com.college"version = "1.0-SNAPSHOT"repositories { mavenCentral()}dependencies { implementation("org.apache.commons:commons-text:1.15.0") runtimeOnly("com.mysql:mysql-connector-j:26.7.0") testImplementation(platform("org.junit:junit-bom:5.14.4")) testImplementation("org.junit.jupiter:junit-jupiter") testRuntimeOnly("org.junit.platform:junit-platform-launcher")}tasks.withType<JavaCompile>().configureEach { options.release = 17 options.encoding = "UTF-8"}application { mainClass = "com.college.app.Main"}tasks.test { useJUnitPlatform()}$ ./gradlew -q run Hostel fee receipt Student: Asha Rao Total: Rs 110000 $ ./gradlew -q run --args="meena" Hostel fee receipt Student: Meena Total: Rs 110000
What this code does
- The JUnit BOM (
platform(...)) fixes one version for every JUnit artifact, so those lines need no version of their own. application { mainClass = ... }plays the part of the exec plugin:runstartsmain, and--argspasses the arguments.
build.gradle.kts is written in Kotlin (.kts means Kotlin script); older projects use build.gradle in Groovy, which reads almost the same, and a small settings.gradle.kts names the project. Gradle keeps the src/main/java layout but writes its output to build instead of target.
The biggest practical difference: a pom is fixed XML with one way to do each thing, while a Gradle file is code, shorter and more flexible, and also easier to make confusing.
-q (quiet), so only the program's lines show. Without it Gradle prints task names such as > Task :run and BUILD SUCCESSFUL.Shared ideas: coordinates, Maven Central, scopes and configurations
A Gradle configuration says how a library is used, like a Maven scope: implementation, runtimeOnly, compileOnly or testImplementation.
Everything from this module carries over. Gradle names libraries with the same coordinates, written as one string "group:artifact:version", and it downloads from the same Maven Central. This command prints the libraries on the run classpath.
./gradlew dependencies --configuration runtimeClasspathTransitive dependencies work the same way, except that when two versions clash Gradle picks the highest one, where Maven picks the nearest. Maven's scopes become configurations, and each Maven command has a Gradle twin:
- compile scope →
implementation - runtime scope →
runtimeOnly; provided →compileOnly - test scope →
testImplementation(andtestRuntimeOnly) mvn test/package/exec:java→gradlew test/build/run
Predict the output
Maven to Gradle
Pick the Gradle word that matches the Maven idea.
Round 1 of 3
The MySQL driver has scope runtime in Maven.
Wrappers: mvnw and gradlew so everyone uses the same version
A wrapper is a small script committed with the project. The first time it runs, it downloads one exact version of the build tool and uses it, so teammates need only a JDK.
Add the Maven Wrapper to the receipt app. Then run Maven through ./mvnw, whatever Maven is or is not installed on the computer.
Example 3 · Add the Maven Wrapper to a project
Runs in: a terminal in the receipt-app Maven project
mvn wrapper:wrapper "-Dmaven=3.9.16"cat .mvn/wrapper/maven-wrapper.properties$ mvn wrapper:wrapper "-Dmaven=3.9.16" [INFO] Scanning for projects... [INFO] ----------------------< com.college:receipt-app >----------------------- [INFO] Building receipt-app 1.0-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ jar ]--------------------------------- [INFO] --- wrapper:3.3.4:wrapper (default-cli) @ receipt-app --- [INFO] Unpacked only-script type wrapper distribution org.apache.maven.wrapper:maven-wrapper-distribution:zip:only-script:3.3.4 [INFO] Configuring .mvn/wrapper/maven-wrapper.properties to use Maven 3.9.16 and download from https://repo.maven.apache.org/maven2 [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 3.615 s [INFO] Finished at: 2026-10-16T10:28:21+05:30 [INFO] ------------------------------------------------------------------------ $ cat .mvn/wrapper/maven-wrapper.properties wrapperVersion=3.3.4 distributionType=only-script distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.16/apache-maven-3.9.16-bin.zip
What this code does
- The wrapper plugin writes
mvnw,mvnw.cmdand.mvn/wrapper/maven-wrapper.properties.distributionUrlpins Maven 3.9.16, downloaded from Maven Central. - To move the whole team to a newer Maven later, change the version in this one file and commit it.
Example 4 · ./mvnw: Maven without installing Maven
Runs in: a terminal in the receipt-app Maven project with the wrapper
./mvnw -v./mvnw -q compile exec:java "-Dexec.mainClass=com.college.app.Main"$ ./mvnw -v Apache Maven 3.9.16 (2bdd9fddda4b155ebf8000e807eb73fd829a51d5) Maven home: C:\Users\asha\.m2\wrapper\dists\apache-maven-3.9.16\56ba1f9f Java version: 19.0.2, vendor: Oracle Corporation, runtime: C:\Program Files\Java\jdk-19 Default locale: en_IN, platform encoding: UTF-8 OS name: "windows 11", version: "10.0", arch: "amd64", family: "windows" $ ./mvnw -q compile exec:java "-Dexec.mainClass=com.college.app.Main" Hostel fee receipt Student: Asha Rao Total: Rs 110000
What this code does
- "Maven home" is inside
~/.m2/wrapper/dists: the wrapper downloaded Maven 3.9.16 on first use, whatevermvnis installed on this computer. - Every
mvncommand works with./mvnw(in PowerShell.\mvnw).-Dexec.mainClasspasses the main class on the command line, so this project needs no exec configuration in its pom.
From then on everyone types ./mvnw instead of mvn (in PowerShell: .\mvnw), and the downloaded Maven lands in ~/.m2/wrapper/dists. Gradle projects almost always have a wrapper too: gradlew, gradlew.bat and the gradle/wrapper folder, so ./gradlew build needs no Gradle install at all.
mvnw without its run permission; run git update-index --chmod=+x mvnw once before committing, or macOS teammates get "permission denied" (they can fix it with chmod +x mvnw).Where each is used in industry, and why this course uses Maven
Both are everywhere. Maven is very common in company back-end projects, including most Spring Boot services, because its fixed structure makes every project look alike. Gradle is the standard for Android apps, and many newer and very large projects choose it for speed (it reuses earlier results more aggressively) and flexibility.
This course uses Maven because the XML is explicit: every setting you learn has one obvious place, errors point at a named element, and Spring Boot's own guides and project generator support it fully. Once you are fluent in Maven, moving to Gradle is a matter of reading a new syntax for the same ideas.
- Choose Maven for standard Java and Spring Boot back ends and teams that value a fixed structure.
- Expect Gradle in Android work and in projects with unusual or very large builds.
- Either way: use the wrapper and pin versions.
Reading a Gradle build well enough to run someone else's project
To run a Gradle project from GitHub, read build.gradle.kts from top to bottom: plugins shows the kind of project, dependencies lists the libraries, and application { mainClass = ... } names the entry point.
./gradlew tasks # list what this build can do./gradlew test # compile and run the tests./gradlew build # compile, test and build build/libs/receipt-app-1.0-SNAPSHOT.jar./gradlew -q run --args="meena"./gradlew dependencies --configuration runtimeClasspath # like mvn dependency:treeCheck which Java version the build asks for, and always use the project's wrapper. On Windows the script is gradlew.bat, run in PowerShell as .\gradlew.
Next step
Check your understanding
11 quick questions