Master Maven: Essential Concepts, Lifecycle, and Project Setup Guide
This comprehensive guide explains Maven's core concepts, project structure, repository types, installation steps, common commands, lifecycle phases, creating new Maven projects, POM file details, dependency management, and various Maven project types, providing developers with a complete reference for effective build automation.
Concept
Maven is a project management tool that includes a Project Object Model (POM), a standard collection, a project lifecycle, a dependency management system, and logic to run plugin goals defined in lifecycle phases.
1. Project Build Process
2. Maven Project Directory
Repository Types
Maven uses three types of repositories: local, central, and remote.
Local Repository
The local repository stores copies of plugins and JAR files downloaded by Maven. Default locations are ~/.m2/repository on Linux, C:\Documents and Settings\username\.m2\repository on Windows XP, and C:\Users\username\.m2\repository on Windows 7. Maven checks the local repository first before searching remote repositories. The path can be changed via the localRepository setting in %MAVEN_HOME%/conf/settings.xml. Running mvn install installs the project into the local repository.
Central Repository
The central repository, provided by the Maven community at http://repo1.maven.org/maven2, is queried for any dependencies not found locally, and the retrieved artifacts are cached in the local repository.
Remote Repository
Remote repositories host internal projects shared across multiple projects. They can reside on any web server, either on the Internet or within a private network, and are not publicly accessible. Dependencies from remote repositories are also downloaded into the local repository. Remote repositories are configured in the POM, for example:
<repositories>
<repository>
<id>jenkov.code</id>
<url>http://maven.jenkov.com/maven2/lib</url>
</repository>
</repositories>Installation
1. Install JDK. 2. Download the latest Maven binary zip from http://maven.apache.org/download.cgi and extract it. 3. Set environment variables: M2_HOME to the Maven directory and add M2_HOME\bin to PATH. 4. Verify installation with mvn –version. 5. Configure Eclipse: add Maven installation path, set local repository path, and configure VM parameters (e.g., -Dmaven.multiModuleProjectDirectory=$M2_HOME).
Common Commands
Lifecycle
Maven defines three built‑in lifecycles: clean (removes generated files), default (compiles and packages the project), and site (generates documentation). Each lifecycle consists of phases, which in turn consist of goals. Examples of phases include pre-clean, clean, post-clean, compile, test, package, install, site, etc.
Creating a New Maven Project
Steps: create a Maven project, select the project type, fill in project information, and run the project (right‑click → Run As → Maven build). If no goals are specified, add compile in the run configuration.
POM File
Differences between dependencyManagement and dependencies: dependencies are inherited by child modules automatically. dependencyManagement only declares versions; child modules must explicitly declare the dependency to inherit the version.
Example parent POM snippet:
<properties>
<javaee-api.version>1.0-SNAPSHOT</javaee-api.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>${javaee-api.version}</version>
</dependency>
</dependencies>
</dependencyManagement>Child POM snippet:
<parent>
<artifactId>parent</artifactId>
<groupId>com.zhisheng</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
</dependency>
</dependencies>Differences between pluginManagement and plugins: pluginManagement declares plugins for inheritance without applying them. plugins directly binds plugins to lifecycle phases.
Dependency Management
Maven resolves transitive dependencies automatically. Dependency scopes include compile, provided, runtime, test, system, and import. Exclusions and optional dependencies can be specified to control transitivity.
Example of adding a direct dependency:
<dependencies>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.7.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>External (system) dependencies can be added with a systemPath, or installed into the local repository using mvn install:install-file. Snapshot dependencies are indicated by a -SNAPSHOT suffix.
Maven Project Types
Three main types: WAR projects, JAR projects, and POM projects. Relationships include dependency, aggregation (using <modules> in a parent POM), and inheritance (parent POM provides shared configuration).
Source: http://www.uml.org.cn/xmgl/201802092.asp
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
ITFLY8 Architecture Home
ITFLY8 Architecture Home - focused on architecture knowledge sharing and exchange, covering project management and product design. Includes large-scale distributed website architecture (high performance, high availability, caching, message queues...), design patterns, architecture patterns, big data, project management (SCRUM, PMP, Prince2), product design, 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.
