EduArn – Online & Offline Training with Free LMS for Python, AI, Cloud & More

Showing posts with label Maven. Show all posts
Showing posts with label Maven. Show all posts

Maven Lifecycle Explained: Compile, Test, Package, Install & Deploy | Java & DevOps

EduArn | Java • Maven • DevOps • CI/CD

Maven Lifecycle Explained: Compile, Test, Package, Install & Deploy

Learn what Maven is, how the Maven build lifecycle works, what happens when you run mvn package, how mvn clean package creates a fresh build, and how Maven connects Java development with Jenkins, CI/CD, Docker and cloud deployment.

Java Maven DevOps CI/CD Jenkins Interview Preparation

What Is Maven?

Imagine you're in a Java Developer or DevOps interview and the interviewer gives you a Maven project.

They ask:

“How would you build this application and generate a JAR?”

You answer:

$ mvn package

Then comes the real question:

“What exactly happens when you execute mvn package?”

If your answer is only “It creates a JAR,” you're missing one of the most important Maven concepts: the Maven Build Lifecycle.

Maven is not simply about memorizing commands. It provides a structured way to build, test, package and deliver Java applications.

Watch the Maven Lifecycle Explained

See the Maven lifecycle explained with a practical Java project and real-world DevOps examples.

▶ Watch the Maven Video

Why Is Maven Important for Java and DevOps?

Maven is widely used in Java projects to manage the build process, dependencies, testing and application packaging.

For DevOps engineers, Maven becomes even more important because Java builds often run inside CI/CD systems such as Jenkins and GitHub Actions.

The Big Picture

Source Code
→
Maven
→
Test
→
JAR
→
Docker
→
Cloud

Understanding Maven With a Java Project

Let's use the EduArn Retail Training project as our example.

Imagine the project contains a product class, its tests, the Maven configuration file, resources and required dependencies.

src/ ├── main/ │ └── java/ │ └── Product.java │ └── test/ └── java/ └── ProductTest.java pom.xml

The developer writes or changes code in Product.java. But that source file is not directly deployed to production.

The application needs to go through a build process:

Compile
→
Test
→
Package
→
Install
→
Deploy

The Maven Build Lifecycle

The Maven lifecycle is a sequence of phases that define how a project moves through its build process.

Core Maven Lifecycle Concept

Compile
Build classes
→
Test
Validate
→
Package
Create artifact
→
Install
Local repository
→
Deploy
Remote repository

01. Maven Compile Phase

01

Compile the Java Source Code

The compile phase takes the project's main Java source code and compiles it. The source code is normally located under:

src/main/java

For our project, that includes:

Product.java → compiled Product.class

Run:

$ mvn compile

Maven generates build output under the target directory.

Interview Question
What does mvn compile do?

Answer: It compiles the project's main source code.

02. Maven Test Phase

02

Run Automated Tests

After compilation, Maven can execute the tests configured for the project.

$ mvn test

For our example, Maven can execute:

ProductTest.java
DevOps Connection

In a CI/CD pipeline, Maven tests can act as an automated quality gate. If tests fail, the pipeline can stop instead of producing or promoting a successful build.

Developer Push
→
Jenkins
→
Maven Test
→
Pass / Fail

03. What Happens When You Run mvn package?

$ mvn package

This is one of the most common Maven interview questions.

A beginner may say:

“mvn package creates a JAR.”

That is incomplete.

When you request the package phase, Maven executes the required earlier lifecycle phases before reaching package.

Compile
→
Test
→
Package

If the required build and test steps succeed, Maven creates the configured application artifact. For a typical Java project, this may be a JAR file.

target/application.jar
Strong Interview Answer
“I can use mvn package to execute the Maven lifecycle up to the package phase. Maven performs the required earlier phases and then packages the application into the configured artifact, such as a JAR.”

04. What Does mvn clean Do?

Suppose you built the application yesterday. The target directory may contain generated files from the previous build.

You changed the source code and want to start the build from clean generated output.

$ mvn clean

Easy Memory Trick

clean = remove previous generated build output

05. Why Is mvn clean package So Common?

$ mvn clean package

This combines cleaning the previous generated output with running the Maven lifecycle through the package phase.

Clean
→
Compile
→
Test
→
Package

The result is a fresh build artifact if the required build and test steps succeed.

DevOps Tip:

This command is frequently useful in automated builds when you want to remove previous generated output and produce a fresh artifact.

06. Maven Package vs Install

P

mvn package

Creates the configured application artifact, such as a JAR or WAR.

$ mvn package
I

mvn install

Installs the generated artifact into the local Maven repository.

$ mvn install

The default local Maven repository is commonly located under:

~/.m2/repository

This allows Maven projects on the same machine to resolve artifacts that have been installed locally.

07. What Does mvn deploy Do?

$ mvn deploy

The deploy phase publishes the project artifact and related metadata to a configured remote Maven repository.

Install

Artifact → Local Maven Repository

Think: Local

Deploy

Artifact → Remote Maven Repository

Think: Remote

08. Complete Maven Lifecycle Flow

Compile
Source → Classes
→
Test
Validate
→
Package
Create artifact
→
Install
Local repo
→
Deploy
Remote repo

The most important idea is: when you request a later lifecycle phase, Maven runs the required earlier phases first.

09. Maven in a Real DevOps Pipeline

Now let's connect Maven to a real software delivery scenario.

Imagine your company has a Java application. A developer pushes code to GitHub. A Jenkins pipeline starts and checks out the source code.

Jenkins runs:

$ mvn clean package
1

Clean

Remove previous generated output.

2

Compile

Compile Java source code.

3

Test

Run automated tests.

4

Package

Create the JAR artifact.

Pipeline Result

If the build and tests succeed, Jenkins has a JAR artifact that can be used by subsequent delivery stages.

10. Maven → Docker → Cloud

This is where Maven becomes particularly relevant for DevOps engineers.

Java Code
→
Maven
→
JAR
→
Docker
→
Container Registry
→
Cloud

Maven does not replace Docker, Jenkins, GitHub Actions or cloud platforms. Instead, Maven can be one of the build components inside a larger software delivery pipeline.

11. Maven and CI/CD

Maven commands can be executed automatically by CI/CD systems. A simplified pipeline might look like:

git checkout mvn clean mvn test mvn package

The resulting JAR can then be passed to later stages for containerization, publishing or deployment.

This makes Maven useful knowledge for both Java developers and DevOps engineers.

12. Maven Command Quick Reference

Command Purpose Remember
mvn clean Removes generated build output Clean
mvn compile Compiles main source code Build classes
mvn test Runs project tests Validate
mvn package Creates configured artifact Create JAR
mvn clean package Cleans and packages Fresh build
mvn install Installs artifact locally Local repository
mvn deploy Publishes artifact remotely Remote repository

13. Maven Interview Questions

01.

What is Apache Maven?

02.

What is the Maven Build Lifecycle?

03.

What does mvn compile do?

04.

What does mvn test do?

05.

What happens during mvn package?

06.

Why use mvn clean package?

07.

What is package vs install?

08.

What is install vs deploy?

09.

Where is the Maven local repository?

10.

How does Maven fit into CI/CD?

14. The Easy Way to Remember Maven

Don't just memorize Maven commands. Connect every command with what it does.

mvn compile → Source code becomes compiled classes
mvn test → Validate the application through tests
mvn package → Create the application artifact
mvn clean → Remove previous generated build output
mvn clean package → Clean and create a fresh package
mvn install → Put the artifact into the local repository
mvn deploy → Publish the artifact to a configured remote repository

The DevOps Connection

Git → Maven → Test → JAR → Docker → CI/CD → Cloud

15. Maven FAQ

What is Maven in Java?

Maven is a build automation and project management tool commonly used for Java projects. It can manage dependencies, execute lifecycle phases, run tests and create application artifacts. 


 

What does mvn package do?

It executes the Maven lifecycle up to the package phase and creates the configured artifact, such as a JAR or WAR.

What does mvn clean do?

It removes files generated by previous builds, allowing the next build to start without the previous generated build output.

What is the difference between package and install?

Package creates the artifact. Install additionally places that artifact into the local Maven repository.

What is the difference between install and deploy?

Install places the artifact in the local Maven repository, while deploy publishes it to a configured remote Maven repository.

Where is the Maven local repository?

The default local Maven repository is commonly located at ~/.m2/repository.

Why is Maven important for DevOps?

Java applications commonly use Maven during their build process. Understanding Maven helps DevOps engineers work with CI/CD pipelines, Jenkins, GitHub Actions, Docker and cloud deployment.

Learn Today. Lead Tomorrow.

Build practical technology skills with EduArn through structured learning, hands-on projects and career-focused training.

Learn Java, Maven, DevOps, Cloud, CI/CD, Docker, Kubernetes, Jenkins, GitHub Actions and other technologies through practical learning.

EduArn also supports retail learners and corporate training programs for teams and organizations looking to build modern technology capabilities.

Visit EduArn.com

Final Thoughts

Maven is not simply a command-line tool for creating JAR files. The real value comes from understanding the lifecycle and knowing how each phase contributes to the software delivery process.

When you hear mvn compile, think compiled source code. When you hear mvn test, think automated validation. When you hear mvn package, think application artifact.

When you hear mvn install, think local Maven repository. When you hear mvn deploy, think configured remote artifact repository.

Don't just memorize Maven commands. Understand the journey from source code to artifact and how that artifact fits into modern CI/CD.

Git → Maven → Test → JAR → Docker → CI/CD → Cloud

Learn Today. Lead Tomorrow. Learn with EduArn.

🌐 eduarn.com


Topics: Apache Maven, Maven Lifecycle, Maven Build Lifecycle, Maven Tutorial, Maven Commands, mvn clean, mvn compile, mvn test, mvn package, mvn clean package, mvn install, mvn deploy, Java, Java Developer, DevOps, CI/CD, Jenkins, GitHub Actions, Docker, Cloud, Maven Interview Questions, Java DevOps

Maven Project Structure, POM.xml & Dependencies Explained | Beginner Guide

Maven Project Structure, POM.xml & Dependencies Explained | Beginner Guide

Maven Project Structure, POM.xml & Dependencies Explained: Complete Beginner Guide

If you are learning Java, Selenium, automation testing, or software development, you will probably come across Maven very quickly.

At first, a Maven project can look confusing. You may see folders such as src/main/java, src/test/java, resources folders, and a file called pom.xml.

Then you may ask:

  • What is the Maven project structure?
  • What is pom.xml?
  • Where should Java code go?
  • Where should test code go?
  • What are Maven dependencies?
  • How does Maven download libraries?
  • What do commands like mvn clean, mvn test, and mvn package do?

This guide explains all of these concepts in simple language with a practical Eduarn retail training project.

Watch the Maven Tutorial

Before we get into the details, you can watch the complete beginner-friendly video tutorial here:

In this video, we build a simple Maven project and explain the project structure, pom.xml, dependencies, and essential Maven commands.

What Is Maven?

Apache Maven is a build automation and dependency management tool commonly used for Java projects.

In simple terms, Maven helps you manage the libraries your project needs and provides commands for common tasks such as compiling code, running tests, cleaning build files, and packaging an application.

Imagine that your Java application needs several libraries. You might need JUnit for testing, Selenium for browser automation, Jackson for JSON processing, or Spring for application development.

Instead of manually downloading and managing every library, you can define what your project needs in pom.xml.

Simple idea:

Your project needs a library → define the dependency in pom.xml → Maven manages the dependency.

A Quick Maven Story

When I was working on my first project back in 2004, managing project libraries and configuration felt much more difficult.

There was more manual work involved, especially when you were new to the technology.

Today, tools such as Maven make this process much easier. With a properly configured pom.xml and a few Maven commands, you can manage dependencies, run tests, clean build output, and package your project.

If Maven looks confusing when you first start, don't try to learn everything at once. Begin with four concepts:

Project Structure → POM → Dependencies → Maven Commands

Maven Project Structure

Let's use a simple project called:

eduarn-retail-training

A typical structure can look like this:

eduarn-retail-training


│
├── pom.xml
│
└── src
├── main
│ ├── java
│ └── resources
│
└── test
├── java
└── resources

src/main/java

This is where your main Java application code normally lives.

For example:

package com.eduarn.retailtraining;


public class Product {

private String productName;
private double price;

public Product(String productName, double price) {
    this.productName = productName;
    this.price = price;
}

public double getPrice() {
    return price;
}


}

In our example, the package is:

com.eduarn.retailtraining

This keeps the example connected naturally with the Eduarn retail training project.

src/main/resources

This folder is generally used for resources required by the application.

Examples include:

application.properties


product-data.json
config.properties

src/test/java

This is where your Java test classes normally go.

For example:

@Test


void verifyProductPrice() {

Product product =
    new Product("Laptop", 50000);

System.out.println(product.getPrice());


}

src/test/resources

This folder can contain resources specifically required by your tests.

Easy rule to remember:

main = application

test = testing

What Is pom.xml?

One of the most important files in a Maven project is:

pom.xml

POM stands for Project Object Model.

You can think of pom.xml as the main instruction and configuration file for your Maven project.

It can define information such as:

  • Project identity
  • Project version
  • Java version
  • Project dependencies
  • Build plugins
  • Other Maven configuration

groupId

<groupId>com.eduarn</groupId>

The groupId generally identifies the organization or project group. In this example, we use com.eduarn.

artifactId

<artifactId>eduarn-retail-training</artifactId>

The artifactId identifies the Maven project or artifact.

version

<version>1.0-SNAPSHOT</version>

The version identifies the current version of the project.

Easy way to remember:

Group → Project → Version

What Are Maven Dependencies?

Dependencies are external libraries that your project needs.

For example, if our Eduarn retail training project needs JUnit for testing, we can define JUnit as a Maven dependency.

<dependency>

<groupId>org.junit.jupiter</groupId>

<artifactId>junit-jupiter</artifactId>

<version>5.10.2</version>

<scope>test</scope>


There are four important pieces here:

  • groupId — identifies the library group or provider.
  • artifactId — identifies the library.
  • version — specifies the library version.
  • scope — defines where the dependency is used.

Maven and Selenium

If you're learning Selenium automation with Java, Maven is commonly used to manage Selenium dependencies.

A Selenium dependency can look like this:

<dependency>

<groupId>org.seleniumhq.selenium</groupId>

<artifactId>selenium-java</artifactId>

<version>YOUR_VERSION</version>


The exact dependency version should be selected according to the version appropriate for your project.

Don't memorize the XML.

Understand the process:

Project needs a library → Add dependency → Maven manages it.

Complete Maven pom.xml Example

Here is a simple Maven configuration for our Eduarn retail training example:

<?xml version="1.0" encoding="UTF-8"?>




<modelVersion>4.0.0</modelVersion>

<groupId>com.eduarn</groupId>
<artifactId>eduarn-retail-training</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
    <maven.compiler.source>17</maven.compiler.source>
    <maven.compiler.target>17</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>

    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter</artifactId>
        <version>5.10.2</version>
        <scope>test</scope>
    </dependency>

</dependencies>


Important Maven Commands for Beginners

1. mvn clean

mvn clean

This removes previous build output, including the Maven target directory when applicable.

2. mvn test

mvn test

This compiles the relevant code and runs the project's tests during the Maven test phase.

3. mvn package

mvn package

This runs the Maven lifecycle through the package phase and creates the project's package output according to its build configuration.

Start with these three commands:

mvn clean → Clean

mvn test → Test

mvn package → Package

Maven Project Structure: Quick Recap

eduarn-retail-training


│
├── pom.xml
│
└── src
├── main
│ ├── java
│ └── resources
│
└── test
├── java
└── resources
  • src/main/java → Application code
  • src/main/resources → Application resources
  • src/test/java → Test code
  • src/test/resources → Test resources
  • pom.xml → Maven project configuration
  • Dependencies → Libraries required by the project

Why Learn Maven?

Maven is worth understanding because it appears frequently in Java development and automation projects.

Once you understand the project structure, POM file, dependencies, and basic Maven commands, many Java projects become easier to navigate.

This knowledge is especially useful when working with Java automation frameworks, Selenium projects, JUnit testing, CI/CD pipelines, and larger development teams.

Learn More with Eduarn

If you're interested in practical Java, Selenium, automation testing, DevOps, and retail technology training, explore more learning content from Eduarn.

Continue Learning

Watch the complete Maven tutorial above and follow along by creating your own Maven project.

What should we cover next?

  • Maven Lifecycle
  • Maven Plugins
  • Maven Profiles
  • Selenium + Maven Project
  • Complete Java Automation Framework

Comment below and let us know.

Final Takeaway

You don't need to become a Maven expert in one day.

Start by understanding these four concepts:

Project Structure → pom.xml → Dependencies → Maven Commands

Once these basics are clear, you can gradually move on to Maven lifecycle, plugins, profiles, Selenium automation, and larger Java projects.

Keep learning, keep building, and practice by creating small real-world projects.

🚀 Keep Learning with Eduarn

Explore more practical technology and retail training resources at Eduarn.com.

Build your skills one concept at a time and turn your learning into practical, real-world knowledge.

👉 Explore Eduarn.com