Complete Java Maven Guide with Examples and Output
Apache Maven is a build automation and project management tool used to develop, compile, test, package, and deploy Java applications.
Maven is widely used in Java development, Selenium automation, TestNG, Cucumber, REST API automation, Spring Boot, and CI/CD pipelines.
1. What is Apache Maven?
Apache Maven helps automate the software development process.
Without Maven, developers manually download required JAR files, add dependencies, compile Java source files, execute test cases, create JAR or WAR files, and manage library versions.
With Maven, these activities can be automated through a configuration file
called pom.xml.
Maven Features
| Feature | Description |
|---|---|
| Dependency management | Downloads and manages external libraries |
| Build automation | Compiles and packages Java projects |
| Project structure | Provides a standard directory structure |
| Testing | Integrates with JUnit and TestNG |
| Plugins | Supports compilation, testing, reporting, and packaging |
| Repository management | Stores and retrieves dependencies |
| CI/CD integration | Works with Jenkins and GitHub Actions |
| Multi-module projects | Manages multiple related projects |
2. How Maven Works
Maven follows a build process using a project configuration file
(pom.xml).
Maven Execution Flow
- Developer creates a Java project.
- Maven reads
pom.xml. - Maven checks the required dependencies.
- Maven downloads missing dependencies from repositories.
- Maven compiles Java source files.
- Maven executes test cases.
- Maven packages the application.
- Maven generates the final build artifact.
3. Maven Installation and Configuration
Step 1: Prerequisites
- Java JDK
- Apache Maven
- IntelliJ IDEA or Eclipse
- Command Prompt or Terminal
Step 2: Verify Java Installation
java -version
Expected output:
java version "26.0.2"
Java(TM) SE Runtime Environment
Verify Java Compiler
javac -version
Expected output:
javac 26.0.2
Step 3: Verify Maven Installation
mvn -version
Expected output:
Apache Maven 3.9.x
Maven home: C:\apache-maven
Java version: 26.0.2
Java home: C:\Program Files\Java\jdk-26
OS name: Windows
The version numbers and paths depend on your installation.
Step 4: Environment Variables
| Variable | Example |
|---|---|
| JAVA_HOME | C:\Program Files\Java\jdk-26 |
| MAVEN_HOME | C:\apache-maven |
| PATH | %JAVA_HOME%\bin |
| PATH | %MAVEN_HOME%\bin |
Verify environment variables on Windows:
echo %JAVA_HOME%
echo %MAVEN_HOME%
mvn -version
For macOS/Linux, configure JAVA_HOME and add Maven's
bin directory to PATH.
4. Create a Maven Project
There are two common ways to create a Maven project.
Option 1: Using Maven Command Line
mvn archetype:generate
For a simple Java project:
mvn archetype:generate \
-DgroupId=com.example \
-DartifactId=java-maven-demo \
-DarchetypeArtifactId=maven-archetype-quickstart \
-DinteractiveMode=false
Windows CMD:
mvn archetype:generate -DgroupId=com.example -DartifactId=java-maven-demo -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
Option 2: Using IntelliJ IDEA
- Open IntelliJ IDEA.
- Select New Project.
- Select Java.
- Select Maven as the build system.
- Choose your JDK.
- Enter project name.
- Click Create.
5. Maven Project Directory Structure
java-maven-demo/
│
├── pom.xml
│
├── src/
│ ├── main/
│ │ ├── java/
│ │ │ └── com/example/
│ │ │ └── App.java
│ │ └── resources/
│ │
│ └── test/
│ ├── java/
│ │ └── com/example/
│ │ └── AppTest.java
│ └── resources/
│
└── target/
| Directory/File | Purpose |
|---|---|
| src/main/java | Application Java code |
| src/main/resources | Configuration and resource files |
| src/test/java | Test automation code |
| src/test/resources | Test data and configuration |
| pom.xml | Maven configuration |
| target | Compiled classes, reports, JAR/WAR files |
Maven automatically recognizes these standard folders.
6. What is pom.xml?
POM stands for Project Object Model. It is the main configuration file of a Maven project.
It defines:
- Project information
- Group ID
- Artifact ID
- Version
- Java compiler version
- Dependencies
- Plugins
- Build configuration
- Profiles
Basic pom.xml Example
<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
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>java-maven-demo</artifactId>
<version>1.0.0</version>
<properties>
<maven.compiler.release>17</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
This example uses Java 17 for broad compatibility. Change the release value according to your installed JDK.
Understanding Maven Coordinates
<groupId>com.example</groupId>
<artifactId>java-maven-demo</artifactId>
<version>1.0.0</version>
| Element | Meaning | Example |
|---|---|---|
| groupId | Organization or package identifier | com.example |
| artifactId | Project or library name | java-maven-demo |
| version | Project version | 1.0.0 |
The combination is commonly called Maven coordinates.
7. Maven Dependencies
Dependencies are external libraries required by your Java application.
Examples:
- Selenium WebDriver
- TestNG
- JUnit
- Rest Assured
- Jackson
- Apache POI
- Log4j
Example: Add TestNG Dependency
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.11.0</version>
<scope>test</scope>
</dependency>
</dependencies>
Maven downloads TestNG and its required dependencies automatically.
Example: Multiple Dependencies
<dependencies>
<!-- TestNG -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.11.0</version>
<scope>test</scope>
</dependency>
<!-- Selenium -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.38.0</version>
</dependency>
<!-- Rest Assured -->
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>5.5.6</version>
<scope>test</scope>
</dependency>
<!-- Jackson -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.20.1</version>
</dependency>
</dependencies>
These are example dependency versions. Check compatibility and available releases before adopting them.
Maven Dependency Scopes
| Scope | Compile | Test | Runtime | Package |
|---|---|---|---|---|
| compile (default) | Yes | Yes | Yes | Yes |
| provided | Yes | Yes | Yes | No |
| runtime | No | Yes | Yes | Yes |
| test | No | Yes | No | No |
| system | Yes | Yes | Yes | No |
| import | Special dependency management scope | |||
Compile Scope
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.20.1</version>
</dependency>
Test Scope
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.11.0</version>
<scope>test</scope>
</dependency>
Provided Scope
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>6.1.0</version>
<scope>provided</scope>
</dependency>
8. Maven Repositories
A Maven repository stores project dependencies, plugins, and build artifacts.
There are three important repository types.
1. Local Repository
Default Windows location:
C:\Users\USERNAME\.m2\repository
Linux/macOS:
~/.m2/repository
2. Central Repository
Maven Central is the primary public repository for Java libraries.
3. Remote Repository
Organizations can maintain private repositories using tools such as Nexus Repository and JFrog Artifactory.
Example repository configuration:
<repositories>
<repository>
<id>company-repository</id>
<url>https://repo.example.com/maven2</url>
</repository>
</repositories>
9. Maven Build Lifecycle
Maven lifecycle is one of the most important Maven concepts.
A lifecycle is a sequence of build phases that perform different operations.
Maven has three built-in lifecycles:
- Default lifecycle.
- Clean lifecycle.
- Site lifecycle.
Default Lifecycle Phases
| Phase | Description |
|---|---|
| validate | Validates the project |
| initialize | Initializes build state |
| generate-sources | Generates source code |
| process-sources | Processes source files |
| generate-resources | Generates resources |
| process-resources | Copies and processes resources |
| compile | Compiles main Java code |
| process-classes | Processes compiled classes |
| generate-test-sources | Generates test source code |
| process-test-sources | Processes test sources |
| generate-test-resources | Generates test resources |
| process-test-resources | Processes test resources |
| test-compile | Compiles test code |
| process-test-classes | Processes test classes |
| test | Runs unit tests |
| prepare-package | Prepares packaging |
| package | Creates JAR/WAR |
| pre-integration-test | Prepares integration testing |
| integration-test | Runs integration testing |
| post-integration-test | Cleans up integration testing |
| verify | Verifies package quality |
| install | Installs artifact locally |
| deploy | Publishes artifact to remote repository |
When you execute a later phase, Maven executes the preceding phases in that lifecycle automatically.
Example: Package Command
mvn package
Maven runs the phases up to package, including compilation and testing.
Clean Lifecycle
| Phase | Description |
|---|---|
| pre-clean | Executes before cleaning |
| clean | Deletes previous build output |
| post-clean | Executes after cleaning |
mvn clean
Site Lifecycle
| Phase | Description |
|---|---|
| pre-site | Prepares site generation |
| site | Generates project documentation |
| post-site | Executes after site generation |
| site-deploy | Publishes project site |
10. Important Maven Commands with Examples
| Command | Purpose |
|---|---|
mvn -version |
Displays Maven version |
mvn validate |
Validates project |
mvn compile |
Compiles source code |
mvn test-compile |
Compiles test code |
mvn test |
Executes tests |
mvn package |
Creates JAR/WAR |
mvn verify |
Runs verification |
mvn install |
Installs artifact locally |
mvn deploy |
Deploys artifact remotely |
mvn clean |
Removes target directory |
mvn clean install |
Cleans and installs project |
mvn dependency:tree |
Displays dependency hierarchy |
mvn dependency:resolve |
Resolves project dependencies |
mvn help:effective-pom |
Displays effective POM |
mvn help:describe |
Describes Maven plugin goals |
Example 1: Compile Project
mvn compile
Expected output:
[INFO] Scanning for projects...
[INFO] Compiling 1 source file
[INFO] BUILD SUCCESS
[INFO] Total time: 2.5 s
Example 2: Run Tests
mvn test
Expected output:
[INFO] Running com.example.AppTest
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
[INFO] BUILD SUCCESS
Example 3: Package Project
mvn package
Expected output:
[INFO] Building jar: target/java-maven-demo-1.0.0.jar
[INFO] BUILD SUCCESS
Example 4: Install Project
mvn clean install
Expected output:
[INFO] Cleaning project
[INFO] Compiling source files
[INFO] Running tests
[INFO] Building jar
[INFO] Installing jar into local repository
[INFO] BUILD SUCCESS
The JAR will be available in:
~/.m2/repository/com/example/java-maven-demo/1.0.0/
11. Maven Plugins
Maven plugins perform the actual build operations.
Maven defines lifecycle phases, while plugins provide the goals that execute during those phases.
| Plugin | Purpose |
|---|---|
| maven-compiler-plugin | Compiles Java code |
| maven-surefire-plugin | Executes unit tests |
| maven-failsafe-plugin | Executes integration tests |
| maven-jar-plugin | Creates JAR files |
| maven-war-plugin | Creates WAR files |
| maven-clean-plugin | Cleans build directory |
| maven-resources-plugin | Copies resources |
| maven-dependency-plugin | Manages dependencies |
| maven-install-plugin | Installs artifacts locally |
| maven-deploy-plugin | Deploys artifacts remotely |
| maven-site-plugin | Generates project documentation |
Example 1: Maven Compiler Plugin
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.14.1</version>
<configuration>
<release>17</release>
</configuration>
</plugin>
</plugins>
</build>
Command:
mvn clean compile
Example 2: Maven Surefire Plugin
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.5.4</version>
</plugin>
</plugins>
</build>
Command:
mvn test
Expected output:
[INFO] Running com.example.AppTest
Tests run: 3
Failures: 0
Errors: 0
Skipped: 0
[INFO] BUILD SUCCESS
Example 3: Maven JAR Plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.4.2</version>
</plugin>
Command:
mvn package
Generated file:
target/java-maven-demo-1.0.0.jar
12. Complete Maven Project with Java Example
Let's create a complete Java Maven project with a Java class, TestNG test cases, dependencies, and plugins.
Step 1: Project Structure
java-maven-demo/
│
├── pom.xml
│
└── src/
├── main/
│ └── java/
│ └── com/
│ └── example/
│ └── Calculator.java
│
└── test/
└── java/
└── com/
└── example/
└── CalculatorTest.java
Step 2: Create Calculator.java
File: src/main/java/com/example/Calculator.java
package com.example;
public class Calculator {
public int add(int a, int b) {
return a + b;
}
public int subtract(int a, int b) {
return a - b;
}
public int multiply(int a, int b) {
return a * b;
}
public int divide(int a, int b) {
if (b == 0) {
throw new IllegalArgumentException(
"Division by zero is not allowed"
);
}
return a / b;
}
public static void main(String[] args) {
Calculator calculator = new Calculator();
System.out.println("Addition: " + calculator.add(10, 20));
System.out.println("Subtraction: " + calculator.subtract(20, 10));
System.out.println("Multiplication: " + calculator.multiply(10, 5));
System.out.println("Division: " + calculator.divide(20, 5));
}
}
Step 3: Execute Java Program
Compile using Maven:
mvn clean compile
Run the main method:
java -cp target/classes com.example.Calculator
Expected output:
Addition: 30
Subtraction: 10
Multiplication: 50
Division: 4
Step 4: Create CalculatorTest.java
File: src/test/java/com/example/CalculatorTest.java
package com.example;
import org.testng.Assert;
import org.testng.annotations.Test;
public class CalculatorTest {
Calculator calculator = new Calculator();
@Test
public void testAddition() {
Assert.assertEquals(calculator.add(10, 20), 30);
System.out.println("Addition test passed");
}
@Test
public void testSubtraction() {
Assert.assertEquals(calculator.subtract(20, 10), 10);
System.out.println("Subtraction test passed");
}
@Test
public void testMultiplication() {
Assert.assertEquals(calculator.multiply(10, 5), 50);
System.out.println("Multiplication test passed");
}
@Test
public void testDivision() {
Assert.assertEquals(calculator.divide(20, 5), 4);
System.out.println("Division test passed");
}
@Test(expectedExceptions = IllegalArgumentException.class)
public void testDivisionByZero() {
calculator.divide(10, 0);
}
}
Step 5: Complete pom.xml
<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
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>java-maven-demo</artifactId>
<version>1.0.0</version>
<properties>
<maven.compiler.release>17</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.11.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.14.1</version>
<configuration>
<release>17</release>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.5.4</version>
</plugin>
</plugins>
</build>
</project>
Step 6: Execute Test Cases
mvn clean test
Expected output:
[INFO] Scanning for projects...
[INFO] Compiling 1 source file
[INFO] Compiling 1 test source file
[INFO] Running com.example.CalculatorTest
Addition test passed
Subtraction test passed
Multiplication test passed
Tests run: 5, Failures: 0, Errors: 0, Skipped: 0
[INFO] BUILD SUCCESS
The Surefire test report is generated under:
target/surefire-reports/
13. Maven Profiles
Maven profiles allow you to maintain different configurations for different environments.
- Development
- QA
- Staging
- Production
Example: Define Profiles in pom.xml
<profiles>
<profile>
<id>dev</id>
<properties>
<environment>development</environment>
<base.url>https://dev.example.com</base.url>
</properties>
</profile>
<profile>
<id>qa</id>
<properties>
<environment>qa</environment>
<base.url>https://qa.example.com</base.url>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<environment>production</environment>
<base.url>https://example.com</base.url>
</properties>
</profile>
</profiles>
Execute a Particular Profile
Development:
mvn clean test -Pdev
QA:
mvn clean test -Pqa
Production:
mvn clean test -Pprod
Expected output:
[INFO] Building java-maven-demo
[INFO] BUILD SUCCESS
You can access configured Maven properties in your build configuration and resource filtering.
14. Maven Properties
Properties allow you to define reusable values inside
pom.xml.
Example
<properties>
<java.version>17</java.version>
<testng.version>7.11.0</testng.version>
<selenium.version>4.38.0</selenium.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
Use these properties in dependencies:
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>${selenium.version}</version>
</dependency>
Benefits
- Avoids repeating version numbers.
- Makes dependency upgrades easier.
- Improves maintainability.
- Reduces configuration duplication.
15. Maven Dependency Management
dependencyManagement centralizes dependency versions,
especially in multi-module projects.
Example
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.11.0</version>
</dependency>
</dependencies>
</dependencyManagement>
A child module can then declare:
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
</dependency>
Difference Between Dependencies and Dependency Management
| dependencies | dependencyManagement |
|---|---|
| Adds dependencies to the project | Defines dependency defaults |
| Dependencies are used directly | Does not automatically add dependencies |
| Can be inherited | Can be inherited |
16. Maven Multi-Module Project
A multi-module project allows you to manage multiple Maven projects under one parent project.
This is useful for enterprise Java applications and automation frameworks.
Example Project Structure
automation-framework/
│
├── pom.xml
│
├── common/
│ └── pom.xml
│
├── api-tests/
│ └── pom.xml
│
└── ui-tests/
└── pom.xml
Parent pom.xml
<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
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.automation</groupId>
<artifactId>automation-framework</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>
<modules>
<module>common</module>
<module>api-tests</module>
<module>ui-tests</module>
</modules>
</project>
Child Module pom.xml Example
For api-tests/pom.xml:
<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
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.automation</groupId>
<artifactId>automation-framework</artifactId>
<version>1.0.0</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>api-tests</artifactId>
</project>
Execute All Modules
mvn clean install
Execute only one module and its required dependencies:
mvn clean install -pl api-tests -am
-plselects the project/module.-amalso builds required modules.
17. Maven Test Execution Options
These options are especially useful for automation testing frameworks.
Run a Specific Test Class
mvn test -Dtest=CalculatorTest
Run a Specific Test Method
mvn test -Dtest=CalculatorTest#testAddition
Run Multiple Test Classes
mvn test -Dtest=CalculatorTest,LoginTest
Skip Test Execution
mvn package -DskipTests
This compiles tests but does not execute them.
Skip Test Compilation and Execution
mvn package -Dmaven.test.skip=true
Run Tests Using TestNG XML Suite
Add this to Surefire configuration:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.5.4</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
Run:
mvn clean test
Maven will execute the TestNG suite specified in testng.xml.
18. Maven Wrapper
Maven Wrapper allows developers to run a project with a configured Maven version without manually installing Maven.
It creates these files:
.mvn/
wrapper/
maven-wrapper.properties
mvnw
mvnw.cmd
Generate Maven Wrapper
mvn wrapper:wrapper
Execute Maven on Windows
mvnw.cmd clean test
Execute Maven on Linux/macOS
./mvnw clean test
Benefits
- Consistent Maven versions across teams.
- Easier CI/CD integration.
- No separate Maven installation required for contributors.
- Reproducible build environment.
19. Maven Integration with GitHub Actions
Maven can automatically build and test Java applications through GitHub Actions.
Example Workflow
File: .github/workflows/maven.yml
name: Java Maven CI
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout source code
uses: actions/checkout@v4
- name: Setup Java
uses: actions/setup-java@v4
with:
distribution: temurin
java-version: '17'
cache: maven
- name: Build Maven project
run: mvn clean install
- name: Upload test reports
if: always()
uses: actions/upload-artifact@v4
with:
name: surefire-reports
path: target/surefire-reports/
Expected CI Pipeline
Checkout source code SUCCESS
Setup Java SUCCESS
Maven dependency download SUCCESS
Compile Java SUCCESS
Execute TestNG tests SUCCESS
Package application SUCCESS
Upload test reports SUCCESS
20. Maven Troubleshooting
Common Maven problems and solutions:
| Error | Possible Solution |
|---|---|
| mvn is not recognized | Configure Maven PATH |
| JAVA_HOME is not defined | Configure JAVA_HOME |
| Could not resolve dependencies | Check internet, repository, and dependency version |
| BUILD FAILURE | Check Maven error logs |
| Compilation failure | Check Java version and source code |
| Tests are not running | Check Surefire configuration and naming |
| No tests to run | Check test class naming and test framework |
| Unsupported class file version | Check JDK and dependency compatibility |
| Plugin resolution failed | Check plugin version and repository access |
Useful Troubleshooting Commands
Display dependency tree:
mvn dependency:tree
Force dependency updates:
mvn clean install -U
Display effective POM:
mvn help:effective-pom
Run Maven in debug mode:
mvn clean install -X
Display detailed error information:
mvn clean install -e
21. Maven vs Gradle
Both Maven and Gradle are popular Java build automation tools.
| Feature | Maven | Gradle |
|---|---|---|
| Configuration | XML (pom.xml) | Groovy/Kotlin DSL |
| Build configuration | Declarative | Declarative and programmable |
| Dependency management | Yes | Yes |
| Build performance | Good | Often faster with caching |
| Learning curve | Easier initially | More flexible, steeper initially |
| Multi-module | Yes | Yes |
| Java support | Yes | Yes |
| Selenium integration | Yes | Yes |
| CI/CD integration | Yes | Yes |
When to use Maven: Traditional enterprise Java projects, Selenium frameworks, TestNG, Cucumber, and standard build pipelines.
When to use Gradle: Projects requiring flexible build logic, advanced customization, and build performance optimization.
22. Important Maven Interview Questions and Answers
Q1. What is Maven?
Maven is a Java build automation and project management tool used to compile, test, package, and deploy applications.
Q2. What is pom.xml?
POM stands for Project Object Model. It contains project information, dependencies, plugins, properties, and build configurations.
Q3. What is the difference between Maven and Ant?
- Maven follows conventions and uses declarative XML configuration.
- Ant uses XML-based build scripts with explicitly defined tasks.
- Maven provides built-in dependency management.
Q4. What is the difference between mvn clean install and mvn clean package?
-
clean package: Removes old build files, compiles, tests, and packages the application. -
clean install: Performs the same operations and installs the generated artifact into the local repository.
Q5. What is Maven Central?
Maven Central is a public repository where developers can publish and download Java libraries and Maven plugins.
Q6. What is the difference between dependency and plugin?
- Dependency provides libraries required by an application.
- Plugin performs build operations such as compilation, testing, and packaging.
Q7. What is the difference between Surefire and Failsafe?
| Surefire | Failsafe |
|---|---|
| Unit test execution | Integration test execution |
| Usually runs during test | Usually runs during integration-test and verify |
| Commonly uses *Test naming | Commonly uses *IT naming |
Q8. What is the difference between -DskipTests and -Dmaven.test.skip=true?
-
-DskipTestsskips test execution but still compiles tests. -
-Dmaven.test.skip=trueskips test compilation and execution.
Q9. What is a transitive dependency?
A transitive dependency is a library automatically included because another dependency requires it.
Q10. What is the difference between dependencyManagement and dependencies?
dependencies declares libraries used by a project.
dependencyManagement centralizes dependency versions and
configurations for projects that declare those dependencies.
Q11. What is Maven Wrapper?
Maven Wrapper allows a project to specify and use its own Maven distribution without requiring a separate Maven installation.
Q12. How do you execute a particular TestNG test using Maven?
mvn test -Dtest=LoginTest
Q13. How do you execute Maven tests in parallel?
Configure Surefire with parallel execution settings:
<configuration>
<parallel>methods</parallel>
<threadCount>4</threadCount>
</configuration>
This enables method-level parallel execution for supported test providers. Make sure your tests are thread-safe.
Q14. What is the difference between compile and test-compile?
compilecompiles application source code.test-compilecompiles test source code.
Q15. What is the purpose of Maven profiles?
Profiles provide different build configurations for environments such as development, QA, staging, and production.
23. Maven Command Cheat Sheet
| Requirement | Command |
|---|---|
| Check Maven | mvn -version |
| Compile | mvn compile |
| Compile tests | mvn test-compile |
| Run tests | mvn test |
| Create JAR | mvn package |
| Verify project | mvn verify |
| Install locally | mvn install |
| Clean project | mvn clean |
| Clean and build | mvn clean install |
| Update dependencies | mvn clean install -U |
| Dependency tree | mvn dependency:tree |
| Skip tests | mvn package -DskipTests |
| Run specific test | mvn test -Dtest=LoginTest |
| Activate profile | mvn test -Pqa |
| Debug Maven | mvn clean install -X |
| Effective POM | mvn help:effective-pom |
24. Official Maven Learning Resources
25. Practical Maven Learning Roadmap
Beginner
- Maven installation
- Maven project structure
- POM configuration
- Dependencies and repositories
- Basic Maven commands
Intermediate
- Maven lifecycle
- Plugins and goals
- Dependency scopes
- Maven profiles
- Dependency management
- Maven Surefire and Failsafe
Advanced
- Multi-module Maven projects
- Maven Wrapper
- Parallel test execution
- Custom plugins
- Dependency exclusions
- Build optimization
Real-World Automation Projects
- Selenium + TestNG + Maven
- Cucumber + TestNG + Maven
- Rest Assured + Maven
- Extent Reports integration
- Jenkins and GitHub Actions integration
- Docker-based Maven execution
Recommendation: After completing the basic Maven project, build a Selenium + TestNG automation framework using Maven with Page Object Model, parallel execution, Extent Reports, and GitHub Actions. This provides practical experience with Maven in a real automation testing project.

No comments:
Post a Comment