When you're building an application, sooner or later you'll need libraries — whether it's Spring Framework, Logback for logging, or something exotic like a graph-processing library. Manually searching, downloading, adding, and updating those libraries can turn your development into a tedious nightmare. There's also a risk of running into incompatible library versions.
The logical question arises: how do you automate dependency management and project builds?
This is where Maven or Gradle come in. These tools:
- Simplify adding dependencies.
- Provide convenient version management for libraries.
- Let you configure the project build process.
- Help resolve compatibility issues.
Maven: meet the granddaddy of Java builds
Maven is a build tool for Java projects, designed to standardize and automate the build process. It uses XML files (pom.xml) for configuration and dependency management.
Main responsibilities of Maven:
- Managing dependencies via repositories.
- Compiling and building the project.
- Generating documentation.
- Running tests and producing reports.
Structure of the pom.xml file
Maven uses a pom.xml (Project Object Model) file that describes the project and its dependencies. Here's a simple example of a pom.xml for an app using Spring Boot:
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- Main project info -->
<groupId>com.example</groupId> <!-- Unique project identifier -->
<artifactId>spring-demo</artifactId> <!-- Artifact name -->
<version>1.0-SNAPSHOT</version> <!-- Version -->
<!-- Project dependencies -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
</dependencies>
<!-- Build plugins -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
If pom.xml feels a bit intimidating, don't worry! With time it'll become as familiar as your favorite keyboard layout — natural and intuitive.
Dependency management
The secret sauce of Maven is centralized dependency management via repositories:
- Maven Central repository (https://repo.maven.apache.org/maven2) — the main source of libraries.
- Local repository — a directory on your machine where Maven stores downloaded libraries.
- Corporate/custom repository — e.g., Nexus or Artifactory.
When you add a dependency to pom.xml, Maven: 1. Checks if the library exists in the local repository. 2. If not, downloads it from the Central repository. 3. Cheers you up because everything just worked 😊.
Gradle: the modern challenger to Maven
Gradle is a younger, more flexible build tool created to solve the same problems as Maven. Unlike Maven, Gradle uses a more concise configuration based on Groovy or Kotlin DSL, which makes it popular among modern developers.
Main advantages of Gradle:
- Flexibility: you can fine-tune the build process.
- Speed: thanks to
incremental buildsand thedaemon, builds are significantly faster. - Clearer syntax (especially if XML gives you hives).
Structure of the build.gradle file
Here's what a similar configuration for a Spring app looks like in Gradle:
plugins {
id 'org.springframework.boot' version '3.0.0'
id 'io.spring.dependency-management' version '1.0.15.RELEASE'
id 'java'
}
group = 'com.example'
version = '1.0-SNAPSHOT'
sourceCompatibility = '17'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter:3.0.0'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
As you can see, Gradle's syntax is more concise and easier to digest, especially if you're just starting out with build tools.
Maven vs Gradle: which one to pick?
| Characteristic | Maven | Gradle |
|---|---|---|
| Flexibility | Rigid XML structure | High flexibility via DSL |
| Build speed | Slower | Faster thanks to incremental builds and cache |
| Learning curve | Slightly higher | Concise and clear syntax |
| Popularity | Very popular, especially in legacy projects | Gaining traction in modern projects |
| Support | Wide, lots of documentation | Great fit for new applications |
If you're working with Spring Boot projects and like simplicity, Gradle might be your pick. But if you're maintaining older projects or working in a team that already uses Maven, it's totally fine to stick with it too.
Practice: setting up a Spring Boot project with Maven and Gradle
Maven
- Create a new project via Spring Initializr: https://start.spring.io.
- Select Maven and dependencies like
Spring Web. - Import the project into your favorite IDE and run it.
- Open
pom.xml, add one more dependency (for example,Lombok):<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.28</version> </dependency> - Refresh the project and make sure everything runs.
Gradle
- Create a project via Spring Initializr, selecting Gradle.
- Import the project into your IDE.
- Open
build.gradleand add theLombokdependency:dependencies { implementation 'org.projectlombok:lombok:1.18.28' } - Click the
Refresh Gradle Projectbutton in your IDE and run the app.
Links to official documentation
The world of Maven and Gradle will open up a ton of possibilities for automating and simplifying your project workflow. In the next lecture we'll dive deeper into Spring Boot application structure and learn how to manage their configuration. Let's go! 🚀
GO TO FULL VERSION