π οΈ How to build the java web application using Maven, and how to deploy it on an Apache Tomcat server
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.
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.
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 --versionwget 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 -v3. 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 packagewget 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#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/webappscd /home/ec2-user/apache-tomcat-9.0.104/bin
./shutdown.sh
./startup.sh #http://<pubic-ip>:8080/mvn-appWatch the full setup on YouTube: