Course topics

By WebNest Studio

Spring Framework Tutorial

Installing Maven and Gradle

Spring projects are built with either Maven or Gradle. A build tool downloads the Spring libraries your project needs, compiles your code, runs tests, and packages the application. You only need one of them, and neither is required to be installed system-wide, because every project generated by Spring Initializr includes a wrapper script that downloads the correct version automatically.

This lesson explains the wrapper, shows manual installation for both tools on every operating system, and compares the two so you can choose.

The build wrapper: the recommended way

A project made with Spring Initializr contains mvnw and mvnw.cmd (Maven) or gradlew and gradlew.bat (Gradle). These small scripts read a properties file, download the exact tool version the project was designed for, cache it in your home folder, and run it. Everyone on the team, and your CI server, therefore uses the same version without installing anything except a JDK.

In a project generated for Spring Boot 4.1.1 the file .mvn/wrapper/maven-wrapper.properties points to Apache Maven 3.9.16. Run the scripts like this: ./mvnw clean package on macOS and Linux, mvnw.cmd clean package in Windows Command Prompt, and .\mvnw clean package in PowerShell.

Installing Maven manually

Spring Boot 4.1 supports Maven 3.6.3 or later. Maven itself runs on Java, so install the JDK first.

  • Windows (manual): download the binary zip from maven.apache.org, extract it to a folder such as C:\tools\apache-maven, then add its bin folder to PATH.
  • Windows (package managers): choco install maven with Chocolatey, or scoop install maven with Scoop. Note that the winget catalogue does not provide a Maven package.
  • macOS: brew install maven
  • Ubuntu / Debian: sudo apt install maven (the packaged version can be older, so check it with mvn -v).
  • Any system with SDKMAN: sdk install maven
  • Verify: mvn -v. It prints the Maven version, the Java version it found, and the OS.

Installing Gradle manually

Spring Boot 4.1 supports Gradle 8.14 or later in the 8.x line, and Gradle 9.x. Gradle also needs a JDK to run.

  • macOS: brew install gradle
  • Windows: choco install gradle or scoop install gradle, or unzip the binary distribution from gradle.org and add its bin folder to PATH.
  • Any system with SDKMAN: sdk install gradle
  • Verify: gradle -v
  • To add a wrapper to an existing Gradle project, run gradle wrapper once, then use ./gradlew afterwards.

Maven or Gradle: how to choose

Both are fully supported by Spring Boot and by every major IDE, and the dependencies are identical. The difference is style.

  • Maven uses a declarative XML file (pom.xml) with a fixed lifecycle. It is verbose but predictable, and it is the most common choice in enterprise teams and in learning material.
  • Gradle uses a Groovy or Kotlin script (build.gradle or build.gradle.kts). It is shorter, very flexible, and often faster on large projects through incremental builds and caching.
  • For this course we use Maven in examples, but every dependency shown can be written for Gradle with the same coordinates.

The commands you will use every day

You will use only a handful of commands. The table in the example shows the Maven form, and the Gradle form follows the same idea. The first run is slow because the tool downloads dependencies into a local cache (the .m2 folder for Maven, the .gradle folder for Gradle), and later runs are fast.

Examples

Everyday Maven wrapper commands (macOS/Linux; use mvnw.cmd on Windows)

Java
./mvnw clean            # delete the target folder
./mvnw compile          # compile the code
./mvnw test             # run the unit tests
./mvnw package          # build target/<name>.jar
./mvnw spring-boot:run  # run a Spring Boot application
./mvnw dependency:tree  # show every library on the classpath

The same tasks with the Gradle wrapper

Java
./gradlew clean
./gradlew compileJava
./gradlew test
./gradlew build         # build build/libs/<name>.jar
./gradlew bootRun       # run a Spring Boot application
./gradlew dependencies  # show the dependency tree

Common Mistakes

  • Installing Maven or Gradle but forgetting the JDK. Both tools stop with an error if no JDK is found.
  • Mixing tools in one project: running mvn in a Gradle project or the reverse. Look for pom.xml or build.gradle to know which one you have.
  • Running mvn instead of ./mvnw and getting a different Maven version than your teammates.
  • On macOS and Linux, running ./mvnw when the script is not executable. Fix it with chmod +x mvnw.
  • Deleting the .m2 cache to "fix" a problem. It only forces a long re-download; read the error first.

Key Points to Remember

  • Use the wrapper (mvnw or gradlew) that Spring Initializr generates; it needs only a JDK.
  • Spring Boot 4.1 supports Maven 3.6.3 or later and Gradle 8.14+ or 9.x.
  • winget has no Maven or Gradle package; use the wrapper, Chocolatey, Scoop, SDKMAN, Homebrew or apt.
  • Maven is declarative XML; Gradle is a script. Pick one per project and stay consistent.

Practice the examples

Change an input, predict the result, then compare it with the output. Explain why the result changes.

Use your local JDK or project IDE for these examples. Codelab currently runs Python and HTML/CSS/JavaScript; framework examples may need project dependencies.