Maven: Dependency and Build Management

Introduction

Maven automates Java builds: compile, test, package JARs, and manage dependencies (libraries like Gson, JUnit). Real teams use pom.xml to keep projects reproducible. This chapter covers a minimal Maven project aligned with JDK 21 from install chapters and Java 21 Features.

Prerequisites

  • JDK 21 installed
  • Command line basics
  • Optional: JSON Handling Gson example

Install Maven

Download from maven.apache.org or use package managers:

bash
# macOS (Homebrew)
brew install maven
 
mvn -version

Verify Maven uses Java 21 in output.

1) Standard Directory Layout

text
my-app/
  pom.xml
  src/main/java/...        # application code
  src/main/resources/      # config files
  src/test/java/...        # tests

Maven conventions let tools and CI understand projects instantly.

2) Minimal pom.xml

xml
<?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.hellocode</groupId>
    <artifactId>learn-java</artifactId>
    <version>1.0.0-SNAPSHOT</version>
 
    <properties>
        <maven.compiler.release>21</maven.compiler.release>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
 
    <dependencies>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.11.0</version>
        </dependency>
 
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>5.10.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
 
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.2.5</version>
            </plugin>
        </plugins>
    </build>
</project>

maven.compiler.release 21 enables Java 21 language features.

3) Common Commands

bash
mvn compile          # compile main sources
mvn test             # run tests
mvn package          # build JAR in target/
mvn clean package    # clean then package

4) JUnit Example with @Test

Annotations (Annotations) in tests:

java
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
 
class CalculatorTest {
 
    @Test
    void addsNumbers() {
        assertEquals(8, 3 + 5);
    }
}

Place under src/test/java. Run with mvn test.

5) Coordinates Explained

groupId:artifactId:version identifies dependencies in Maven Central.

IntelliJ can import Maven projects directly (Open → select pom.xml).

Common Beginner Mistakes

Wrong Java Version in pom.xml

Symptoms: cannot use record or new syntax—set release to 21.

Dependencies Without Refresh

After editing pom.xml, reload Maven project in IDE.

Putting Source in Wrong Folder

src/main/java only—not project root.

What’s Next

Gradle alternative, then Books.

FAQ

Maven vs Gradle?

Maven: XML, convention-heavy. Gradle: flexible Kotlin/Groovy DSL, popular on Android. Both fine—learn one deeply first.

Where do JARs download?

Local ~/.m2/repository cache.

How to run main class?

exec-maven-plugin or package fat JAR—advanced; IntelliJ run is enough while learning.

Spring Boot?

Builds on Maven/Gradle with starters—beyond this tutorial’s scope.

Compiler release must match installed JDK major version.