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


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
Source Code: Developers write
.java
files.Compilation: Java compiler converts
.java
→.class
(bytecode).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
TERM | DESCRIPTION |
Archetype | Template for project type (e.g., quickstart , webapp ) |
Group ID | Organization or domain name |
Artifact ID | Project name |
Version | Project version (SNAPSHOT for dev, RELEASE for prod) |
Packaging | Output packaging type: jar or war |
Dependencies | Required libraries (e.g., JUnit, Spring) |
Goals | Commands like clean , compile , package , install |
Repositories | Where 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 configurationsrc/main
: Business logicsrc/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>
GOAL | DESCRIPTION |
clean | Deletes target/ directory |
compile | Compiles .java to .class |
test | Runs unit tests (compile + test) |
package | Creates .jar or .war (compile + test+ create jar/war file) |
install | Installs artifact in local repo (~/.m2 ) |
deploy | Deploys 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
Feature | Manual | Maven |
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.
Subscribe to my newsletter
Read articles from Vaishnavi D directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
