🔨 Day 8: Maven Build Tools

Hands-On Guide - pom.xml, Lifecycle & Plugins

📋 Day 8 Overview

Today's Learning Goals

  • ✅ Understand what Maven is and why it's used
  • ✅ Install Maven on your system
  • ✅ Understand pom.xml structure
  • ✅ Learn Maven build lifecycle phases
  • ✅ Configure Maven plugins
  • ✅ Build Java projects with Maven

What is Maven?

Maven is a build automation and project management tool for Java projects. It handles:

Key Concepts

Concept Description
pom.xml Project Object Model - Maven configuration file
Lifecycle Series of build phases (compile, test, package, etc.)
Plugin Extends Maven functionality
Dependency External library your project uses
Repository Storage for dependencies (Maven Central, local)

⬇️ Maven Installation

Prerequisites

Java Required!

Maven needs Java JDK installed first.

# Check Java version java -version # Should show Java 11 or higher java version "17.0.8"

Install Maven - Windows

1 Download Maven

Visit: https://maven.apache.org/download.cgi

Download: apache-maven-3.9.x-bin.zip

2 Extract and Set PATH
  1. Extract to C:\Program Files\Apache\maven
  2. Add to System PATH: C:\Program Files\Apache\maven\bin
  3. Set JAVA_HOME environment variable
3 Verify Installation
mvn -version Apache Maven 3.9.5 Maven home: C:\Program Files\Apache\maven Java version: 17.0.8

Install Maven - macOS

# Using Homebrew (easiest) brew install maven # Verify mvn -version

Install Maven - Linux

# Ubuntu/Debian sudo apt update sudo apt install maven # RHEL/CentOS sudo yum install maven # Verify mvn -version

📄 Understanding pom.xml

pom.xml = Project Object Model

The configuration file that defines your Maven project

Minimal pom.xml

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <!-- Project Coordinates --> <groupId>com.mycompany</groupId> <artifactId>my-app</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>My Application</name> </project>

Complete pom.xml with Dependencies

<project> <modelVersion>4.0.0</modelVersion> <groupId>com.dpworld</groupId> <artifactId>logistics-app</artifactId> <version>1.0.0</version> <packaging>jar</packaging> <properties> <maven.compiler.source>17</maven.compiler.source> <maven.compiler.target>17</maven.compiler.target> </properties> <dependencies> <!-- JUnit for Testing --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> <scope>test</scope> </dependency> </dependencies> </project>

pom.xml Key Sections

Section Purpose
<groupId> Organization identifier (reverse domain)
<artifactId> Project name
<version> Project version (1.0.0, 2.1-SNAPSHOT)
<dependencies> External libraries needed
<build> Build configuration and plugins
<properties> Project-wide variables

🔄 Maven Build Lifecycle

What is the Build Lifecycle?

A defined sequence of phases that Maven executes to build and distribute your project

Default Lifecycle Phases (in order)

Phase Description Command
validate Validate project is correct mvn validate
compile Compile source code mvn compile
test Run unit tests mvn test
package Package code into JAR/WAR mvn package
verify Run integration tests mvn verify
install Install to local Maven repo mvn install
deploy Deploy to remote repository mvn deploy
💡 Important: When you run a phase, Maven executes ALL previous phases automatically!
mvn package # Automatically runs: # 1. validate # 2. compile # 3. test # 4. package

Common Maven Commands

# Clean target directory mvn clean # Compile source code mvn compile # Run tests mvn test # Create JAR/WAR file mvn package # Clean and package (most common) mvn clean package # Install to local repository mvn clean install # Skip tests (faster builds) mvn clean package -DskipTests # Run in debug mode mvn clean package -X

🔌 Maven Plugins

What are Plugins?

Plugins provide most of Maven's functionality. Each plugin performs specific build tasks.

Essential Plugins

1. Maven Compiler Plugin

Compiles Java source code

<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.11.0</version> <configuration> <source>17</source> <!-- Java version to use --> <target>17</target> </configuration> </plugin>
2. Maven Surefire Plugin

Runs unit tests during test phase

<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.0.0</version> </plugin>
3. Maven JAR/WAR Plugin

Packages compiled code into JAR or WAR files

<!-- For JAR files --> <plugin> <artifactId>maven-jar-plugin</artifactId> <version>3.3.0</version> </plugin> <!-- For WAR files (web apps) --> <plugin> <artifactId>maven-war-plugin</artifactId> <version>3.4.0</version> </plugin>

🔨 Practice Exercises

Exercise 1: Create Maven Project

📝 Task: Create your first Maven project
# Create Maven project from archetype mvn archetype:generate \ -DgroupId=com.mycompany \ -DartifactId=my-app \ -DarchetypeArtifactId=maven-archetype-quickstart \ -DinteractiveMode=false # Navigate to project cd my-app # View structure ls -la pom.xml src/ # View pom.xml cat pom.xml

Exercise 2: Build Your Project

📝 Task: Run Maven build lifecycle
# 1. Compile the code mvn compile BUILD SUCCESS # 2. Run tests mvn test Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 BUILD SUCCESS # 3. Package into JAR mvn package Building jar: target/my-app-1.0-SNAPSHOT.jar BUILD SUCCESS # 4. Check target directory ls target/ my-app-1.0-SNAPSHOT.jar

Exercise 3: Add Dependencies

📝 Task: Add JUnit dependency to pom.xml
# Open pom.xml and add to <dependencies> section: <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> <scope>test</scope> </dependency> # Save pom.xml and run: mvn clean install # Maven downloads JUnit automatically! Downloading: junit-4.13.2.jar Downloaded: 384 KB

Exercise 4: Configure Compiler Plugin

📝 Task: Set Java version to 17
# Add to pom.xml in <build><plugins> section: <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> # Verify by building: mvn clean compile

Challenge: Complete Maven Project

🎯 Challenge: Create a complete Maven web application
  1. Generate Maven project with war packaging
  2. Add Spring Boot dependency
  3. Configure compiler for Java 17
  4. Build and package
  5. Verify WAR file in target/