Maven is the most widely used build and dependency management tool for Java projects. This guide will take you from Maven basics to advanced concepts, helping you become a Maven expert.
Table of Contents
- Maven Fundamentals
- Project Object Model (POM)
- Maven Build Lifecycle
- Dependency Management
- Plugins & Customization
- Multi-Module Projects
- Advanced Maven
- Best Practices
- Maven vs. Gradle
- Resources
1. Maven Fundamentals
What is Maven?
- A build automation tool for Java (and other JVM languages).
- Manages dependencies, compilation, testing, packaging, and deployment.
- Uses XML-based configuration (
pom.xml
).
Key Features
✔ Standardized project structure
✔ Dependency management (automatic downloads from Maven Central)
✔ Build lifecycle management (clean
, compile
, test
, package
, install
, deploy
)
✔ Extensible via plugins
Installation
# Linux/macOS
brew install maven # or `sudo apt install maven`
# Windows (via Chocolatey)
choco install maven
# Verify
mvn -v
2. Project Object Model (POM)
The pom.xml
file defines the project structure, dependencies, and build configuration.
Basic POM Structure
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany</groupId>
<artifactId>my-app</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging> <!-- or 'war', 'pom' -->
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.9.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Key POM Elements
Tag | Purpose |
---|---|
<groupId> | Organization identifier (e.g., com.company ) |
<artifactId> | Project name (e.g., my-app ) |
<version> | Release version (e.g., 1.0.0-SNAPSHOT ) |
<packaging> | jar , war , pom , etc. |
<dependencies> | External libraries |
<build> | Plugins and build settings |
3. Maven Build Lifecycle
Maven follows a fixed build lifecycle with phases:
Phase | Description |
---|---|
validate | Checks project correctness |
compile | Compiles source code |
test | Runs unit tests |
package | Creates JAR/WAR |
verify | Runs integration tests |
install | Installs artifact in local .m2 repo |
deploy | Uploads to remote repo |
Common Commands
mvn clean # Deletes `target/`
mvn compile # Compiles code
mvn test # Runs tests
mvn package # Creates JAR/WAR
mvn install # Installs in local repo
mvn deploy # Deploys to remote repo
4. Dependency Management
Dependency Scope
Scope | Description |
---|---|
compile | Default (included in all phases) |
provided | Needed for compilation but not packaged (e.g., servlet-api ) |
runtime | Needed at runtime but not compile-time |
test | Only for testing (e.g., JUnit) |
system | Local JAR (avoid if possible) |
Excluding Transitive Dependencies
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>6.0.0</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
BOM (Bill of Materials)
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>3.1.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
5. Plugins & Customization
Essential Plugins
Plugin | Purpose |
---|---|
maven-compiler-plugin | Sets Java version |
maven-surefire-plugin | Runs tests |
maven-jar-plugin | Customizes JAR manifest |
maven-shade-plugin | Creates fat JARs |
maven-assembly-plugin | Builds ZIP/TAR distributions |
Example: Fat JAR with Shade Plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.5.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
6. Multi-Module Projects
Parent POM (pom.xml
)
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany</groupId>
<artifactId>parent-project</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>
<modules>
<module>module1</module>
<module>module2</module>
</modules>
</project>
Child Module (module1/pom.xml
)
<project>
<parent>
<groupId>com.mycompany</groupId>
<artifactId>parent-project</artifactId>
<version>1.0.0</version>
</parent>
<artifactId>module1</artifactId>
</project>
Building Multi-Module Projects
mvn clean install # Builds all modules
7. Advanced Maven
Profiles
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<env>development</env>
</properties>
</profile>
</profiles>
Run with:
mvn -Pdev package
Custom Properties
<properties>
<java.version>17</java.version>
</properties>
Maven Wrapper
Generate mvnw
(Maven Wrapper):
mvn wrapper:wrapper
Then use:
./mvnw clean install # No Maven installation needed!
8. Best Practices
✅ Use dependencyManagement
for version control
✅ Avoid system
scope (use local repo instead)
✅ Use Maven Wrapper (mvnw
) for reproducible builds
✅ Keep POM clean (avoid unnecessary plugins)
✅ Use CI/CD integration (Jenkins, GitHub Actions)
9. Maven vs. Gradle
Feature | Maven | Gradle |
---|---|---|
Config Language | XML | Groovy/Kotlin DSL |
Performance | Slower (XML parsing) | Faster (incremental builds) |
Flexibility | Strict conventions | Highly customizable |
Learning Curve | Simpler | Steeper |
When to Use Which?
- Maven: Standard projects, enterprise environments.
- Gradle: Custom builds, Android, performance-critical projects.