Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save rajgupta2/bb30e83fc9bedf07aa1f2c4d1090ab13 to your computer and use it in GitHub Desktop.

Select an option

Save rajgupta2/bb30e83fc9bedf07aa1f2c4d1090ab13 to your computer and use it in GitHub Desktop.
πŸš€ This Gist covers how to build and package the java web application using Maven, and how to deploy it on an Apache Tomcat server.

πŸ› οΈ How to build the java web application using Maven, and how to deploy it on an Apache Tomcat server

Let's understand some basic

What is Build?

It is a compilation process to compile all the source code and package it as a jar or war file. It removes all the uncessary code such as comments and spaces. It make the optimized version of source code.

What is Maven?

Maven is aka as Java Build tool, used to build and package the java application as jar or war file. It is also used to manage dependency. Java is required to run maven.


1. Install Java

wget https://download.oracle.com/java/24/latest/jdk-24_linux-x64_bin.tar.gz
tar -xvf jdk-24_linux-x64_bin.tar.gz
vim .bashrc
#add these two lines in .bashrc file to add environment variables so that the user can access their command from anywhere
export JAVA_HOME="$HOME/jdk-24.0.1"
export PATH="$JAVA_HOME/bin:$PATH"
#For the changes to take effect, update the .bashrc file using the source command:
source .bashrc
java --version

2. Install Maven

wget https://dlcdn.apache.org/maven/maven-3/3.9.9/binaries/apache-maven-3.9.9-bin.tar.gz
tar -xvf apache-maven-3.9.9-bin.tar.gz
vim .bashrc
#add these two lines to add environment variables so that the user can access their command from anywhere
export MAVEN_HOME="$HOME/apache-maven-3.9.9"
export PATH="$JAVA_HOME/bin:$MAVEN_HOME/bin:$PATH"
#For the changes to take effect, update the .bashrc file using the source command:
source .bashrc
mvn -v

3. Now create a sample maven application and then build and package the application using Maven Goal

mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-webapp -DarchetypeVersion=1.5
#name the artifact id as <your-project-name>, I have given here as mvn-app
cd mvn-app
mvn compile package

4. Install Apache Tomcat

wget https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.104/bin/apache-tomcat-9.0.104.tar.gz
tar -xvf apache-tomcat-9.0.104.tar.gz
cd apache-tomcat-9.0.104/
cd bin/
./startup.sh 
#Now check apache tomcat server by http://<your-public-ip>:8080

5. πŸš€ Deploy to Apache Tomcat

#Now copy your war file created in <your-project-app>/target to apache-tomcat-9.0.104/webapps folder
cd /home/ec2-user/mvn-app/target
cp mvn-app.war ../../apache-tomcat-9.0.104/webapps

6. Now Restart the apache tomcat

cd /home/ec2-user/apache-tomcat-9.0.104/bin
./shutdown.sh 
./startup.sh 

7. Find your maven application at

#http://<pubic-ip>:8080/mvn-app

πŸŽ₯ Video Tutorial

Watch the full setup on YouTube:
Watch the video


πŸ“š References

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment