Maven Fundamentals: Setup, Concepts, and Project Management

Introduction to Maven

Maven is a project management tool that abstracts development processes into a Project Object Model (POM). It addresses common issues like jar version conflicts and complex project upgrades by providing standardized dependency management and build automation.

Key Functions

  • Build Automation: Standardized cross-platform project building
  • Dependency Management: Resolves version conflicts and manages resources
  • Standard Structure: Defines consistent project directories:
    • src/main/java - Application source code
    • src/main/resources - Configuration files
    • src/test/java - Test source code

Environment Setup

Installation

  1. Download from official site
  2. Extract archive to installation directory (e.g., D:\apache-maven)

Configuration

  1. Set MAVEN_HOME environment varible to installation path
  2. Add %MAVEN_HOME%\bin to system PATH
  3. Verify with terminal: mvn -v

Core Concepts

Repositories

  • Local: Developer machine cache (~/.m2/repository)
  • Central: Public repository hosted by Apache
  • Private: Organization-specific repository

Coordinates

Uniquely identify resources in repositories:

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-core</artifactId>
  <version>5.3.8</version>
</dependency>

Repository Configuration

Modify conf/settings.xml:

<localRepository>D:/custom-repo</localRepository>
<mirror>
  <id>aliyun-mirror</id>
  <url>https://maven.aliyun.com/repository/public</url>
  <mirrorOf>central</mirrorOf>
</mirror>

Project Creation

Manual Setup

  1. Create standard directory structure
  2. Add sample class: ``` package com.example; public class Sample { public String greet(String name) { return "Hello " + name; } }
  3. Create test class: ``` package com.example; import org.junit.Test; public class SampleTest { @Test public void testGreet() { Sample s = new Sample(); Assert.assertEquals("Hello world", s.greet("world")); } }
  4. Configure pom.xml: ``` <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>demo-app</artifactId> 1.0.0 <groupId>junit</groupId> <artifactId>junit</artifactId> 4.13.2
    
    

IntelliJ Setup

  1. Create new project: File → New → Project → Maven
  2. Configure cooordinates in POM
  3. Add dependencies in <dependencies> section
  4. For web projects: ``` war <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> 2.2
    
    

Dependency Management

Transitivity Rules

  • Path Priority: Closer dependencies override distant ones
  • Declaration Order: First declarasion takes precedence

Scopes

Scope Main Test Package
compile
test
provided

Lifecycle and Plugins

Build Lifecycles

  • clean: Removes build artifacts
  • default: Core build process (compile, test, package)
  • site: Generates project documentation

Plugin Configuration

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>3.8.1</version>
      <configuration>
        <source>1.8</source>
        <target>1.8</target>
      </configuration>
    </plugin>
  </plugins>
</build>

Tags: Maven Apache Maven POM Dependency Management Build Lifecycle

Posted on Wed, 23 Sep 2026 16:48:30 +0000 by masson