Introduction to Apache Maven: Core Concepts and Configuration

Understanding Maven

Apache Maven serves as a comprehensive project management and comprehension tool. It abstracts the project development and management process into a Project Object Model (POM), which is defined in the pom.xml file. This approach addresses common challenges in traditional project management, such as inconsistent library versions, compatibility issues, and complex upgrade procedures.

Core Functions

  • Project Building: Provides standardized, cross-platform automated build processes
  • Dependency Management: Efficiently manages project dependencies (jar packages) while preventing version conflicts
  • Unified Structure: Establishes consistent project organization across development teams

Installation and Setup

Maven requires Java to be installed and properly configured with JAVA_HOME. Download the binary archive from the official Apache Maven website, extract it, and configure the environment:

  1. Set MAVEN_HOME to point to the Maven installation directory
  2. Add %MAVEN_HOME%\bin to the system PATH
  3. Verify installation by running mvn -version in terminal

Repository Architecture

Maven repositories store project resources including plugins and dependencies. The repository system operates on three levels:

Local Repository

Located on the developer's machine, it caches downloaded artifacts. Default location is ~/.m2/repository, but can be customized in settings.xml:

<settings>
    <localRepository>D:/dev/maven-repo</localRepository>
</settings>

Remote Repository

  • Central Repository: Managed by the Maven community, contains open-source libraries
  • Private Repository (Mirror): Internal servers that proxy the central repository, improving download speeds and enabling storage of proprietary artifacts

To configure an Alibaba Cloud mirror for faster downloads in China:

<mirrors>
    <mirror>
        <id>aliyun-mirror</id>
        <mirrorOf>central</mirrorOf>
        <name>Aliyun Maven Mirror</name>
        <url>https://maven.aliyun.com/repository/public</url>
    </mirror>
</mirrors>

Maven Coordinates

Coordinates uniquely identify artifacts in the Maven ecosystem. They consist of three primary components:

Element Description Example
groupId Organization or company identifier com.example
artifactId Project or module name user-service
version Release version number 1.0.0

POM Configuration

The Project Object Model file defines project structure and configuration:

<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>
    
    <groupId>com.example.app</groupId>
    <artifactId>sample-application</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    
    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>5.9.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    
</project>

Build Commands

Maven provides lifecycle commands executed via the mvn command:

Command Description
mvn compile Compiles source code into target directory
mvn clean Removes the target directory
mvn test Executes unit tests and generates reports
mvn package Packages compiled code in to JAR/WAR file
mvn install Installs artifact to local repository

Project Generation

Create new projects using archetypes:

# Generate a Java application
mvn archetype:generate \
    -DgroupId=com.example \
    -DartifactId=demo-app \
    -DarchetypeArtifactId=maven-archetype-quickstart \
    -DinteractiveMode=false

# Generate a Web application
mvn archetype:generate \
    -DgroupId=com.example \
    -DartifactId=web-demo \
    -DarchetypeArtifactId=maven-archetype-webapp \
    -DinteractiveMode=false

Dependency Management

Dependency Scope

Dependencies can be scoped to specific build phases:

Scope Main Code Test Code Package
compile
provided
runtime
test

Transitive Dependencies

Dependencies are inherited transitively. Conflict resolution follows these rules:

  • Path Priority: Shorter dependency path takes precedence
  • Declaration Priority: First declared dependency wins at same depth

Optional Dependencies

Hide transitive dependencies from consumers:

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>2.0.7</version>
    <optional>true</optional>
</dependency>

Dependency Exclusion

Remove unwanted 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>

Build Lifecycle

Maven defines three distinct lifecycles:

Clean Lifecycle

  1. pre-clean: Executes pre-cleanup tasks
  2. clean: Removes all generated files
  3. post-clean: Executes post-cleanup tasks

Default Lifecycle

The primary build lifecycle includes phases executed in sequence:

  1. validate: Verifies project structure
  2. compile: Compiles source code
  3. test-compile: Compiles test code
  4. test: Runs unit tests
  5. package: Creates distributable archive
  6. install: Deploys to local repository
  7. deploy: Deploys to remote repository

Plugin Configuration

Plugins extend Maven functionality by binding to lifecycle phases:

Executable JAR Plugin

Configure the main class for executable JAR files:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.3.0</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>com.example.Application</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>

Tomcat Plugin for Web Applications

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.2</version>
            <configuration>
                <port>8080</port>
                <path>/myapp</path>
            </configuration>
        </plugin>
    </plugins>
</build>

Custom Plugin Execution

Bind plugins to specific lifecycle phases:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-source-plugin</artifactId>
            <version>3.2.1</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>jar-no-fork</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Tags: Maven Apache Maven POM Dependency Management Build Lifecycle

Posted on Sun, 23 Aug 2026 16:27:14 +0000 by Killswitch