Day 12 of 90 Days of DevOps Challenge: Build and Deploy Smarter with Apache Maven

Vaishnavi DVaishnavi D
4 min read

Introduction

Apache Maven is a powerful build automation tool primarily used for Java projects. As a DevOps engineer or Java developer, understanding Maven helps automate build processes, manage dependencies, and streamline deployments.

What is Java?

Java is a high-level, object-oriented programming language, developed by Sun Microsystems in 1991. Using Java, developers can build:

  • Standalone Applications: Run on a single machine (e.g., Calculator, VS Code)

  • Web Applications: Accessible via the internet (e.g., Gmail, LinkedIn)

Java Project Execution Flow

  1. Source Code: Developers write .java files.

  2. Compilation: Java compiler converts .java.class (bytecode).

  3. Packaging:

    • .jar for standalone apps (Java archive)

    • .war for web apps (web archive)

Execution:

  • Run .jar directly.

  • Deploy .war to a server (e.g., Tomcat).

Note: Java applications involve Build + Deployment.

Manual vs Automated Build Process

Manual Build Steps:

  • Download libraries (spring, Junit)

  • Configure build path

  • Compile source (.java to .class)

  • Run tests (Junit)

  • Package project as jar/war file

Challenges: Time-consuming and error prone.

Solution: Use Build Tools like Apache Maven to automate this process.

What Maven can do

  • Project creation and folder structure setup

  • Automatic dependency management

  • Source compilation and testing

  • Packaging as .jar or .war

  • Deployment to repositories

Maven Setup on Amazon Linux

# Step 1: Connect to EC2 instance
ssh ec2-user@<your-ec2-ip>

# Step 2: Install Maven
sudo yum install maven -y

# Step 3: Verify installation
mvn -version

NOTE: Maven also installs Java if not already present.

Maven Terminology

TERMDESCRIPTION
ArchetypeTemplate for project type (e.g., quickstart, webapp)
Group IDOrganization or domain name
Artifact IDProject name
VersionProject version (SNAPSHOT for dev, RELEASE for prod)
PackagingOutput packaging type: jar or war
DependenciesRequired libraries (e.g., JUnit, Spring)
GoalsCommands like clean, compile, package, install
RepositoriesWhere dependencies are stored: Local, Central, Remote

Creating a Standalone Application with Maven

mvn archetype:generate \
  -DgroupId=in.ZeroToRoot \
  -DartifactId=learning-app \
  -DarchetypeArtifactId=maven-archetype-quickstart \
  -DarchetypeVersion=1.4 \
  -DinteractiveMode=false

Project Structure:

learning-app/
├── pom.xml
└── src/
    ├── main/java/
    └── test/java/
  • pom.xml: Maven configuration

  • src/main: Business logic

  • src/test: Unit tests

Maven Build Lifecycle (Goals)

Maven provides specific goals that are used to automate and control different phases of the project build process.

How to Run Maven Goals

cd <project-directory>
mvn <goal>
GOALDESCRIPTION
cleanDeletes target/ directory
compileCompiles .java to .class
testRuns unit tests (compile + test)
packageCreates .jar or .war (compile + test+ create jar/war file)
installInstalls artifact in local repo (~/.m2)
deployDeploys artifact to remote repo

Example:

cd learning-app
mvn clean install

Creating a Web Application Using Maven

mvn archetype:generate \
  -DgroupId=in.ZeroToRoot \
  -DartifactId=sbi-webapp \
  -DarchetypeArtifactId=maven-archetype-webapp \
  -DarchetypeVersion=1.4 \
  -DinteractiveMode=false

Build & Package:

cd sbi-webapp
mvn clean package

NOTE: WAR file will be available in target/ directory.

Maven Dependencies

To include external libraries, add them to pom.xml:

<dependencies>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>6.1.7</version>
  </dependency>
</dependencies>

Find dependencies on: https://mvnrepository.com

After editing pom.xml, run:

mvn clean package

Maven Repositories

NOTE: Maven checks the local repo first. If not found, it downloads from central/remote.

Quick recap

FeatureManualMaven
Download Dependencies
Compile Code
Run Tests
Package Project
Automate Workflow

Final thoughts

Learning Maven gave me a clear understanding of how Java projects are built and managed efficiently. Initially, the manual build process felt tedious, but Maven’s automation simplified everything from dependency management to packaging. As a DevOps learner, it felt rewarding to see how a simple command like mvn clean install could streamline the entire workflow. This tool has made backend processes clearer and more organized. Maven isn't just a build tool, it's a vital part of a smoother DevOps pipeline.

5
Subscribe to my newsletter

Read articles from Vaishnavi D directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Vaishnavi D
Vaishnavi D