Comprehensive Maven Tutorial: Installation, Configuration, Lifecycle, Commands, and Eclipse Integration
This article provides a detailed, step‑by‑step guide to Maven, covering why to use it, its core concepts, installation, project structure, pom.xml configuration, common commands, dependency scopes, repository handling, lifecycle phases, Eclipse setup, advanced features, and build customization for Java backend development.
Why use Maven – Maven solves problems such as managing large projects as multiple modules, avoiding manual copying of JAR files, handling version conflicts, and automatically resolving transitive dependencies.
What Maven is – Maven is an automated build tool for the Java platform that defines a standard project structure and a set of lifecycle phases (clean, default, site) to compile, test, package, and deploy code.
Installation – Ensure JAVA_HOME is set, download Maven, extract it to a non‑space path, add M2_HOME and .../bin to the system PATH, then verify with mvn -v .
First Maven project – Create the conventional directory layout (src/main/java, src/main/resources, src/test/java, etc.) and add a simple Hello.java class and a minimal pom.xml that declares groupId, artifactId, version, and a JUnit test dependency.
Common Maven commands – mvn clean , mvn compile , mvn test-compile , mvn test , mvn package , mvn install , and mvn deploy must be run from the directory containing pom.xml .
Repository and coordinates – Maven stores artifacts in a local repository (default ~/.m2/repository ) and can fetch missing dependencies from remote repositories such as Maven Central. An artifact is uniquely identified by groupId:artifactId:version .
Dependency scopes – The scopes compile , provided , runtime , test , and system control when a dependency is available during build, test, or runtime.
Lifecycle phases – Maven defines three independent lifecycles: Clean (pre‑clean, clean, post‑clean), Default (validate, compile, test, package, install, deploy, etc.), and Site (pre‑site, site, post‑site, site‑deploy).
Eclipse integration – Configure Maven in Eclipse via Window → Preferences → Maven → Installations and set the settings.xml file for the local repository. Create Maven projects through the New Project wizard and adjust Java Build Path and Project Facets as needed.
Advanced dependency features – Maven supports transitive dependencies, version conflict resolution (shortest path wins, first declaration wins), and centralized version management using <properties> in the POM.
Build configuration – The <build> section of the POM can customize the final artifact name, resource filtering, and plugins such as maven-compiler-plugin , maven-resources-plugin , and maven-war-plugin with specific compiler versions and encoding.
After completing these steps, running mvn package produces the expected JAR/WAR files, and the article concludes with a link to the Maven Central repository search site ( http://mvnrepository.com/ ).
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
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.