Comprehensive Maven Guide: Installation, Configuration, Commands, Lifecycle, and Advanced Features
This article provides a thorough introduction to Maven, covering why to use it, its core concepts, installation steps, creating the first project, repository management, dependency scopes, lifecycle phases, Eclipse integration, advanced dependency features, and build configuration with practical code examples.
Maven is a powerful build automation tool for Java projects that simplifies project modularization, dependency management, and reproducible builds.
Why use Maven? It allows large projects to be split into multiple modules, eliminates manual copying of JAR files by storing them in a central repository, ensures consistent versions across modules, and automatically resolves transitive dependencies.
What is Maven? Maven follows the evolution chain make → Ant → Maven → Gradle . It defines a Project Object Model (POM) file ( pom.xml ) that describes the project structure, dependencies, plugins, and build lifecycle.
Installation
1. Verify that JAVA_HOME is set. 2. Download Maven, extract it to a non‑Chinese, space‑free directory. 3. Add M2_HOME (pointing to the Maven root) and Maven\bin to the system PATH . 4. Run mvn -v to confirm the installation.
First Maven Project
Create the standard directory layout:
project-root/
├─ src/main/java
├─ src/main/resources
├─ src/test/java
├─ pom.xmlAdd a simple Java class src/main/java/com/hzg/maven/Hello.java :
package com.hzg.maven;
public class Hello {
public String sayHello(String name) {
return "Hello " + name + "!";
}
}Sample pom.xml :
<?xml version="1.0"?>
<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.hzg.maven</groupId>
<artifactId>Hello</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>Common Maven Commands
Run these commands inside the directory containing pom.xml :
mvn clean – removes previous build output.
mvn compile – compiles main sources.
mvn test-compile – compiles test sources.
mvn test – runs unit tests.
mvn package – packages the compiled code (e.g., JAR or WAR).
mvn install – installs the artifact into the local repository.
The default local repository resides at C:\Users\<username>\.m2\repository . After a successful mvn install , the generated JAR appears there following the coordinate path groupId/artifactId/version/artifactId-version.jar .
Repository and Coordinates
Artifacts are uniquely identified by three coordinates: groupId , artifactId , and version . Maven first looks in the local repository; if missing, it fetches from remote repositories such as Maven Central or a private Nexus server.
Dependency Scopes
Scopes control when a dependency is available:
compile – default, available in all phases.
provided – needed for compilation but supplied by the runtime container.
runtime – required only at execution time (e.g., JDBC drivers).
test – only for testing.
system – similar to provided but requires an explicit path.
Lifecycle
Maven defines three independent lifecycles:
Clean – pre-clean , clean , post-clean .
Default – the core build phases from validate to deploy (including compile, test, package, install, etc.).
Site – generates project documentation and site deployment.
When you invoke a phase (e.g., mvn install ), Maven executes all preceding phases in that lifecycle.
Using Maven in Eclipse
Configure Maven in Eclipse via Window → Preferences → Maven → Installations to point to your extracted Maven directory, and set the settings.xml (e.g., to change the local repository path):
<localRepository>C:\Program Files\Java\repository</localRepository>Create a Maven Web project through the wizard, then adjust the Java Build Path, Project Facets (e.g., Dynamic Web Module 3.1, Java 1.8), and add required libraries such as Tomcat.
Advanced Dependency Features
Transitive dependencies are automatically included unless the scope prevents it. Version conflicts are resolved by the "shortest path" rule, and if paths are equal, the first declaration in the pom.xml wins. You can centralize version numbers using <properties> and reference them with ${propertyName} .
Build Configuration Example
<build>
<finalName>WebMavenDemo</finalName>
<resources>
<resource>
<directory>src/main/java</directory>
<includes><include>**/*.xml</include></includes>
<excludes><exclude>**/*.txt</exclude><exclude>**/*.doc</exclude></excludes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<phase>compile</phase>
</execution>
</executions>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1</version>
<configuration>
<warName>WebMavenDemo1</warName>
</configuration>
</plugin>
</plugins>
</build>After configuring the build, running mvn package produces the expected WAR/JAR files in the target directory.
Overall, this guide walks you through the entire Maven workflow—from installation to advanced dependency management—providing the essential knowledge to efficiently manage Java projects.
Architect's Tech Stack
Java backend, microservices, distributed systems, containerized programming, and more.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.