<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!
# Clean target directorymvn clean# Compile source codemvn compile# Run testsmvn test# Create JAR/WAR filemvn package# Clean and package (most common)mvn clean package# Install to local repositorymvn clean install# Skip tests (faster builds)mvn clean package -DskipTests# Run in debug modemvn 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>
<!-- 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>
# 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