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