Project: Maven WAR Deploy

Introduction

This lab walks through an end-to-end Maven WAR deploy: build a small API WAR, copy it to Tomcat webapps/, verify with curl, and redeploy after a code change. It consolidates Your First Servlet Application and Deploying WAR into one repeatable checklist.

Prerequisites

Project Goals

  • Maven packaging>war</packaging> project blog-api
  • Servlet GET /posts returns JSON-ish plain text
  • Deploy blog-api.war → context /blog-api
  • Redeploy v2 without leaving stale files

Step 1: Create Project

bash
mkdir -p blog-api/src/main/java/com/example/blog
mkdir -p blog-api/src/main/webapp/WEB-INF
cd blog-api

pom.xml:

PostsServlet.java:

Step 2: Build

bash
mvn clean package
jar tf target/blog-api.war | grep PostsServlet

Expected: WEB-INF/classes/com/example/blog/PostsServlet.class

Step 3: Deploy to Tomcat

bash
export CATALINA_HOME=/opt/tomcat
$CATALINA_HOME/bin/shutdown.sh
rm -rf $CATALINA_HOME/webapps/blog-api $CATALINA_HOME/webapps/blog-api.war
cp target/blog-api.war $CATALINA_HOME/webapps/
$CATALINA_HOME/bin/startup.sh

Step 4: Verify

bash
curl -s http://127.0.0.1:8080/blog-api/posts
curl -I http://127.0.0.1:8080/blog-api/
grep -i blog-api $CATALINA_HOME/logs/catalina.$(date +%Y-%m-%d).log

Pass criteria:

  • JSON body contains Hello WAR
  • Log shows deployment of /blog-api

Step 5: Redeploy v2

Change response string to Hello WAR v2, then:

bash
mvn clean package
$CATALINA_HOME/bin/shutdown.sh
rm -rf $CATALINA_HOME/webapps/blog-api $CATALINA_HOME/webapps/blog-api.war
cp target/blog-api.war $CATALINA_HOME/webapps/
$CATALINA_HOME/bin/startup.sh
curl -s http://127.0.0.1:8080/blog-api/posts

Confirm v2 appears—proves you replaced both WAR and exploded directory.

Acceptance Checklist

  • mvn package succeeds
  • WAR name matches desired context /blog-api
  • curl hits /blog-api/posts, not /posts alone
  • Redeploy removes old webapps/blog-api/ folder
  • No SEVERE in logs/catalina.*.log

What You Learned

SkillChapter reference
Maven WAR layout08
Context path = WAR name07
Clean redeploy07

Next

Project: External docBase