Setting Up Java Web Calculator with Maven and Tomcat

Table of contents
- Introduction
- Why This Build Tool is Useful for Companies
- Step 1: Update System Packages
- Sep 2: Install Java Development Kit (JDK)
- Step 3: Install Maven
- Step 4: Clone the Java Web Calculator Project
- Step 5: Build the Java Web Application
- Step 6: Download and Install Apache Tomcat
- Step 7: Deploy Java Web Calculator to Tomcat
- Conclusion
Introduction
This guide walks you through setting up a Java Web Calculator application using Maven, Git, and Apache Tomcat. Follow these steps to install dependencies, build the project, and deploy it on Tomcat.
Why This Build Tool is Useful for Companies
Using Maven as a build tool provides several advantages for companies, such as:
Dependency Management: Automatically handles project dependencies and ensures compatibility.
Standardized Build Process: Simplifies building, testing, and packaging Java applications.
Automation & Integration: Easily integrates with CI/CD pipelines for automated deployment.
Scalability: Supports large enterprise applications with modular project structures.
Consistency: Provides a uniform development environment across teams.
Step 1: Update System Packages
Before installing any dependencies, update your package list:
sudo apt update
Sep 2: Install Java Development Kit (JDK)
Since the project requires OpenJDK 17, install it using:
sudo apt install openjdk-17-jdk -y
Verify the installation:
java --version
Step 3: Install Maven
Maven is required to build the Java Web Calculator project. Install it using:
sudo apt install maven -y
Check if Maven is installed correctly:
mvn --version
Step 4: Clone the Java Web Calculator Project
Use Git to clone the repository:
git clone https://github.com/DevOps-Siba/JavaWebCalculator
Navigate into the project directory:
cd JavaWebCalculator/
ls -l
Step 5: Build the Java Web Application
- Validate the project:
mvn validate
- Compile the Java source code:
mvn compile
- Run unit test:
mvn test
- Package the application into a war file:
mvn package
- Run verify the build:
mvn verify
- Install dependencies into the local Maven repository:
mvn install
Check the target/ directory to confirm that the .war file has been generated:
cd target/
ls
Step 6: Download and Install Apache Tomcat
Download Apache Tomcat 9:
wget https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.102/bin/apache-tomcat-9.0.102.tar.gz
Extract the archive:
tar -xvf apache-tomcat-9.0.102.tar.gz
Rename and move the extracted folder to /opt/
:
mv apache-tomcat-9.0.102 tomcat9
sudo cp -r tomcat9/ /opt
Change ownership of the directory:
sudo chown -R ubuntu:ubuntu /opt/tomcat9/
Step 7: Deploy Java Web Calculator to Tomcat
- Copy the generated .war file to Tomcat’s
webapps
directory:
cp target/*.war /opt/tomcat9/webapps/
- Start Tomcat:
/opt/tomcat9/bin/startup.sh
- Access the application in your browser:
http://<your-server-ip>:8080/JavaWebCalculator
Conclusion
Following these steps, you have successfully set up and deployed the Java Web Calculator using Maven and Apache Tomcat. If you face any issues, check the Tomcat logs:
cat /opt/tomcat9/logs/catalina.out
Using Maven helps companies streamline their software development lifecycle, improve code maintainability, and enhance team collaboration. This ensures faster development, testing, and deployment of Java applications.
Subscribe to my newsletter
Read articles from Sibananda Pradhan directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
