{"id":458,"date":"2018-03-11T13:11:33","date_gmt":"2018-03-11T13:11:33","guid":{"rendered":"http:\/\/shijuvarghese.com\/?p=458"},"modified":"2018-05-30T10:42:03","modified_gmt":"2018-05-30T10:42:03","slug":"maven-make-a-build-with-simple-java-program","status":"publish","type":"post","link":"http:\/\/shijuvarghese.com\/?p=458","title":{"rendered":"Maven &#8211; Make a build from a simple Java program."},"content":{"rendered":"<p>Maven is a Build Automation tool, or in a more accurate way, a Project Automation Tool<\/p>\n<p>This is a simple tutorial of how to buld a Java code using Maven. For this we will first create a Java code to display &#8220;Hello my world &#8211; Shiju Varghese!!&#8221;<\/p>\n<p>Prerequisite:<\/p>\n<ul>\n<li>CentOS system with Java and Maven installed.<\/li>\n<\/ul>\n<p>Let us create a folder named &#8220;<strong>\/data\/myProject<\/strong>&#8221; and navigate to the folder by using the command &#8220;<em><strong>cd \/data\/myProject<\/strong><\/em>&#8221;<\/p>\n<p>We have to start this project by crating a &#8220;<strong>pom.xml<\/strong>&#8221; file for the specific project that will assist Maven to build the project. This will inherit other attributes from the super POM. Let us use the posted below code:<\/p>\n<p style=\"text-align: center;\">===========<\/p>\n<p><em>&lt;project&gt;<\/em><br \/>\n<em> &lt;modelVersion&gt;4.0.0&lt;\/modelVersion&gt;<\/em><\/p>\n<p><em>&lt;groupId&gt;com.shijuvarghese&lt;\/groupId&gt;<\/em><br \/>\n<em> &lt;artifactId&gt;helloWorld&lt;\/artifactId&gt;<\/em><br \/>\n<em> &lt;version&gt;1.0&lt;\/version&gt;<\/em><\/p>\n<p><em>&lt;\/project&gt;<\/em><\/p>\n<p style=\"text-align: center;\">===========<\/p>\n<p>Maven expects the java code in a specific folder. Therefor let us create the required folder using the command &#8220;<em><strong>mkdir -p src\/main\/java\/com\/shijuvarghese<\/strong><\/em>&#8221;<br \/>\nLet us go to this folder by using the command &#8220;<em><strong>cd \/data\/myProject\/src\/main\/java\/com\/shijuvarghese<\/strong><\/em>&#8221;<\/p>\n<p>Let us create a java file using an editor, with the file name as hworld.java. The content can be as follows:<\/p>\n<p style=\"text-align: center;\">=========<\/p>\n<p><em>package com.shijuvarghese;<\/em><\/p>\n<p><em>public class hworld {<\/em><br \/>\n<em> public static void main (String[] args){<\/em><br \/>\n<em> System.out.println (&#8220;Hello my world &#8211; Shiju Varghese!!&#8221;);<\/em><br \/>\n<em> }<\/em><br \/>\n<em>}<\/em><\/p>\n<p style=\"text-align: center;\">===========<\/p>\n<p>Now come back to the folder having the pom.xml file and execute the command &#8220;<em><strong>mvn compile<\/strong><\/em>&#8221;<\/p>\n<p>If all goes well without any error, you will see a line towards the end that read &#8220;<strong>[INFO] BUILD SUCCESS<\/strong>&#8221;<\/p>\n<p>To run the java program, go to the classes folder by using the command &#8220;<em><strong>cd \/data\/myProject\/target\/classes<\/strong><\/em>&#8221; and type the command &#8220;<em><strong>java com.shijuvarghese.hworld<\/strong><\/em>&#8220;. The result will be a file that displays:<\/p>\n<ul>\n<li><em>Hello my world &#8211; Shiju Varghese!!<\/em><\/li>\n<\/ul>\n<p style=\"text-align: center;\">======== =====<\/p>\n<p>Additional commands:<\/p>\n<p>Clean and remove the class files and folders that was created by Maven : &#8220;<em><strong>mvn clean<\/strong><\/em>&#8220;<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>Maven is a Build Automation tool, or in a more accurate way, a Project Automation Tool This is a simple tutorial of how to buld <a class=\"mh-excerpt-more\" href=\"http:\/\/shijuvarghese.com\/?p=458\" title=\"Maven &#8211; Make a build from a simple Java program.\">[&#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-458","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\/458","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=458"}],"version-history":[{"count":5,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/458\/revisions"}],"predecessor-version":[{"id":463,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/458\/revisions\/463"}],"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=458"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=458"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=458"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}