{"id":639,"date":"2018-05-30T10:36:09","date_gmt":"2018-05-30T10:36:09","guid":{"rendered":"http:\/\/shijuvarghese.com\/?p=639"},"modified":"2018-05-31T15:00:53","modified_gmt":"2018-05-31T15:00:53","slug":"maven-use-maven-to-package-a-war-file-to-deploy-in-tomcat","status":"publish","type":"post","link":"http:\/\/shijuvarghese.com\/?p=639","title":{"rendered":"Maven: Use Maven to package a &#8220;.war&#8221; file (Ready to deploy in Tomcat)"},"content":{"rendered":"<p>Tomcat is an application server that requires the jave (.jsp) and related files to be packaged in to a &#8220;<strong>.war<\/strong>&#8221; file before uploading then to the server. Once the war file is uploaded, the server unpacks it and does the magic.<\/p>\n<p>A &#8220;<em>.war<\/em>&#8221; fale can be created using Maven, using the command &#8220;<strong>mvn package<\/strong>&#8220;, OR simple using the &#8220;<strong>jar -cvf &lt;file name&gt;.war &lt;list of files or folders to be compressed&gt;<\/strong>&#8220;.<\/p>\n<p>&nbsp;<\/p>\n<p><strong><u>Create a &#8220;.war&#8221; file using Maven<\/u><\/strong><\/p>\n<ul>\n<li>Let us find the version of Maven installed in our host.<\/li>\n<\/ul>\n<p><em><strong>[root@gw16-lap-devops \/]# mvn &#8211;version<\/strong><\/em><br \/>\n<em>Apache Maven 3.5.3 (3383c37e1f9e9b3bc3df5050c29c8aff9f295297; 2018-02-25T01:19:05+05:30)<\/em><br \/>\n<em> Maven home: \/opt\/maven<\/em><br \/>\n<em> Java version: 1.8.0_171, vendor: Oracle Corporation<\/em><br \/>\n<em> Java home: \/usr\/lib\/jvm\/java-1.8.0-openjdk-1.8.0.171-8.b10.el7_5.x86_64\/jre<\/em><br \/>\n<em> Default locale: en_US, platform encoding: UTF-8<\/em><br \/>\n<em> OS name: &#8220;linux&#8221;, version: &#8220;3.10.0-514.el7.x86_64&#8221;, arch: &#8220;amd64&#8221;, family: &#8220;unix&#8221;<\/em><\/p>\n<p>Let us assume that\u00a0we want to create a new project in a new\u00a0folder named\u00a0<strong>MySecondWebApp<\/strong>, inside the <em>root folder &#8220;<strong>\/data2&#8243;<\/strong><\/em>. We want all the source codes to be\u00a0uploaded to tomcat to be grouped inside a group name &#8220;<strong>shijuvarghese.com<\/strong>&#8220;. We can use Maven to layout the necessary folder structure and also create the &#8220;<strong>pom.xml<\/strong>&#8221; file to use to create the &#8220;<strong>.war<\/strong>&#8221; file.<\/p>\n<ul>\n<li><em><strong>[root@gw16-lap-devops \/]# mkdir \/data2<\/strong><\/em><\/li>\n<li><em><strong>[root@gw16-lap-devops \/]#\u00a0cd\u00a0\/data2<\/strong><\/em><\/li>\n<li>Use Maven to generat the archetype suitable to packagethe &#8220;<strong>.war<\/strong>&#8221; file<br \/>\n<em><strong>[root@gw16-lap-devops \/]# mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-webapp -DarchetypeVersion=1.3<\/strong><\/em><\/li>\n<li>When prompted for &#8220;<strong>Define value for property &#8216;groupId&#8217;:<\/strong>&#8221; enter the value &#8220;<strong>shijuvarghese.com<\/strong>&#8220;<\/li>\n<li>When prompted for &#8220;<strong>Define value for property &#8216;artifactId&#8217;:<\/strong>&#8221; enter the value &#8220;<strong>MySecondWebApp<\/strong>&#8220;<\/li>\n<li>When prompted for\u00a0&#8220;<strong>Define value for property &#8216;version&#8217; 1.0-SNAPSHOT: :<\/strong>&#8221; just hit enter<\/li>\n<li>When prompted for\u00a0&#8220;<strong>Define value for property &#8216;package&#8217; shijuvarghese.com: :<\/strong>&#8221; enter &#8220;<strong><em>war<\/em><\/strong>&#8220;<\/li>\n<li>Now enter &#8220;<strong>Y<\/strong>&#8221; to confirm the values entered.<\/li>\n<\/ul>\n<p>Verify the folder Maven has created inside the &#8220;<strong>\/data2<\/strong>&#8221; folder. In the folder &#8220;<strong>\/data2\/MySecondWebApp\/src\/main\/webapp\/<\/strong>&#8221; a simple &#8220;<strong>index.jsp<\/strong>&#8221; page too will be created along with the &#8220;<strong>WEB-INF&#8221; folder. <\/strong>Checkout the &#8220;pom.xml&#8221; file &#8220;<strong>\/data2\/MySecondWebApp\/pom.xml<\/strong>&#8221;<\/p>\n<p>&nbsp;<\/p>\n<ul>\n<li>Let us create another &#8220;.jsp&#8221; file too as an example.<br \/>\n<em><strong>[root@gw16-lap-devops shijuvarghese]#\u00a0vi \/data2\/MySecondWebApp\/src\/main\/webapp\/trial.jsp<\/strong><\/em><br \/>\n<em>&lt;!DOCTYPE html&gt;<\/em><br \/>\n<em> &lt;html&gt;<\/em><br \/>\n<em> &lt;body&gt;<\/em><br \/>\n<em> &lt;%<\/em><br \/>\n<em> for(int i = 0; i &lt; 10; i++) {<\/em><br \/>\n<em> out.println(&#8220;Hello World !!&#8221;);<\/em><br \/>\n<em> }<\/em><br \/>\n<em> %&gt;<\/em><br \/>\n<em> &lt;\/body&gt;<\/em><br \/>\n<em> &lt;\/html&gt;<\/em><\/li>\n<\/ul>\n<ul>\n<li>Now let us package the content of &#8220;<strong>\/data2\/MySecondWebApp\/<\/strong>&#8221; using the &#8220;pom.xml&#8221; file created by Maven.\u00a0Go to the folder &#8220;<strong>\/data2\/MySecondWebApp\/<\/strong>&#8221;<br \/>\n<em><strong>[root@gw16-lap-devops \/]# cd <strong>\/data2\/MySecondWebApp\/<\/strong><\/strong><\/em><\/li>\n<li>Run the following run command to delete any folder files created earlier by Maven using this &#8220;pom.xml&#8221; file , and then package the files to a &#8220;.war&#8221; file inside the folder &#8220;<strong>\/data2\/MySecondWebApp\/target\/<\/strong>&#8220;. Look for a line that reads &#8220;<strong>[INFO] BUILD SUCCESS<\/strong>&#8221; to ensure the build was successful after running the below command.<br \/>\n<em><strong>[root@gw16-lap-devops MySecondWebApp]# mvn clean package<\/strong><\/em><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>Tomcat is an application server that requires the jave (.jsp) and related files to be packaged in to a &#8220;.war&#8221; file before uploading then to <a class=\"mh-excerpt-more\" href=\"http:\/\/shijuvarghese.com\/?p=639\" title=\"Maven: Use Maven to package a &#8220;.war&#8221; file (Ready to deploy in Tomcat)\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":642,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21,3],"tags":[],"class_list":["post-639","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops","category-linux"],"_links":{"self":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/639","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=639"}],"version-history":[{"count":7,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/639\/revisions"}],"predecessor-version":[{"id":650,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/639\/revisions\/650"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/media\/642"}],"wp:attachment":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=639"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=639"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=639"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}