- Maven = Build automation + Dependency management tool.
- Written in Java, primarily used for Java projects.
- Uses Project Object Model (POM) (
pom.xml) to manage project configuration. - Provides standardization, convention over configuration, and reproducible builds.
- Dependency Management (automatic download from repositories).
- Lifecycle Management (compile, test, package, deploy).
- Plugin-based (plugins add tasks like compiling, testing, packaging).
- Supports multi-module projects.
- Integration with CI/CD (Jenkins, GitLab, GitHub Actions).
- Repository system (Local → Central → Remote).
Project → POM.xml → Build Lifecycle → Plugins → Goals → Output (jar/war/ear)
- Local Repository:
~/.m2/repository(cache of dependencies). - Central Repository: Default Maven repo (
repo.maven.apache.org). - Remote Repository: Company/private repos (e.g., Nexus, Artifactory).
Main configuration file: pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>com.company</groupId>
<artifactId>employee-app</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<name>Employee Management App</name>
<description>Spring Boot app for employee management</description>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
<!-- Example: Spring Boot Starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>3.3.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Compiler plugin -->
<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>Maven defines 3 lifecycles:
- validate → Check POM correctness
- compile → Compile source
- test → Run unit tests
- package → Create JAR/WAR
- verify → Integration tests
- install → Copy artifact to local repo
- deploy → Push artifact to remote repo
pre-clean→ tasks before cleaningclean→ removetarget/post-clean
site→ Generate documentationsite-deploy→ Publish docs
| Command | Purpose |
|---|---|
mvn clean |
Deletes target/ directory |
mvn compile |
Compiles source |
mvn test |
Runs unit tests |
mvn package |
Builds jar/war |
mvn install |
Installs artifact in local repo |
mvn deploy |
Deploys to remote repo |
mvn verify |
Run checks before install |
mvn site |
Generates site docs |
👉 Combination examples:
mvn clean install→ clean + compile + test + package + installmvn test package→ compile + test + packagemvn dependency:tree→ Shows dependency hierarchy
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.11.0</version>
<scope>test</scope>
</dependency>- compile (default) – Available everywhere.
- provided – Needed for compile, but container provides (e.g., Servlet API).
- runtime – Not for compile, only runtime.
- test – Only for testing (JUnit, Mockito).
- system – External, provided explicitly.
- import – Used for dependency management.
- Nearest-Wins Strategy → If multiple versions exist, the nearest one in dependency tree is picked.
Plugins provide tasks (goals).
Examples:
- Compiler Plugin –
maven-compiler-plugin - Surefire Plugin – Run unit tests
- Failsafe Plugin – Run integration tests
- Shade Plugin – Create uber/fat JAR
- JAR/WAR Plugin – Package into JAR/WAR
- Checkstyle Plugin – Code quality checks
Profiles allow different builds for different environments.
<profiles>
<profile>
<id>dev</id>
<properties>
<db.url>jdbc:mysql://localhost/devdb</db.url>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<db.url>jdbc:mysql://prod-server/proddb</db.url>
</properties>
</profile>
</profiles>Run with:
mvn clean install -Pdev
mvn clean install -PprodParent pom.xml manages multiple modules.
Parent POM (pom.xml)
<modules>
<module>service-api</module>
<module>service-impl</module>
</modules>Child module inherits from parent:
<parent>
<groupId>com.company</groupId>
<artifactId>project-parent</artifactId>
<version>1.0.0</version>
</parent>✅ Keep dependencies minimal.
✅ Use dependency management in parent POM.
✅ Use mvn dependency:tree to avoid conflicts.
✅ Separate test/integration test using Surefire & Failsafe.
✅ Use profiles for environment-specific configs.
✅ Prefer company Nexus/Artifactory instead of central repo for consistency.
| Feature | Maven | Gradle | Ant |
|---|---|---|---|
| Model | Declarative (XML) | DSL (Groovy/Kotlin) | Procedural |
| Performance | Slower | Faster (incremental builds) | Fast but manual |
| Dependency Mgmt | Built-in | Built-in | Manual |
| Learning Curve | Moderate | Steeper | Easy but verbose |
- What is Maven and why use it?
- Explain Maven lifecycle phases.
- Difference between
mvn installandmvn package. - How does Maven resolve dependency conflicts?
- What is the difference between Surefire and Failsafe plugins?
- Explain dependency scopes.
- What’s the role of parent POM in multi-module projects?
- How do you create environment-specific builds?
- Difference between Maven and Gradle?
- What happens when you run
mvn clean install?
✅ This documentation gives you both theory and examples so you can quickly revise before your interview.
Would you like me to also make a 1-page cheat sheet (summary version with diagrams + commands) that you can keep handy for quick last-minute revision?