All about Maven

 


1️⃣ What is Maven?

Maven is a build automation and project management tool used primarily for Java and Spring Boot applications.

🎯 Problems Maven Solves

  • Build generation (compile, test, package)
  • Dependency resolution (automatic download & versioning)
  • Standard project structure
  • Plugin-based extensibility
  • Reproducible builds

🧠 Core Concept

Maven is declarative, not procedural:

  • You describe WHAT you want (in pom.xml)
  • Maven decides HOW to do it (via lifecycle + plugins)

Image

Image

Image


βœ… Quick Summary

  • Maven automates build + dependency management
  • Uses pom.xml as a single source of truth
  • Essential for Spring Boot & enterprise projects

❓ Interview / Exam Questions (with Answers)

  1. What is Maven?
    β†’ A build and dependency management tool for Java projects.
  2. Why is Maven declarative?
    β†’ Developers define configuration, not execution steps.
  3. What problem does Maven solve compared to Ant?
    β†’ Dependency management and standard lifecycle.

2️⃣ pom.xml – Project Object Model

The pom.xml file is the heart of a Maven project.

πŸ”Ή Mandatory Project Coordinates

Tag Meaning
groupId Organization / package namespace
artifactId Project name
version Build version
packaging jar / war
<project>
  <groupId>com.javadoor</groupId>
  <artifactId>learning-springboot</artifactId>
  <version>1.0.0</version>
  <packaging>jar</packaging>
</project>

πŸ”Ή Dependency Management (Internal Working)

  • Maven downloads dependencies from Maven Central
  • Stores them in ~/.m2/repository
  • Uses transitive dependencies automatically

⚠️ Edge Case:
Conflicting versions β†’ solved using dependency mediation (nearest-wins).


Image

Image

Image


βœ… Quick Summary

  • pom.xml controls dependencies, plugins, lifecycle
  • Enables reproducible builds
  • Supports multi-module projects

❓ Interview / Exam Questions

  1. What is pom.xml?
  2. How does Maven resolve version conflicts?
  3. What are transitive dependencies?

3️⃣ Maven Standard Project Structure

Maven enforces Convention over Configuration.

πŸ“ Default Layout

project
 β”œβ”€β”€ pom.xml
 └── src
     β”œβ”€β”€ main
     β”‚   β”œβ”€β”€ java
     β”‚   └── resources
     └── test
         └── java

πŸ” Why This Matters

  • IDEs auto-detect structure
  • CI/CD pipelines rely on it
  • Reduces onboarding time

Image

Image

Image


βœ… Quick Summary

  • Enforced by Maven
  • Separates production & test code
  • Improves maintainability

❓ Interview / Exam Questions

  1. Why does Maven enforce a directory structure?
  2. Where are compiled classes stored?
  3. Where are test cases placed?

4️⃣ Maven Build Lifecycle

Maven lifecycle = ordered phases executed sequentially.

πŸ” Default Lifecycle Phases

Order Phase Purpose
1 validate Validate project
2 compile Compile source code
3 test Run unit tests
4 package Create JAR/WAR
5 verify Quality checks
6 install Local repo
7 deploy Remote repo

πŸ“Œ Key Rule:
Running a later phase executes all previous phases automatically.


 

Image

Image

Image


βœ… Quick Summary

  • Lifecycle is strictly ordered
  • Prevents incomplete builds
  • Core Maven concept for interviews

❓ Interview / Exam Questions

  1. What happens when you run mvn package?
  2. Can phases be skipped?
  3. Difference between lifecycle and goal?

5️⃣ Validate Phase (mvn validate)

πŸ”Ή Purpose

  • Ensures:
    • Project structure is correct
    • pom.xml is valid
  • No compilation happens

πŸ”Ή Real-World Use

  • Enforce Checkstyle
  • Enforce PMD rules
  • Enforce license checks

Image

Image

Image


βœ… Quick Summary

  • First lifecycle phase
  • Ensures correctness before build
  • Extensible using plugins

❓ Interview / Exam Questions

  1. Does validate compile code?
  2. Why use validate phase?
  3. Can plugins run in validate?

6️⃣ Compile Phase (mvn compile)

πŸ”Ή What Happens Internally

  • Uses maven-compiler-plugin
  • Converts .java β†’ .class
  • Output stored in:
target/classes

βš™οΈ Performance Consideration

  • Incremental compilation supported
  • Faster in CI with dependency caching

πŸ–ΌοΈ Required Diagram / Image

Type: Compilation Flow Diagram
Labels must contain:

  • Source Code
  • Compiler Plugin
  • target/classes
    Explains:
    How Maven compiles Java code.

Image

Image

Image


βœ… Quick Summary

  • Compiles only main code
  • No tests executed
  • Generates bytecode

❓ Interview / Exam Questions

  1. Where does Maven store compiled classes?
  2. Which plugin compiles Java code?
  3. Does mvn compile run tests?

7️⃣ Test Phase (mvn test)

πŸ”Ή Purpose

  • Executes unit tests
  • Uses:
    • JUnit
    • TestNG
  • Stops build if any test fails

🧠 Edge Case

  • Integration tests are NOT run here (handled separately)

πŸ–ΌοΈ Required Diagram / Image

Type: Testing Flow Diagram
Labels must contain:

  • Test Classes
  • JUnit
  • Test Results
    Explains:
    How Maven executes unit tests.

Image

Image

Image


βœ… Quick Summary

  • Ensures code correctness
  • Mandatory for CI pipelines
  • Build fails on test failure

❓ Interview / Exam Questions

  1. What happens if a test fails?
  2. Which tests run in mvn test?
  3. Difference between test and verify?

8️⃣ Package Phase (mvn package)

πŸ”Ή Purpose

  • Creates JAR / WAR
  • Combines:
    • Compiled code
    • Resources
    • Metadata

πŸ“¦ Output Location

target/*.jar

πŸ–ΌοΈ Required Diagram / Image

Type: Packaging Diagram
Labels must contain:

  • Compiled Classes
  • Resources
  • JAR/WAR
    Explains:
    How Maven builds deployable artifacts.

Image

Image

Image


βœ… Quick Summary

  • Produces final artifact
  • Used in deployment pipelines
  • Executes previous phases automatically

❓ Interview / Exam Questions

  1. What is created in mvn package?
  2. Difference between jar and war?
  3. Where is the package stored?

9️⃣ Install Phase (mvn install)

πŸ”Ή Purpose

  • Installs artifact into local repository
~/.m2/repository

πŸ”Ή Why Important

  • Enables multi-module projects
  • Allows reuse across local builds

πŸ–ΌοΈ Required Diagram / Image

Type: Repository Diagram
Labels must contain:

  • Project
  • Local Repository
  • Installed Artifact
    Explains:
    How Maven installs artifacts locally.

Image

Image

Image


βœ… Quick Summary

  • Makes artifact reusable
  • Local-only operation
  • Required before deploy

❓ Interview / Exam Questions

  1. Where does mvn install store artifacts?
  2. Why is install required before deploy?
  3. What is local repository?

πŸ”Ÿ Deploy Phase (mvn deploy)

πŸ”Ή Purpose

  • Uploads artifact to remote repository
    • Nexus
    • Artifactory
    • Maven Central

πŸ” Configuration

  • Repository in pom.xml
  • Credentials in settings.xml

πŸ–ΌοΈ Required Diagram / Image

Type: Deployment Architecture Diagram
Labels must contain:

  • Local Repository
  • Remote Repository
  • pom.xml
  • settings.xml
    Explains:
    How Maven deploys artifacts to remote repositories.

Image

Image

Image


βœ… Quick Summary

  • Final lifecycle phase
  • Used in enterprise CI/CD
  • Publishes artifacts

❓ Interview / Exam Questions

  1. Difference between install and deploy?
  2. Where are credentials stored?
  3. Can deploy run without install?

🎯 FINAL CONCLUSION

  • Maven is mandatory knowledge for Spring Boot
  • Lifecycle understanding is frequently tested
  • pom.xml mastery separates junior vs senior engineers

If you want next:

  • βœ… PDF export
  • βœ… Microservices-specific Maven notes
  • βœ… Interview cheat sheet (1-page)
  • βœ… CI/CD Maven pipeline explanation

Just tell me.

Leave a Reply