Build and Deploy a Java Web Application to Tomcat
A simple two-stage GitLab pipeline that builds MyWebApp.war and deploys it to the existing Tomcat server on port 8090.
Configure GitLab CI/CD to build the existing Lab 6 Maven project and deploy the generated WAR file directly to the existing Tomcat server.
Lab Assumptions
This lab assumes the following components are already working:
- The Java project is already available in the repository connected to GitLab.
- The Maven project is located in
MyWebApp. - The POM file is located at
MyWebApp/pom.xml. - The Maven build generates
MyWebApp/target/MyWebApp.war. - Tomcat is already installed, running, and reachable on port
8090. - The Tomcat Manager application is already configured.
- The existing Tomcat username and password used by the Jenkins Lab 6 deployment are available.
- GitLab-hosted runners are enabled for the project.
CI/CD Flow
Developer pushes code
|
v
GitLab pipeline starts
|
v
Maven builds MyWebApp
|
v
MyWebApp.war is created
|
v
GitLab saves the WAR as an artifact
|
v
Deploy job downloads the WAR
|
v
WAR is uploaded to Tomcat on port 8090
|
v
Application runs at /MyWebApp
Repository Structure
The repository should look similar to this:
project-root/
├── .gitlab-ci.yml
└── MyWebApp/
├── pom.xml
└── src/
├── main/
└── test/
Step 1 — Add the Tomcat Variables in GitLab
Open the GitLab project and navigate to:
Add only the following three variables. The application directory, WAR path, and Tomcat context are fixed directly in the pipeline, so students do not need to create variables for them.
| Variable | Value |
|---|---|
TOMCAT_URL |
http://YOUR-TOMCAT-IP:8090 |
TOMCAT_USER |
The same Tomcat username used in Jenkins |
TOMCAT_PASSWORD |
The same Tomcat password used in Jenkins |
You do not need to create
APP_DIR, WAR_FILE, or TOMCAT_CONTEXT in GitLab. The pipeline already uses MyWebApp, MyWebApp/target/MyWebApp.war, and /MyWebApp directly.
/manager or /MyWebApp to TOMCAT_URL.
Example:
TOMCAT_URL=http://20.120.45.90:8090
Set TOMCAT_PASSWORD to Masked and hidden.
Step 2 — Create the GitLab Pipeline File
Create the following file at the root of the repository:
.gitlab-ci.yml
Paste the following pipeline into the file:
stages:
- build
- deploy
variables:
APP_DIR: "MyWebApp"
WAR_FILE: "MyWebApp/target/MyWebApp.war"
TOMCAT_CONTEXT: "/MyWebApp"
build:
stage: build
image: "maven:3.9.16-eclipse-temurin-21-noble"
script:
- mvn -B -ntp -f "$APP_DIR/pom.xml" clean install
artifacts:
paths:
- "$WAR_FILE"
deploy:
stage: deploy
image: "alpine:3.22"
needs:
- job: build
artifacts: true
before_script:
- apk add --no-cache curl
script:
- >
curl
--user "$TOMCAT_USER:$TOMCAT_PASSWORD"
--upload-file "$WAR_FILE"
"${TOMCAT_URL}/manager/text/deploy?path=/MyWebApp&update=true"
How the Pipeline Works
Build Job
The build job uses Maven and Java 21 to run:
mvn clean install
The command reads:
MyWebApp/pom.xml
and generates:
MyWebApp/target/MyWebApp.war
GitLab saves the WAR file as a job artifact.
Deploy Job
The deploy job downloads the WAR artifact and uploads it to:
http://YOUR-TOMCAT-IP:8090/manager/text/deploy?path=/MyWebApp&update=true
The application is deployed under:
/MyWebApp
The update=true parameter allows Tomcat to replace the existing deployment.
Step 3 — Commit and Push the Pipeline
Run:
git add .gitlab-ci.yml git commit -m "Add GitLab build and Tomcat deployment" git push origin main
Step 4 — Monitor the Pipeline
In GitLab, navigate to:
You should see two stages:
build └── build deploy └── deploy
The expected flow is:
build: passed
|
v
deploy: passed
Step 5 — Verify the Application
Open the deployed application in a browser:
http://YOUR-TOMCAT-IP:8090/MyWebApp
Example:
http://20.120.45.90:8090/MyWebApp
Step 6 — Test Continuous Deployment
Edit a visible application file such as:
MyWebApp/src/main/webapp/index.jsp
Change the page content, then run:
git add . git commit -m "Update MyWebApp page" git push origin main
GitLab will automatically:
- Start a new pipeline.
- Build the application.
- Create
MyWebApp.war. - Save the WAR as an artifact.
- Deploy the WAR to Tomcat on port
8090. - Replace the existing
/MyWebAppapplication.
Refresh the application URL to see the updated page.
Every pipeline builds the Maven project and deploys the resulting WAR file to the existing Tomcat server.
Final Workflow
Git Push | v GitLab-Hosted Runner | v mvn clean install | v MyWebApp.war | v GitLab Artifact | v Tomcat Manager on Port 8090 | v /MyWebApp
No comments:
Post a Comment