Backend Development 16 min read

Comprehensive Guide to Maven: Installation, Configuration, Commands, and Lifecycle

This article provides a detailed introduction to Maven, covering why to use it, its core concepts, installation steps, project structure, essential commands, dependency management, lifecycle phases, and integration with Eclipse, offering practical code examples and configuration snippets for Java developers.

Selected Java Interview Questions
Selected Java Interview Questions
Selected Java Interview Questions
Comprehensive Guide to Maven: Installation, Configuration, Commands, and Lifecycle

1. Why Use Maven

Maven helps split large projects into multiple modules, avoids repetitive copying of JAR files, provides a central repository for dependencies, ensures consistent versions across projects, and automatically resolves transitive dependencies.

2. What Is Maven

Maven is an automated build tool for the Java platform. It defines a build process that compiles source code, packages the result, and deploys it to a server. The basic build steps are compile → test → package → install → deploy.

3. Installing Maven

Ensure JAVA_HOME is set, download Maven, extract it to a directory without spaces, add M2_HOME and %M2_HOME%\bin to the system PATH , and verify the installation with mvn -v .

4. First Maven Project

Create the standard Maven directory layout:

project-name/
 ├─ src/
 │   ├─ main/
 │   │   ├─ java/
 │   │   └─ resources/
 │   └─ test/
 └─ pom.xml

Example Hello.java :

package com.hzg.maven;
public class Hello {
    public String sayHello(String name) {
        return "Hello " + name + "!";
    }
}

Example pom.xml (excerpt):

<project xmlns="http://maven.apache.org/POM/4.0.0" ...>
    <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>

5. Common Maven Commands

mvn clean – clean previous build output

mvn compile – compile source code

mvn test-compile – compile test sources

mvn test – run unit tests

mvn package – package the compiled code

mvn install – install the artifact into the local repository

All commands must be executed from the directory containing pom.xml .

6. Repository and Coordinates

The pom.xml is the Project Object Model that defines a Maven project. An artifact is uniquely identified by the three coordinates groupId , artifactId , and version , which map to a path in the local or remote repository (e.g., groupId/artifactId/version/artifactId-version.jar ).

Repositories can be local (default ~/.m2/repository ) or remote (central repository or a private Nexus server).

7. Maven Lifecycle

Maven defines three independent lifecycles:

Clean : pre-clean → clean → post-clean

Default : validate, generate-sources, compile, test, package, install, deploy, etc.

Site : pre-site → site → post-site → site-deploy

Each phase is bound to a plugin that performs the actual work.

8. Using Maven in Eclipse

Configure Maven in Eclipse via Window → Preferences → Maven → Installations and add the extracted Maven directory. Set the settings.xml file to customize the local repository:

<localRepository>C:\Program Files\Java\repository</localRepository>

Create a Maven web project through File → New → Maven Project , choose the appropriate archetype, and adjust the Java Build Path, Project Facets, and library dependencies (e.g., Tomcat) as needed.

9. Advanced Dependency Features

Transitive dependencies are automatically pulled in; however, only dependencies with compile scope are transitive. Maven resolves version conflicts using the “shortest path” rule, and if paths are equal, the first declaration wins. Dependency management can be centralized using <properties> and <dependencyManagement> sections.

10. 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-war-plugin</artifactId>
            <version>2.1</version>
            <configuration>
                <warName>WebMavenDemo1</warName>
            </configuration>
        </plugin>
    </plugins>
</build>

Running mvn package generates the configured WAR file in the target directory.

The article concludes with a link to Maven Repository for searching dependency versions.

JavaBuild Tooldependency managementmavenlifecycleEclipse
Selected Java Interview Questions
Written by

Selected Java Interview Questions

A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.