Maven
Java project management and build tool
🔧 What is Maven?
Apache Maven is a build automation and project management tool for Java projects. It simplifies dependency management, project structure, and build processes through standardized conventions and declarative configuration using XML.
<!-- Basic Maven project structure -->
<project>
<groupId>com.example</groupId>
<artifactId>my-app</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
</project>
Maven Features
Dependency Management
Automatic library download and management
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
Build Lifecycle
Standardized build phases
mvn clean compile test package
Project Structure
Standard directory layout
src/main/java
src/main/resources
src/test/java
Plugins
Extensible build functionality
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
🔹 Basic Maven Project Structure
Maven follows a standard directory layout:
my-app/
├── pom.xml
├── src/
│ ├── main/
│ │ ├── java/
│ │ │ └── com/example/App.java
│ │ └── resources/
│ │ └── application.properties
│ └── test/
│ ├── java/
│ │ └── com/example/AppTest.java
│ └── resources/
└── target/
├── classes/
└── my-app-1.0.0.jar
Directory Purpose:
src/main/java - Source code
src/test/java - Test code
target/ - Compiled output
🔹 Complete pom.xml Example
A comprehensive Maven project configuration:
<?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.example</groupId>
<artifactId>my-web-app</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<name>My Web Application</name>
<description>A sample Spring Boot application</description>
<!-- Properties -->
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<spring.boot.version>3.2.0</spring.boot.version>
<junit.version>5.9.2</junit.version>
</properties>
<!-- Dependencies -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring.boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${spring.boot.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<!-- Build configuration -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring.boot.version}</version>
</plugin>
</plugins>
</build>
</project>
🔹 Maven Commands
Essential Maven commands for daily development:
# Clean previous builds
mvn clean
# Compile source code
mvn compile
# Run tests
mvn test
# Package into JAR/WAR
mvn package
# Install to local repository
mvn install
# Deploy to remote repository
mvn deploy
# Run Spring Boot application
mvn spring-boot:run
# Generate project from archetype
mvn archetype:generate -DgroupId=com.example \
-DartifactId=my-app \
-DarchetypeArtifactId=maven-archetype-quickstart \
-DinteractiveMode=false
Build Lifecycle:
validate → compile → test → package → install → deploy
🔹 Dependency Scopes
Different dependency scopes in Maven:
<dependencies>
<!-- Compile scope (default) - available in all phases -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>6.0.0</version>
<scope>compile</scope>
</dependency>
<!-- Test scope - only for testing -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<!-- Runtime scope - not needed for compilation -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
<scope>runtime</scope>
</dependency>
<!-- Provided scope - provided by container -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
</dependencies>
🔹 Maven Profiles
Use profiles for different environments:
<profiles>
<profile>
<id>development</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<database.url>jdbc:h2:mem:devdb</database.url>
<log.level>DEBUG</log.level>
</properties>
</profile>
<profile>
<id>production</id>
<properties>
<database.url>jdbc:mysql://prod-server:3306/mydb</database.url>
<log.level>WARN</log.level>
</properties>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
</dependency>
</dependencies>
</profile>
</profiles>
<!-- Activate profile: mvn clean package -Pproduction -->