Maven is the most widely used build and dependency management tool for Java projects. This guide will take you from Maven basics to advanced concepts, helping you become a Maven expert.

Table of Contents

  1. Maven Fundamentals
  2. Project Object Model (POM)
  3. Maven Build Lifecycle
  4. Dependency Management
  5. Plugins & Customization
  6. Multi-Module Projects
  7. Advanced Maven
  8. Best Practices
  9. Maven vs. Gradle
  10. Resources

1. Maven Fundamentals

What is Maven?

  • A build automation tool for Java (and other JVM languages).
  • Manages dependencies, compilation, testing, packaging, and deployment.
  • Uses XML-based configuration (pom.xml).

Key Features

Standardized project structure
Dependency management (automatic downloads from Maven Central)
Build lifecycle management (clean, compile, test, package, install, deploy)
Extensible via plugins

Installation

# Linux/macOS
brew install maven  # or `sudo apt install maven`

# Windows (via Chocolatey)
choco install maven

# Verify
mvn -v

2. Project Object Model (POM)

The pom.xml file defines the project structure, dependencies, and build configuration.

Basic POM Structure

<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mycompany</groupId>
    <artifactId>my-app</artifactId>
    <version>1.0.0</version>
    <packaging>jar</packaging> <!-- or 'war', 'pom' -->

    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>5.9.0</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.11.0</version>
                <configuration>
                    <source>17</source>
                    <target>17</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Key POM Elements

TagPurpose
<groupId>Organization identifier (e.g., com.company)
<artifactId>Project name (e.g., my-app)
<version>Release version (e.g., 1.0.0-SNAPSHOT)
<packaging>jar, war, pom, etc.
<dependencies>External libraries
<build>Plugins and build settings

3. Maven Build Lifecycle

Maven follows a fixed build lifecycle with phases:

PhaseDescription
validateChecks project correctness
compileCompiles source code
testRuns unit tests
packageCreates JAR/WAR
verifyRuns integration tests
installInstalls artifact in local .m2 repo
deployUploads to remote repo

Common Commands

mvn clean          # Deletes `target/`
mvn compile        # Compiles code
mvn test           # Runs tests
mvn package        # Creates JAR/WAR
mvn install        # Installs in local repo
mvn deploy         # Deploys to remote repo

4. Dependency Management

Dependency Scope

ScopeDescription
compileDefault (included in all phases)
providedNeeded for compilation but not packaged (e.g., servlet-api)
runtimeNeeded at runtime but not compile-time
testOnly for testing (e.g., JUnit)
systemLocal JAR (avoid if possible)

Excluding Transitive Dependencies

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>6.0.0</version>
    <exclusions>
        <exclusion>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
        </exclusion>
    </exclusions>
</dependency>

BOM (Bill of Materials)

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>3.1.0</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

5. Plugins & Customization

Essential Plugins

PluginPurpose
maven-compiler-pluginSets Java version
maven-surefire-pluginRuns tests
maven-jar-pluginCustomizes JAR manifest
maven-shade-pluginCreates fat JARs
maven-assembly-pluginBuilds ZIP/TAR distributions

Example: Fat JAR with Shade Plugin

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.5.0</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
        </execution>
    </executions>
</plugin>

6. Multi-Module Projects

Parent POM (pom.xml)

<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mycompany</groupId>
    <artifactId>parent-project</artifactId>
    <version>1.0.0</version>
    <packaging>pom</packaging>

    <modules>
        <module>module1</module>
        <module>module2</module>
    </modules>
</project>

Child Module (module1/pom.xml)

<project>
    <parent>
        <groupId>com.mycompany</groupId>
        <artifactId>parent-project</artifactId>
        <version>1.0.0</version>
    </parent>
    <artifactId>module1</artifactId>
</project>

Building Multi-Module Projects

mvn clean install  # Builds all modules

7. Advanced Maven

Profiles

<profiles>
    <profile>
        <id>dev</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <env>development</env>
        </properties>
    </profile>
</profiles>

Run with:

mvn -Pdev package

Custom Properties

<properties>
    <java.version>17</java.version>
</properties>

Maven Wrapper

Generate mvnw (Maven Wrapper):

mvn wrapper:wrapper

Then use:

./mvnw clean install  # No Maven installation needed!

8. Best Practices

Use dependencyManagement for version control
Avoid system scope (use local repo instead)
Use Maven Wrapper (mvnw) for reproducible builds
Keep POM clean (avoid unnecessary plugins)
Use CI/CD integration (Jenkins, GitHub Actions)


9. Maven vs. Gradle

FeatureMavenGradle
Config LanguageXMLGroovy/Kotlin DSL
PerformanceSlower (XML parsing)Faster (incremental builds)
FlexibilityStrict conventionsHighly customizable
Learning CurveSimplerSteeper

When to Use Which?

  • Maven: Standard projects, enterprise environments.
  • Gradle: Custom builds, Android, performance-critical projects.

10. Resources