{"id":476,"date":"2018-03-13T03:19:05","date_gmt":"2018-03-13T03:19:05","guid":{"rendered":"http:\/\/shijuvarghese.com\/?p=476"},"modified":"2018-05-27T15:01:04","modified_gmt":"2018-05-27T15:01:04","slug":"build-and-package-a-java-application-using-jenkins-git-and-maven","status":"publish","type":"post","link":"http:\/\/shijuvarghese.com\/?p=476","title":{"rendered":"Build and package a Java application using Jenkins, Git and Maven"},"content":{"rendered":"<p>In this document we will be going through an outline of compiling and packaging a Java application using Jenkins. Here Jenkins downloads all files (few java codes) from GitHub repository and uses Maven to compile and pack the code in to a &#8220;<strong>.jar<\/strong>&#8221; file.<\/p>\n<p><b>Prerequisite<\/b><strong>:<\/strong><\/p>\n<ul>\n<li>CentOS host with 10GB free space and 2GB RAM<\/li>\n<li><a href=\"http:\/\/shijuvarghese.com\/?p=473\">Install Git<\/a>\u00a0in your local host and test if you can download contents of a public repository<\/li>\n<li><a href=\"http:\/\/shijuvarghese.com\/?p=455\">Install Maven<\/a>\u00a0(<em>apache-maven-3.5.3<\/em>), and see if you can build a sample Java program<\/li>\n<li><a href=\"http:\/\/shijuvarghese.com\/?p=464\">Install Jenkin<\/a>s\u00a0(<em>ver. 2.111<\/em>). Ensure the plugins to integrate with Git and Maven is also installed along with other default plugins. This includes\u00a0the following plugins which can be installed by going to the Jenkins dashboard, then clicking on &#8220;<strong>Manage Jenkins<\/strong>&#8221; link on left, and selecting &#8220;<strong>Manage Plugin<\/strong>&#8221; link:\n<ul>\n<li>GitHub plugin<\/li>\n<li>Git plugin<\/li>\n<li>Git client plugin<\/li>\n<li>Maven Integration<\/li>\n<\/ul>\n<\/li>\n<li>Create a folder named &#8220;<strong>workspace<\/strong>&#8221; in the host via <em>ssh<\/em> terminal, inside the folder &#8220;<strong>\/var\/lib\/jenkins\/<\/strong>&#8220;, and create a file names &#8220;<strong>pom.xml<\/strong>&#8221; with the following content:<\/li>\n<li><span style=\"color: red;\">&lt;project&gt;<br \/>\n&lt;modelVersion&gt;4.0.0&lt;\/modelVersion&gt;<br \/>\n&lt;groupId&gt;com.shijuvarghese&lt;\/groupId&gt;<br \/>\n&lt;artifactId&gt;helloWorld&lt;\/artifactId&gt;<br \/>\n&lt;version&gt;1.0&lt;\/version&gt;<br \/>\n&lt;\/project&gt;<\/span><\/li>\n<li>run the command &#8220;<strong>chown -R jenkins.jenkins \/var\/lib\/jenkins\/workspace<\/strong>&#8220;<\/li>\n<\/ul>\n<p><strong><u>Procedure:<\/u><\/strong><\/p>\n<ul>\n<li>Access the Jenkins <em>dashboard<\/em> by accessing the URL &#8220;<strong>http:\/\/&lt;host IP&gt;:8080<\/strong>&#8220;<\/li>\n<li>First let us setup settings to connect to Maven.\n<ul>\n<li>Go to &#8220;<strong>Manage Jenkins<\/strong>&#8220;, then select &#8220;<strong>Global Configuration Tool<\/strong>&#8220;. Under the heading <strong>Maven<\/strong>, clink the &#8220;<strong>Add Maven<\/strong>&#8221; beside &#8220;<strong>Maven installations<\/strong>&#8220;.<br \/>\nUn-check &#8220;<strong>Install automatically<\/strong>&#8220;<\/li>\n<li>For the <strong>Name<\/strong> text-field, enter &#8220;<em>Maven 3.5.3<\/em>&#8221; if this is the version of Maven installed as per the pre-requisite section in this tutorial.<\/li>\n<li>Find the &#8220;MAVEN_HOME&#8221; variable configured in the Maven installation in the host by accessing the host via <em><strong>ssh<\/strong><\/em> and executing the command &#8220;<em><strong>echo $MAVEN_HOME<\/strong><\/em>&#8220;. Copy and enter the value in the &#8220;<strong>MAVEN_HOME<\/strong>&#8221; text-field in Jenkins.<\/li>\n<li>Click on &#8220;<strong>Apply<\/strong>&#8221; and then &#8220;<strong>Save<\/strong>&#8220;.<\/li>\n<\/ul>\n<\/li>\n<li>Go back to\u00a0Jenkins\u00a0<em>dashboard<\/em>.<\/li>\n<li>Click on <strong>New Job<\/strong> or <strong>New Item<\/strong> link.<\/li>\n<li>Enter the Item Name. For Example &#8220;<strong>hworld_packaging<\/strong>&#8220;<\/li>\n<li>Select &#8220;<strong>Maven\u00a0Project<\/strong>&#8221; and click <strong>OK<\/strong>.<\/li>\n<li>Add a <strong>Description<\/strong>. For example &#8220;<em>Sample Java application that will be build and packaged<\/em>&#8220;.<\/li>\n<li>If needed, select &#8220;<strong>Discard old builds<\/strong>&#8221; checkbox, inter the value &#8220;<em><strong>2<\/strong><\/em>&#8221; in the text-field &#8220;Max no of builds to keep&#8221;.<\/li>\n<li>Select &#8220;<em><strong>Git<\/strong><\/em>&#8221; under the section &#8220;<strong>Source Code Management<\/strong>&#8220;, and add the &#8220;Repository URL&#8221;. If the ULR is not valid, a message will be displayed. If is advisable to use a publicly available GitHub repository. Steps to find this URL is mentioned in one of my previous documents about Git.<\/li>\n<li>Under &#8220;<strong>Build Triggers<\/strong>&#8221; select the checkbox &#8220;<strong>Poll SCM<\/strong>&#8221; and enter the value &#8220;<em><strong>H * * * *<\/strong><\/em>&#8221; in the text-field labeled <strong>Schedule<\/strong>.<\/li>\n<li>Under\u00a0the &#8220;<strong>Build<\/strong>&#8221; section, in the &#8220;<strong>Root POM<\/strong>&#8221; text-field enter the value &#8220;<em>\/var\/lib\/jenkins\/workspace\/pom.xml<\/em>&#8220;<\/li>\n<li>In the &#8220;<strong>Goals and options<\/strong>&#8221; text-field enter the value &#8220;<em><strong>clean package<\/strong><\/em>&#8220;<\/li>\n<li>Click on\u00a0<strong>Apply<\/strong> and\u00a0<strong>Save<\/strong>.<\/li>\n<\/ul>\n<p>You will be in the specific project&#8217;s page now. Else go to the\u00a0<strong>Dashboard<\/strong>, where you will find the link to go to the newly created project.<\/p>\n<p><strong>Running the project:<\/strong><\/p>\n<ul>\n<li>Go to the newly created project&#8217;s page and click on the link &#8220;<strong>Build now<\/strong>&#8221; link on the left.<\/li>\n<li>In the &#8220;<strong>Build History<\/strong>&#8221; section, click on the newly created Build number.<\/li>\n<li>In the new page, select the &#8220;<strong>Console Output<\/strong>&#8221; link, where you will see the results. Make sure the last section of the output says &#8220;<em>Finished: SUCCESS<\/em>&#8220;.<\/li>\n<li>You may confirm is the accessing the server and looking for &#8220;\/var\/lib\/jenkins\/workspace\/hworld_packaging\/target\/helloWorld-1.0.jar&#8221; as mentioned in the &#8220;<strong>Console Output<\/strong>&#8221; in the Jenkins page.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>In this document we will be going through an outline of compiling and packaging a Java application using Jenkins. Here Jenkins downloads all files (few <a class=\"mh-excerpt-more\" href=\"http:\/\/shijuvarghese.com\/?p=476\" title=\"Build and package a Java application using Jenkins, Git and Maven\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":465,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21,3],"tags":[],"class_list":["post-476","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\/476","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=476"}],"version-history":[{"count":31,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/476\/revisions"}],"predecessor-version":[{"id":634,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/476\/revisions\/634"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/media\/465"}],"wp:attachment":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=476"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=476"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=476"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}