Tomcat is an application server that requires the jave (.jsp) and related files to be packaged in to a “.war” file before uploading then to the server. Once the war file is uploaded, the server unpacks it and does the magic.
A “.war” fale can be created using Maven, using the command “mvn package“, OR simple using the “jar -cvf <file name>.war <list of files or folders to be compressed>“.
Create a “.war” file using Maven
- Let us find the version of Maven installed in our host.
[root@gw16-lap-devops /]# mvn –version
Apache Maven 3.5.3 (3383c37e1f9e9b3bc3df5050c29c8aff9f295297; 2018-02-25T01:19:05+05:30)
Maven home: /opt/maven
Java version: 1.8.0_171, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.171-8.b10.el7_5.x86_64/jre
Default locale: en_US, platform encoding: UTF-8
OS name: “linux”, version: “3.10.0-514.el7.x86_64”, arch: “amd64”, family: “unix”
Let us assume that we want to create a new project in a new folder named MySecondWebApp, inside the root folder “/data2″. We want all the source codes to be uploaded to tomcat to be grouped inside a group name “shijuvarghese.com“. We can use Maven to layout the necessary folder structure and also create the “pom.xml” file to use to create the “.war” file.
- [root@gw16-lap-devops /]# mkdir /data2
- [root@gw16-lap-devops /]# cd /data2
- Use Maven to generat the archetype suitable to packagethe “.war” file
[root@gw16-lap-devops /]# mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-webapp -DarchetypeVersion=1.3 - When prompted for “Define value for property ‘groupId’:” enter the value “shijuvarghese.com“
- When prompted for “Define value for property ‘artifactId’:” enter the value “MySecondWebApp“
- When prompted for “Define value for property ‘version’ 1.0-SNAPSHOT: :” just hit enter
- When prompted for “Define value for property ‘package’ shijuvarghese.com: :” enter “war“
- Now enter “Y” to confirm the values entered.
Verify the folder Maven has created inside the “/data2” folder. In the folder “/data2/MySecondWebApp/src/main/webapp/” a simple “index.jsp” page too will be created along with the “WEB-INF” folder. Checkout the “pom.xml” file “/data2/MySecondWebApp/pom.xml”
- Let us create another “.jsp” file too as an example.
[root@gw16-lap-devops shijuvarghese]# vi /data2/MySecondWebApp/src/main/webapp/trial.jsp
<!DOCTYPE html>
<html>
<body>
<%
for(int i = 0; i < 10; i++) {
out.println(“Hello World !!”);
}
%>
</body>
</html>
- Now let us package the content of “/data2/MySecondWebApp/” using the “pom.xml” file created by Maven. Go to the folder “/data2/MySecondWebApp/”
[root@gw16-lap-devops /]# cd /data2/MySecondWebApp/ - Run the following run command to delete any folder files created earlier by Maven using this “pom.xml” file , and then package the files to a “.war” file inside the folder “/data2/MySecondWebApp/target/“. Look for a line that reads “[INFO] BUILD SUCCESS” to ensure the build was successful after running the below command.
[root@gw16-lap-devops MySecondWebApp]# mvn clean package