What is Maven?

Java's most popular build automation and dependency management tool

← Back to Index

What is Maven?

Think of Maven like a smart project assistant:

Apache Maven is a build automation and project management tool that provides:

  • Dependency Management: Automatically downloads and manages libraries
  • Build Automation: Compile, test, package with one command
  • Project Structure: Standard directory layout for all projects
  • Plugin Ecosystem: Extensible through plugins

Why Use Maven?

Without Maven With Maven
Manually download each JAR file Declare dependency, Maven downloads it
Track version compatibility yourself Maven resolves transitive dependencies
Write custom build scripts Standard build lifecycle commands
Different project structures Consistent structure across all projects
Share JARs via email/drive Central repository for all libraries

Installing Maven

# Check if Maven is installed
mvn --version

# Output:
# Apache Maven 3.9.6
# Java version: 21.0.1
# OS name: "windows 10"

# Installation options:
# 1. Download from https://maven.apache.org/download.cgi
# 2. Use package manager:
#    - Windows: choco install maven
#    - Mac: brew install maven
#    - Linux: apt install maven
# 3. Most IDEs include Maven (IntelliJ, Eclipse)

Standard Project Structure

my-project/
├── pom.xml                          # Project configuration (THE key file)
├── src/
│   ├── main/
│   │   ├── java/                    # Java source code
│   │   │   └── com/example/
│   │   │       └── App.java
│   │   └── resources/               # Configuration files, properties
│   │       └── application.properties
│   └── test/
│       ├── java/                    # Test source code
│       │   └── com/example/
│       │       └── AppTest.java
│       └── resources/               # Test resources
└── target/                          # Build output (generated)
    ├── classes/                     # Compiled classes
    ├── test-classes/                # Compiled test classes
    └── my-project-1.0.0.jar         # Final artifact
Convention over Configuration

Maven uses conventions so you don't need to configure everything. Put source code in src/main/java, and Maven knows what to do!

Basic pom.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
         http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <!-- Project coordinates (unique identifier) -->
    <groupId>com.example</groupId>       <!-- Organization/company -->
    <artifactId>my-app</artifactId>     <!-- Project name -->
    <version>1.0.0</version>           <!-- Version -->
    <packaging>jar</packaging>         <!-- Output type: jar, war, pom -->

    <!-- Project metadata -->
    <name>My Application</name>
    <description>A sample Maven project</description>

    <!-- Properties (variables) -->
    <properties>
        <java.version>21</java.version>
        <maven.compiler.source>${java.version}</maven.compiler.source>
        <maven.compiler.target>${java.version}</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <!-- Dependencies (libraries your project needs) -->
    <dependencies>
        <!-- Example: JUnit for testing -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>5.10.0</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

Common Maven Commands

# Clean - removes target directory
mvn clean

# Compile - compiles source code
mvn compile

# Test - runs unit tests
mvn test

# Package - creates JAR/WAR file
mvn package

# Install - installs to local repository (~/.m2/repository)
mvn install

# Combined commands (most common)
mvn clean install          # Clean, compile, test, package, install
mvn clean package          # Clean, compile, test, package
mvn clean test             # Clean, compile, test

# Skip tests (use sparingly!)
mvn package -DskipTests

# Run with specific profile
mvn package -Pprod

# Show dependency tree
mvn dependency:tree

# Check for dependency updates
mvn versions:display-dependency-updates
Command What It Does
mvn clean Deletes target/ directory
mvn compile Compiles src/main/java to target/classes
mvn test Runs tests in src/test/java
mvn package Creates JAR/WAR in target/
mvn install Copies artifact to ~/.m2/repository
mvn deploy Uploads to remote repository

Maven Repositories

// How Maven finds dependencies:

// 1. Local Repository (your machine)
//    ~/.m2/repository/
//    - Caches downloaded dependencies
//    - Stores your installed projects

// 2. Central Repository (default remote)
//    https://repo.maven.apache.org/maven2/
//    - Hosts millions of open source libraries
//    - Automatically searched if not in local

// 3. Custom/Private Repositories
//    - Company internal repositories
//    - Configured in pom.xml or settings.xml

Adding Custom Repository

<repositories>
    <repository>
        <id>company-repo</id>
        <url>https://nexus.company.com/repository/maven-public/</url>
    </repository>
</repositories>

Creating a New Maven Project

# Using archetype (project template)
mvn archetype:generate \
    -DgroupId=com.example \
    -DartifactId=my-app \
    -DarchetypeArtifactId=maven-archetype-quickstart \
    -DinteractiveMode=false

# Common archetypes:
# maven-archetype-quickstart  - Simple Java project
# maven-archetype-webapp      - Web application (WAR)

# Or use Spring Initializr for Spring Boot projects:
# https://start.spring.io

Maven vs Other Build Tools

Feature Maven Gradle Ant
Configuration XML (pom.xml) Groovy/Kotlin DSL XML (build.xml)
Convention Strong conventions Flexible No conventions
Learning Curve Medium Steeper Low
Build Speed Good Better (incremental) Manual optimization
IDE Support Excellent Excellent Good
Popularity Most popular Growing fast Legacy

Best Practices

DO:

  • Use properties for versions - Easy to update
  • Follow standard directory structure - Convention over configuration
  • Use dependency management - Parent POM for multi-module
  • Run mvn dependency:tree - Understand your dependencies
  • Use .mvn/maven.config - Project-specific Maven options
  • Specify encoding - UTF-8 in properties

DON'T:

  • Don't commit target/ - Add to .gitignore
  • Don't hardcode versions everywhere - Use properties
  • Don't skip tests in CI - Only locally when needed
  • Don't use SNAPSHOT in production - Use release versions
  • Don't ignore dependency conflicts - Resolve them explicitly

Summary

  • Maven: Build automation and dependency management tool
  • pom.xml: Project Object Model - the configuration file
  • Coordinates: groupId:artifactId:version uniquely identify artifacts
  • Dependencies: Libraries your project needs
  • Lifecycle: clean, compile, test, package, install, deploy
  • Repository: Where Maven finds/stores dependencies
  • Convention: Standard project structure