A Practical Introduction to Maven Build Tool

1. What is Maven?

Apache Maven is a build automation tool used to generate artifacts such as .jar, .war, and .ear files. It is primarily used for Java projects.

Maven helps automate the complete software development lifecycle, including:

  • Compiling source code

  • Running tests

  • Packaging the application

  • Deploying the artifact to a repository or server

Note: pom.xml is the main file for all Maven-based projects it acts as the heart of the build system

Why Do We Need Maven?

Suppose you clone a Java project from GitHub the code is just a collection of .java files (source code).
But you cannot deploy raw source code to a server like Tomcat or JBoss.

You need to:

  1. Compile the code

  2. Download required libraries (dependencies)

  3. Run tests

  4. Package it into a deployable .jar or .war

  5. Optionally push it to a repository (e.g., Nexus)

  • Doing all of this manually can lead to errors and takes a lot of time.

  • This is where Maven helps it automates everything from build to deploy.

Key Features:

  • Open Source – Maintained by the Apache Software Foundation

  • Cross-Platform – Works on Linux, Windows, and macOS

  • Convention over Configuration – Reduces setup and boilerplate

  • Dependency Management – Automatically pulls required libraries

  • Extensible with Plugins – Test, package, deploy, report, and more

  • Standard Directory Structure – Easy to understand and maintain

Build Tool by Language

  • Java: Ant, Maven, Gradle

  • Python: Pybuilder

  • .NET: Nant, MSBuild

  • Ruby: Rake

  • Go: Go build tool


2. JAR, WAR, EAR – What Are They?

When you write Java code, after compiling and building, you get a package called an artifact.

There are 3 common types:

What are the differences between Ear , Jar and War files?

1. .jar – Java Archive

  • Standalone Java Applications

  • A .jar file is a compressed archive (similar to a .zip file) that contains compiled Java code.

  • All .class files (Java bytecode), which are generated by compiling .java source files, are bundled together.

  • It may also include:

  • Configuration files like .properties or .xml

  • An optional metadata file: META-INF META-INF/MANIFEST.MF

Purpose:

  • To package and distribute Java programs easily.

  • Allows running Java applications directly from the command line:

Key Point:

  • A .jar file is like a container for Java logic.

  • It does not typically include web content like HTML or JSP files.


2. .warWeb Application Archive

  • Used for: Web Applications

  • A .war file is a compressed archive used to package:

  • Backend Java logic (.class files, servlets)

  • Frontend files (HTML, CSS, JavaScript, images)

  • These are all bundled into a single .war file for deployment.

  • Contents of a typical .war file:

  • WEB-INF/ folder

  • web.xml (deployment descriptor)

  • Compiled .class files

  • Static files like index.html, style.css, script.js, images, etc.

Deployment:

You deploy a .war file to a Java application server like:

  • Apache Tomcat

  • JBoss

  • GlassFish


3. .earEnterprise Archive

Used for: Big Enterprise Applications

  • In real-world big projects (like banking or government apps), you need multiple modules:

    • Web Module (.war)

    • Logic/Service Module (.jar)

  • These are combined together into .ear

  • .ear = .war + .jar (multiple modules together)

Deployed to heavy enterprise servers like:

  • JBoss

  • WebLogic

  • WebSphere


3. Apache Maven Installation on EC2 (Red Hat)

System Requirements

  • Maven Version: 3.9.x or above

  • Java Requirement: JDK 8 or higher (Java is required before Maven)

  • EC2 Instance Type: t2.micro is sufficient

  • Memory: No specific requirement

  • OS: Platform-independent (Linux / Windows supported)

  • Note: External software will be installed in /opt directory

Step 1: Launch EC2 & Connect

  • Launch an Amazon EC2 instance (Redhat)

  • SSH into the instance:

Step 2: Switch to Root User

sudo su -

Step 3: Install Java (JDK)

Java is required before installing Maven, because Maven runs on Java.

1.Update system packages (updates all installed software):

yum update -y

2. Install JDK 21

yum install java-21-openjdk-devel -y

3. Verify Java Installation

java --version

Step 4: Install Maven

Make sure Java is installed before this step.

1. Install tools to download and extract Maven:

yum install wget unzip -y

2. Download & Extract Maven

  • It is a good practice to install external tools like Maven in the /opt directory

  • Download Maven from the official Apache website:

  • Always use the official source: https://maven.apache.org/download.cgi

cd /opt
wget https://dlcdn.apache.org/maven/maven-3/3.9.10/binaries/apache-maven-3.9.10-bin.zip

  • Unzip the Downloaded File

  • After the download, you will see the file: apache-maven-3.9.10-bin.zip. Unzip that file.

unzip apache-maven-3.9.10-bin.zip

  • After extraction, a new directory will be created:

Why mvn -v Might Not Work After Installation

  • Even after successful extraction, if you try: mvn --version

  • You might get: mvn: command not found

  • Reason: Because the mvn executable is not yet part of your system’s PATH, the terminal doesn’t know where to find it.

Step 5. Set Environment Variables

Open .bash_profile:

vi ~/.bash_profile

Add the following lines at the end:

export M2_HOME=/opt/apache-maven-3.9.10
export PATH=$PATH:$M2_HOME/bin

  • export M2_HOME=/opt/apache-maven-3.9.10
    → This line tells the system where Maven is installed. Some tools and scripts use this variable to locate Maven automatically.

  • export PATH=$PATH:$M2_HOME/bin
    → This line updates the system PATH so that you can run the mvn command from anywhere in the terminal, without typing the full path like /opt/apache-maven-3.9.10/bin/mvn.

Save and exit.

Step 6. Reload .bash_profile

This loads your changes into the current terminal session, next Verify Maven Installation

source ~/.bash_profile
mvn --version
# or
mvn -v


4.Maven Directory Structure (After Installation)

When you extract Maven, you'll see a folder like this:

apache-maven-<version>/
├── bin/
├── boot/
├── lib/
├── conf/

bin/Executable Scripts

  • Contains executable files like mvn (Linux) or mvn.cmd (Windows)

  • This is how you run Maven commands from terminal

boot/Startup Libraries

  • Contains internal libraries used to bootstrap Maven.

  • Example: plexus-classworlds-*.jar — helps load Maven core classes.

  • These are essential for Maven to start correctly.

lib/Maven Core Libraries

  • Contains all the essential .jar files Maven needs to run.

  • Example files:

    • maven-core.jar

    • maven-model.jar

    • maven-plugin-api.jar

  • These libraries define how Maven works internally.

conf/Configuration Directory

  • Contains global configuration files for Maven.

  • Most important file: settings.xml

This is the most important file inside the conf/ directory.

What Can You Configure in settings.xml?

1. Proxies

  • Use case: If your system is behind a firewall (e.g., corporate network) and you need Maven to access the internet.

  • You can define proxy host, port, username, and password.

2. Remote Repositories

  • These are external repositories from where Maven downloads dependencies or uploads your own artifacts.

  • Examples: Nexus ,JFrog Artifactory

  • You can configure the repository URL and credentials.

3. Server Authentication (Credentials)

  • You can securely store login credentials in settings.xml for:

  • Nexus (for publishing your .jar, .war, .ear files)

  • SonarQube (for uploading code quality reports)

1
Subscribe to my newsletter

Read articles from Kandlagunta Venkata Siva Niranjan Reddy directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Kandlagunta Venkata Siva Niranjan Reddy
Kandlagunta Venkata Siva Niranjan Reddy