Quote

Hi @all, it’s me again, long time no see, how is your new year? Let’s kickoff a bit energy with simple trying from my site with new technology and methodology to build and serve Java API with couple of tools, such as Maven, Tomcat and see how it work. Now, let’s digest for first blog of 2025

center

Java world and attachable technologies

Question

When I come up with new idea 2025, I think about learn around webserver for first start and luckily, I found to Tomcat cuz I have request from my old co-worker to help him see why his web hosted by Tomcat is not work, I give a bit time to see Tomcat work and It actually kinda interesting

But anyway, when I try to reach to Tomcat and I know this tool spent for Java, especially Java Servlet because I know this technology for the long time before when I learn that one in university, kinda old but yeah, I found some a bit idea to foster that become more enchanting.

center

Java and C# are both of big technology, I means there are a lot of differences to build up the product from those languages, you need to know about

  • Build tools
  • WebServer for serving
  • Integrate attachable
  • and more and more

That why make those language is becoming big and separate with DevOps who work with lot of stuff related Node, PHP, … But corresponded among languages, we always have multiple choice about technology for each topics above, and java is really insane. Let’s see what we got

Following LambdaTest - 19 Best Java Build Tools For Developers and BairesDevBlog - Top 5 Java Build Tools Compared about Build Tools for Java, you often see couple of things familiar, including

  • Apache Ant: A Java library and command-line tool whose mission is to drive processes described in build files as targets and extension points dependent upon each other (XML-based configurations)
  • Apache Maven (Recommendation): An open-source project management and build automation tool sed mainly forΒ Java projects (Declarative formats)
  • Gradle: An advanced build automation tool thanks to its powerful, flexible domain-specific language based on Groovy/Kotlin
  • Others way, we have CMake, Bazel, … but that not spend for all of us

Quote

When I hear from my friend, shout out to Tri An who super recommendation about Maven and talk Maven is most of powerful thing him work with as Java Developer Engineer

Next, I spend a couple mins and see about list of WebServer for Java Developed and It provides a lot of information about which one should be choice, explore at Baeldung - Web and Application Servers for Java

  • Apache Tomcat: One of the more popular web servers in the Java ecosystem
  • Jetty: A highly scalable and memory-efficient web server and servlet container, supporting many protocols such as HTTP/3,2,1 and WebSocket
  • Apache TomEE: A lightweight, yet powerful, JavaEE Application server with feature rich tooling.
  • Oracle WebLogic Server: Β A unified and extensible platform for developing, deploying, and running enterprise applications in Java for on-premises and in the cloud

Quote

Yeah, absolutely multiple optional for newbie when they want to start like me, so I ask my friend, again Tri An and him said about only Maven or should be attached with β€œTomcat” cuz Jetty is not good enough when compare between them.

Spend a bit time and I see couple of blogs write about e2e pipeline CI/CD with Java and he try to combine between Tomcat and Maven, so I catch this chance to setup and see how both them work πŸ˜„. BTW, you can explore those blogs below

But first of all, let learn a bit about Maven and Tomcat

About Maven and Tomcat

center

Info

As usual, when kick off something like practice or lab session, I will spend a little bit time to go through technology used and see what is it ?

Maven

center

Info

What is Maven?

Apache Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project’s build, reporting and documentation from a central piece of information.

Couple of primary goals of Maven, such as

  • Making the build process easy
  • Providing a uniform build system
  • Providing quality project information
  • Encouraging better development practices

Once information from documentation, β€œMaven is truly extend and reusable Ant scriptlets”. It seems kinda straightforward contribute, but I know that do such incredible thing cuz that truthful have high recognize from community, especially Java Developer, so happiness and totally appreciate for such incredible contributing πŸ™Œ

When you work with Maven, You will spend a bit time to learn and define configuration through XML below couple of files, including

  • POM (Project Object Model): An XML representation of a Maven project held in a file namedΒ pom.xml (Same as package.json (Node) or requirements.txt (Python))
  • Settings: settingsΒ element in theΒ settings.xmlΒ file contains elements used to define values which configure Maven execution in various ways, like theΒ pom.xml, but should not be bundled to any specific project, or distributed to an audience. These include values such as the local repository location, alternate remote repository servers, and authentication information.

It actually complex than what I know πŸ˜„, BTW we will turn back when we start a practical session, but let’s see are there any thing else. So, I found about Maven Repository, again that kinda same like npm, pypi, crates, and moreover. It seems like place where developer public package, library to serve developing process.

To run and execute Maven, this tool provides CLI, plugins, extension and you can try to install Maven in your shell through some links, including

Success

I think kinda enough for Maven, if i spend hour in Maven, I will share multiple information about this one and really confuse. Therefore, we will move to Tomcat before we turn back in the practical session

Tomcat

center

Info

What is Tomcat ?

Apache Tomcat is a free and open-source implementation of the Jakarta Servlet, Jakarta Expression Language, and WebSocket technologies. It provides a β€œpure Java” HTTP web server environment in which Java code can also run. Thus it is a Java web application server, although not a full JEE application server.

Following the Wikipedia - Apache Tomcat, they talk about these components implement and provide by Tomcat, such as

  • Catalina: Tomcat’sΒ servlet container. Catalina implementsΒ Sun Microsystems’ specifications forΒ servletΒ and JavaServer Pages (JSP).
  • Coyote: A Connector component for Tomcat that supports the HTTP 1.1 and 2 protocol as a web server
  • Jasper: Tomcat’s JSP Engine. JasperΒ parsesΒ JSPΒ files to compile them into Java code as servlets (that can be handled by Catalina).
  • Cluster: Β Used forΒ load balancingΒ that can be achieved through many techniques
  • High availability: Facilitate the scheduling of system upgrades (e.g. new releases, change requests) without affecting the live environment.
  • Plus more - Valve: Pluggable components for intercepting requests at any point in the processing pipeline.

Thank to Jimin who contribute legit helpful articles about Tomcat Architecture and Configuration with detailed explanation at

From those articles, I collect some cool stuff about Tomcat. From my perspective, Tomcat have to maintain and approach the configuration with XML, so we need to have experience when we work with Tomcat as well. There are four files to concern when we work with Tomcat WebServer

Configuration FileScopeFunctionality
server.xmlGlobalConfigures Tomcat’s server-wide settings such as connectors, thread pools, virtual hosts, and global servlet definitions.
context.xmlApplication-Specific or GlobalConfigures resources and environment settings for individual web applications or globally across all web applications.
web.xmlApplication-SpecificDefines how each web application handles requests and responses, including servlets, filters, welcome files, and error pages.
tomcat-users.xmlGlobalDefines user accounts and roles for authentication and authorization purposes within Apache Tomcat.

Source: Jimin

Success

Once upon again, we will reach to detail for what you need to configure with Tomcat server, so turn back in next part, practical session to see how we can work with both Maven and Tomcat

Practical Session

Question

Now, we are kicking off the practical session, in this session, I will focus inside run spring boot java application through artifact application of Maven and deploying into Tomcat Server. That one will totally encapsulate inside docker container, just build and run.

First of all, I am not familiar with Java in my daily job, I have bit experience when I lay in university, that why I can code Java but can’t familiar or expertise to setup Spring Boot Framework project as well for example, but don’t worry I always try to learn and found some source code, and we can inspect together.

center

Therefore, today I found once for this lab, shout out to shawon100 who contribute the detailing concept and repository like lab expectation, Explore more at Spring-Boot-Maven-Tomcat-Docker

Prerequisites

First of all, we need to figure out project what is it, learn and inspect deeper inside. So we will separate that into two progress, run with local and run through docker. To handle this situation you need to setup and install couple of tools, including

  • Docker
  • Java
  • Maven
  • Linux VM or WSL2

Warning

To easier approach and follow my step, you need to use Linux OS with couple distro about Debian like Ubuntu Or Debian. If you are window user, you can use Linux through WSL2

For install Docker, that is a simple task and you just need to paste this command and Docker will be on your host

curl -fsSL https://get.docker.com | sudo bash -

Next, you need to install Java and Maven, in my situation I will try to install maven version 3.9.9 (LTS Version) and it doesn’t specific particular java JDK, so I will try to install JDK18 to see what going on πŸ˜ƒ. BTW, you can double check about Version Range Specification

With Java, you can install via apt package with simple command

sudo apt install openjdk-18-jdk –y 

Wait a bit and you have java version 18 in your host, double check with command

java --version

Afterward, we will install Maven and version specific is 3.9.9. We can easily install by couple of commands

# Download maven 3.9.9
wget https://dlcdn.apache.org/maven/maven-3/3.9.9/binaries/apache-maven-3.9.9-bin.tar.gz
 
# Extract compress file
tar -xzf apache-maven-3.9.9-bin.tar.gz
 
# Move decompress into /usr/local
sudo mv apache-maven-3.9.9 /usr/local
 
# Serve PATH for bin folder of Maven 3.9.9 (Edit inside .zshrc or .bashrc)
export PATH="$PATH:/usr/local/apache-maven-3.9.9/bin"

Now you can have Maven in your local machine, double check with command

mvn --version

Note

You can add mvn into plugin Oh-my-zsh if used to execute completion mode for Maven in your shell

Now you environment in local machine is ready to practice. Reach next part to see what happen

Play with Project

First of all, you need to ensure clone code from repository

git clone https://github.com/shawon100/Spring-Boot-Maven-Tomcat-Docker.git

First insight, we can take look about structure of project

Spring-Boot-Maven-Tomcat-Docker/
β”œβ”€β”€ .gitignore
β”œβ”€β”€ Dockerfile
β”œβ”€β”€ LICENSE
β”œβ”€β”€ README.md
β”œβ”€β”€ docker-compose.yml
β”œβ”€β”€ pom.xml
└── src
    └── main
        β”œβ”€β”€ java
        β”‚Β Β  └── com
        β”‚Β Β      └── shawon
        β”‚Β Β          └── dockerdemo
        β”‚Β Β              β”œβ”€β”€ DockerDemoApp.java
        β”‚Β Β              └── MyController.java
        β”œβ”€β”€ resources
        β”‚Β Β  └── application.properties
        └── webapp
            └── WEB-INF
                └── jsp
                    └── index.jsp

As you can see, we have few thing to inspect, such as

  • Dockerfile
  • pom.xml
  • DockerDemoApp.java and MyController.java
  • index.jsp

If you take a look about java and jsp, the website is kinda simple API with GET Method. When we hit into that URL, it will return the text Welcome + parameter name=?

So we reach to pom.xml and see what is it, you need to take POM (Project Object Model) documentation to understand more configuration

pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.2.5.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.shawon</groupId>
	<artifactId>DockerDemoApp</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>DockerDemoApp</name>
	<description>Demo project for Spring Boot Webapp deployed in Tomcat running in Docker</description>
	<packaging>war</packaging>
 
	<properties>
		<java.version>11</java.version>
	</properties>
 
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.apache.tomcat.embed</groupId>
			<artifactId>tomcat-embed-jasper</artifactId>
		</dependency>
 
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
			<scope>provided</scope>
		</dependency>
	</dependencies>
 
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<finalName>sample</finalName>
				</configuration>
			</plugin>
		</plugins>
	</build>
 
</project>

When take a look, we can see couple of things to concern inside this file

  • POM version 4.0.0
  • Java version 11
  • Install package include
    1. spring-boot-starter-web
    2. tomcat-embed-jasper
    3. spring-boot-starter-tomcat (provided) - a container to provide it at runtime
  • Build plugin spring-boot-maven-plugin for Spring Boot and Maven
  • In final, artifact build will name to sample.war

Now we try to build the artifact file for this project

mvn clean package

After wait about 1-2 mins for download package and build war file inside target/ folder

target
β”œβ”€β”€ DockerDemoApp-0.0.1-SNAPSHOT
β”‚Β Β  β”œβ”€β”€ META-INF
β”‚Β Β  └── WEB-INF
β”œβ”€β”€ DockerDemoApp-0.0.1-SNAPSHOT.war
β”œβ”€β”€ classes
β”‚Β Β  β”œβ”€β”€ application.properties
β”‚Β Β  └── com
β”œβ”€β”€ generated-sources
β”‚Β Β  └── annotations
β”œβ”€β”€ maven-archiver
β”‚Β Β  └── pom.properties
β”œβ”€β”€ maven-status
β”‚Β Β  └── maven-compiler-plugin
└── sample.war

As you can see sample.war is archive and now you can run this file to expose WEB API

Following the discussion about StackOverFlow - How do I run a class in a WAR from the command line?, you have two ways to serve your website

Run with standalone command (Not Recommend)

java -jar target/sample.war
  • This command will host Tomcat server with one embed inside build progress
  • But it will expose exception about java.io.IOException: Unable to open root Jar file cuz we don’t provide enough lib for this operation

Info

But It seems like not have any problem, so we can consider to run for testing. BTW, we have another way can be better.

Run with bunch of commands to use class inside build (Recommend)

This methodology look simple as well, so you need to follow couple of step to operate your WEB API

  1. Unwrap the War file
jar -xvf target/sample.war

After step unwrap, you can see folder WEB-INF and META-INF created, you can use this one to execute your web in next step

  1. Redirect into WEB-INF directory
cd WEB-INF
  1. Read MANIFEST.MF in META-INF folder to get Start-Class
MANIFEST.MF
Manifest-Version: 1.0
Created-By: Maven Archiver 3.4.0
Build-Jdk-Spec: 18
Implementation-Title: DockerDemoApp
Implementation-Version: 0.0.1-SNAPSHOT
Main-Class: org.springframework.boot.loader.WarLauncher
Start-Class: com.shawon.dockerdemo.DockerDemoApp
Spring-Boot-Version: 2.2.5.RELEASE
Spring-Boot-Classes: WEB-INF/classes/
Spring-Boot-Lib: WEB-INF/lib/
  1. Run your website with command
java -classpath "lib/*:classes/." com.shawon.dockerdemo.DockerDemoApp 

Docker with Tomcat Server

Question

When you run standalone website, you need to decompress or use webserver to process war file and Tomcat is one handler of this.

But it’s not cover the strategy encapsulate and easier to deliver, so we can try to use Tomcat to become factor and help WEB API smoothly deliver and deploy.

So with strategy of project, author have write and encapsulate whole project into Docker Image and It’s make project become easier to deliver and deploy

#Build Stage
FROM maven:3.6.3-openjdk-11-slim AS build
 
RUN mkdir -p /usr/local/app
 
COPY ./src /usr/local/app/src
 
COPY ./pom.xml /usr/local/app/pom.xml
 
WORKDIR /usr/local/app/
 
RUN mvn -Dmaven.test.skip=true clean package
 
# Publish Stage
FROM tomcat:8.5-jdk11-openjdk-slim
COPY --from=build /usr/local/app/target/sample.war  /usr/local/tomcat/webapps/
EXPOSE 8080
CMD ["catalina.sh", "run"]
  • Separate into 2 stage Build and Publish
  • Copy manifest sample.war from Build to Publish Stage
  • Use Tomcat to serve compress war file inside webapps folder
  • Use catalina.sh to run servlet container. Explore more about bin/catalina.sh

Note

For more information and script to interact with tomcat, you can explore at inside source code of Tomcat at GitHub - Tomcat

Now we can build docker image and try to run that to see what happen

docker build -t maven-tomcat:latest -f Dockerfile .

Wait a bit for build docker image success and after that we will run to see how it work

docker run -d -p 8080:8080 --name sample-maven-tomcat maven-tomcat:latest

Success

It works and now we can serve Spring Boot API with Tomcat Server

Conclusion

center

Success

That’s all for this weekend, hope you find well information and enjoy your time to read my blog. New year for new challenge, new things to learn and truly want to contribute ever and ever, so that is my honor to keep my sprint, hold on the discipline and hopefully y’all guys stand with me for new things in this year 2025.

Quote

Happy to turn back soon with y’all guys, next week will have some special things, I move on for new job and let’s see what thing to motivate new technology fields. Therefore, stay safe, keep learning and we will see each others next weekend. Happy new year and Bye πŸ‘‹