Master Maven: From Installation to Advanced Dependency Management
This comprehensive guide explains why Maven is essential for Java projects, details its core concepts, walks through installation, creates a first Maven project, covers common commands, repository layout, dependency scopes, lifecycle phases, Eclipse integration, and advanced features like transitive dependencies and version management.
Currently all projects use Maven, but I haven't had time to study it; now I have time to organize it properly.
1. Why use a build tool like Maven
① A project is an engineering
If a project is very large, splitting it into packages is not suitable; it is better to have each module correspond to a separate engineering to facilitate collaboration. Maven can split a project into multiple engineering.
② JAR packages need to be copied into the project's lib
Without Maven, the same JAR appears in many projects, requiring repetitive copy‑paste. Maven stores JARs in a repository, and projects reference them directly.
③ JARs must be manually downloaded each time
Maven provides a unified way to download JARs according to a standard.
④ Risk of inconsistent JAR versions
Different projects may use different versions of the same JAR, causing runtime errors. Maven centralizes JARs in a repository, ensuring all projects use the same version.
⑤ Manual handling of transitive dependencies
When a JAR depends on another JAR (e.g., commons‑fileupload depends on commons‑io), developers must add each dependency manually, wasting time. Maven resolves and imports transitive dependencies automatically.
2. What is Maven
① Maven is an automation build tool for the Java platform
make → Ant → Maven → Gradle
The name can be pronounced "Maven" or "Mei‑wen".
② Build definition
Build means compiling the source code, producing bytecode, and deploying the result to a server.
Compilation: .java → .class
Deployment: the compiled files are placed in the servlet container.
③ Build phases
clean: delete previous compiled class files
compile: compile Java source to class files
test: run automated tests (e.g., JUnit)
report: generate test reports
package: create a WAR for web projects or a JAR for Java projects
install: copy the packaged artifact to the local repository
deploy: copy the artifact to a remote repository for sharing
3. Installing Maven
① Ensure the JAVA_HOME environment variable is set.
② Download Maven and extract it to a path without spaces or Chinese characters.
③ Add Maven to the system PATH:
Add M2_HOME pointing to the Maven root directory.
Add %M2_HOME%\bin to the PATH variable.
④ Verify installation with mvn -v and you should see the Maven version.
4. Your first Maven project
① Create the standard Maven directory structure (must follow the convention):
project-root/ |---src/ |---|---main/ (main source code) |---|---|---java/ (Java source files) |---|---|---resources/ (configuration files) |---test/ (test source code) |---pom.xml (core Maven configuration)
Example Java class Hello.java:
package com.hzg.maven;
public class Hello {
public String sayHello(String name) {
return "Hello " + name + "!";
}
}POM file (pom.xml) example:
<?xml version="1.0" encoding="UTF-8"?>
<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>
<name>Hello</name>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>Common Maven commands: mvn clean: clean previous build output mvn compile: compile main source code mvn test-compile: compile test source code mvn test: run tests mvn package: package the compiled code mvn install: install the package to the local repository
All commands must be executed from the directory containing pom.xml.
5. Repository and coordinates
pom.xml is the Project Object Model, the core configuration file for Maven.
Coordinates consist of groupId, artifactId, and version, uniquely identifying an artifact in the repository.
Local repository default location: C:\Users\<username>\.m2\repository.
6. Dependency scopes
compile : default, available in all phases.
provided : only for compile and test, not packaged (e.g., servlet API).
runtime : needed at runtime (e.g., JDBC driver).
test : only for testing, not included in the final artifact.
system : similar to provided but requires an explicit path; Maven does not search the repository.
7. Maven lifecycle
Maven defines three independent lifecycles:
Clean lifecycle : pre‑clean, clean, post‑clean.
Default lifecycle : validate, generate‑sources, process‑sources, generate‑resources, process‑resources, compile, process‑classes, generate‑test‑sources, process‑test‑sources, generate‑test‑resources, process‑test‑resources, test‑compile, test, prepare‑package, package, pre‑integration‑test, integration‑test, post‑integration‑test, verify, install, deploy.
Site lifecycle : 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: Preferences → Maven → Installations → Add the extracted Maven directory and apply.
② Set the user settings file (settings.xml) to configure the local repository path if needed.
③ Create a Maven Web project via File → New → Maven Project, follow the wizard, and finish.
④ Adjust Java Build Path, Project Facets (e.g., set Java version to 1.8), and add required libraries such as Tomcat.
9. Advanced dependency features
Transitive dependencies : when a dependency brings in its own dependencies, Maven resolves them automatically. Only dependencies with compile scope are transitive.
Version conflict resolution follows two principles:
Shortest path wins.
If paths are equal, the first declared dependency wins.
Using <properties> in the POM allows central management of version numbers.
10. Build configuration
<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 generates the WAR file and related resources in the target directory.
For a comprehensive list of Maven artifact versions, visit mvnrepository.com .
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.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
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.
