Maven / Gradle for Java: Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction: What are Maven and Gradle and why are they needed?



In the world of Java development, there is a fundamental problem: how to manage dependencies (external libraries), automate project builds, run tests, and create final artifacts (JAR, WAR)? Manually downloading JAR files and configuring the classpath is a dead end, especially for projects with dozens of dependencies.



Maven and Gradle are the two most popular build automation tools for Java. They solve the following tasks:

  • Dependency management: automatic downloading of libraries from central repositories (Maven Central, Google Maven) and their transitive dependencies.
  • Lifecycle standardization: compilation, testing, packaging, deployment — all performed with standard commands.
  • Build reproducibility: any developer can build a project with a single command, having only the source code and configuration file.
  • CI/CD integration: Jenkins, GitLab CI, GitHub Actions easily run Maven/Gradle builds.


Maven uses XML (the pom.xml file) and a strict lifecycle. Gradle uses Groovy or Kotlin DSL (the build.gradle or build.gradle.kts file), it is more flexible and faster due to incremental builds. In this article, we will look at both tools, but focus on Gradle as a more modern solution.



Installing Maven and Gradle



Installing Maven

Maven is not installed via pip or npm. It needs to be downloaded from the official website or installed via the OS package manager.

# macOS (Homebrew)brew install maven

# Ubuntu/Debiansudo apt-get updatesudo apt-get install maven

# Windows (Chocolatey)choco install maven

# Verify installationmvn --version


Installing Gradle

Gradle is also installed separately. It is recommended to use SDKMAN! for version management.

# Install SDKMAN (if not already installed)curl -s "https://get.sdkman.io" | bashsource "$HOME/.sdkman/bin/sdkman-init.sh"

# Install the latest version of Gradlesdk install gradle

# Verifygradle --version

# Alternatively: download from https://gradle.org/releases/ and unzip


Quick Start: Creating a Project



Creating a project with Maven

Maven can generate a project skeleton using archetypes.

# Create a simple Java projectmvn archetype:generate -DgroupId=com.example -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

# Navigate to the project foldercd my-app

# Build the projectmvn clean package

# Run (if main class is configured)java -cp target/my-app-1.0-SNAPSHOT.jar com.example.App


Creating a project with Gradle

Gradle uses the init command to create a skeleton.

# Create a folder and initialize the projectmkdir my-gradle-appcd my-gradle-appgradle init

# Choose: 2: application -> 3: Java -> 1: Groovy DSL -> 2: JUnit Jupiter -> Enter

# Build and run./gradlew build./gradlew run


After this, you will have a project structure with build.gradle (or pom.xml for Maven) files and src/main/java and src/test/java folders.



Main Methods/Functions (Gradle DSL)

Let's look at the key Gradle constructs (Groovy DSL). For Maven they are similar, but with XML syntax.



1. dependencies { ... }

What it does: Defines the project's dependencies — external libraries that will be downloaded from repositories.

Parameters: Inside the block, configurations (implementation, testImplementation, compileOnly, etc.) and coordinates are specified.

Recommendations