Categories: Maven

Maven Example Hello World

<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">Here we are creating simple Maven Example Hello World using command prompt by executing the archetype&colon;generate command of mvn tool&period;First of all going to any directory of computer machine and open command prompt&period; Here I am using git bash as command prompt&period; For creating a simple hello java project using maven&comma; we have to open command prompt and run the archetype&colon;generate command of mvn tool&period;<&sol;p>&NewLine;<p>The command as following we have used to create project-<&sol;p>&NewLine;<h2><b>Command-<&sol;b><&sol;h2>&NewLine;<pre class&equals;"highlight">mvn archetype&colon;generate -DgroupId&equals;groupid -DartifactId&equals;artifactid &NewLine;-DarchetypeArtifactId&equals;maven-archetype-quickstart -DinteractiveMode&equals;booleanValue &NewLine;<&sol;pre>&NewLine;<h2><b>Creating Java Hello World Maven Project<&sol;b><&sol;h2>&NewLine;<p>Above is the syntax for creating java project so let see below for actually create command&period;<&sol;p>&NewLine;<pre class&equals;"highlight">mvn archetype&colon;generate -DgroupId&equals;com&period;dineshonjava -DartifactId&equals;JavaHelloWorld -DarchetypeArtifactId&equals;maven-archetype-quickstart -DinteractiveMode&equals;false &NewLine;<&sol;pre>&NewLine;<p>After running this command following is output&colon;<&sol;p>&NewLine;<p>Now it will generate following code in the command prompt&colon;<&sol;p>&NewLine;<pre class&equals;"highlight">&dollar; mvn archetype&colon;generate -DgroupId&equals;com&period;dineshonjava -DartifactId&equals;JavaHelloWorld -DarchetypeArtifactId&equals;maven-archetype-quickstart -DinteractiveMode&equals;false &NewLine;&lbrack;INFO&rsqb; Scanning for projects&period;&period;&period; &NewLine;&lbrack;INFO&rsqb; &NewLine;&lbrack;INFO&rsqb; ------------------------------------------------------------------------ &NewLine;&lbrack;INFO&rsqb; Building Maven Stub Project &lpar;No POM&rpar; 1 &NewLine;&lbrack;INFO&rsqb; ------------------------------------------------------------------------ &NewLine;&lbrack;INFO&rsqb; &NewLine;&lbrack;INFO&rsqb; &gt&semi;&gt&semi;&gt&semi; maven-archetype-plugin&colon;2&period;4&colon;generate &lpar;default-cli&rpar; &gt&semi; generate-sources &commat; standalone-pom &gt&semi;&gt&semi;&gt&semi; &NewLine;&lbrack;INFO&rsqb; &NewLine;&lbrack;INFO&rsqb; &lt&semi;&lt&semi;&lt&semi; maven-archetype-plugin&colon;2&period;4&colon;generate &lpar;default-cli&rpar; &lt&semi; generate-sources &commat; standalone-pom &lt&semi;&lt&semi;&lt&semi; &NewLine;&lbrack;INFO&rsqb; &NewLine;&lbrack;INFO&rsqb; --- maven-archetype-plugin&colon;2&period;4&colon;generate &lpar;default-cli&rpar; &commat; standalone-pom --- &NewLine;&lbrack;INFO&rsqb; Generating project in Batch mode &NewLine;&lbrack;INFO&rsqb; ---------------------------------------------------------------------------- &NewLine;&lbrack;INFO&rsqb; Using following parameters for creating project from Old &lpar;1&period;x&rpar; Archetype&colon; maven-archetype-quickstart&colon;1&period;0 &NewLine;&lbrack;INFO&rsqb; ---------------------------------------------------------------------------- &NewLine;&lbrack;INFO&rsqb; Parameter&colon; groupId&comma; Value&colon; com&period;dineshonjava &NewLine;&lbrack;INFO&rsqb; Parameter&colon; packageName&comma; Value&colon; com&period;dineshonjava &NewLine;&lbrack;INFO&rsqb; Parameter&colon; package&comma; Value&colon; com&period;dineshonjava &NewLine;&lbrack;INFO&rsqb; Parameter&colon; artifactId&comma; Value&colon; JavaHelloWorld &NewLine;&lbrack;INFO&rsqb; Parameter&colon; basedir&comma; Value&colon; D&colon;personal datadojmaven-tutorails &NewLine;&lbrack;INFO&rsqb; Parameter&colon; version&comma; Value&colon; 1&period;0-SNAPSHOT &NewLine;&lbrack;INFO&rsqb; project created from Old &lpar;1&period;x&rpar; Archetype in dir&colon; D&colon;personal datadojmaven-tutorailsJavaHelloWorld &NewLine;&lbrack;INFO&rsqb; ------------------------------------------------------------------------ &NewLine;&lbrack;INFO&rsqb; BUILD SUCCESS &NewLine;&lbrack;INFO&rsqb; ------------------------------------------------------------------------ &NewLine;&lbrack;INFO&rsqb; Total time&colon; 01&colon;03 min &NewLine;&lbrack;INFO&rsqb; Finished at&colon; 2016-10-24T23&colon;58&colon;00&plus;05&colon;30 &NewLine;&lbrack;INFO&rsqb; Final Memory&colon; 9M&sol;44M &NewLine;&lbrack;INFO&rsqb; ------------------------------------------------------------------------ &NewLine; &NewLine;<&sol;pre>&NewLine;<div id&equals;"ads-id" align&equals;"center"><&sol;div>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2016&sol;10&sol;Maven-Example-Hello-World&period;jpg" width&equals;"740" height&equals;"253" border&equals;"0" &sol;><&sol;div>&NewLine;<h2><b>Created Project Structure-<&sol;b><&sol;h2>&NewLine;<p>Now go to directory where we have created java project&period; We got following directory structure&period;<&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2016&sol;10&sol;Maven-dirctory-structure&period;jpg" border&equals;"0" &sol;><&sol;div>&NewLine;<h3><b>Created files&colon;<&sol;b><&sol;h3>&NewLine;<p>Following files are auto generated into java project&period;<br &sol;>&NewLine;<b><&sol;b><&sol;p>&NewLine;<h3><b>Maven pom&period;xml file<&sol;b><&sol;h3>&NewLine;<pre class&equals;"highlight">&lt&semi;project xmlns&equals;"http&colon;&sol;&sol;maven&period;apache&period;org&sol;POM&sol;4&period;0&period;0" xmlns&colon;xsi&equals;"http&colon;&sol;&sol;www&period;w3&period;org&sol;2001&sol;XMLSchema-instance" &NewLine; xsi&colon;schemaLocation&equals;"http&colon;&sol;&sol;maven&period;apache&period;org&sol;POM&sol;4&period;0&period;0 http&colon;&sol;&sol;maven&period;apache&period;org&sol;maven-v4&lowbar;0&lowbar;0&period;xsd"&gt&semi; &NewLine; &lt&semi;modelVersion&gt&semi;4&period;0&period;0&lt&semi;&sol;modelVersion&gt&semi; &NewLine; &lt&semi;groupId&gt&semi;com&period;dineshonjava&lt&semi;&sol;groupId&gt&semi; &NewLine; &lt&semi;artifactId&gt&semi;JavaHelloWorld&lt&semi;&sol;artifactId&gt&semi; &NewLine; &lt&semi;packaging&gt&semi;jar&lt&semi;&sol;packaging&gt&semi; &NewLine; &lt&semi;version&gt&semi;1&period;0-SNAPSHOT&lt&semi;&sol;version&gt&semi; &NewLine; &lt&semi;name&gt&semi;JavaHelloWorld&lt&semi;&sol;name&gt&semi; &NewLine; &lt&semi;url&gt&semi;http&colon;&sol;&sol;maven&period;apache&period;org&lt&semi;&sol;url&gt&semi; &NewLine; &lt&semi;dependencies&gt&semi; &NewLine; &lt&semi;dependency&gt&semi; &NewLine; &lt&semi;groupId&gt&semi;junit&lt&semi;&sol;groupId&gt&semi; &NewLine; &lt&semi;artifactId&gt&semi;junit&lt&semi;&sol;artifactId&gt&semi; &NewLine; &lt&semi;version&gt&semi;3&period;8&period;1&lt&semi;&sol;version&gt&semi; &NewLine; &lt&semi;scope&gt&semi;test&lt&semi;&sol;scope&gt&semi; &NewLine; &lt&semi;&sol;dependency&gt&semi; &NewLine; &lt&semi;&sol;dependencies&gt&semi; &NewLine;&lt&semi;&sol;project&gt&semi; &NewLine; &NewLine;<&sol;pre>&NewLine;<p><b>App&period;java<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight">package com&period;dineshonjava&semi; &NewLine; &NewLine;&sol;&ast;&ast; &NewLine; &ast; Hello world&excl; &NewLine; &ast; &NewLine; &ast;&sol; &NewLine;public class App &NewLine;&lbrace; &NewLine; public static void main&lpar; String&lbrack;&rsqb; args &rpar; &NewLine; &lbrace; &NewLine; System&period;out&period;println&lpar; "Hello World&excl;" &rpar;&semi; &NewLine; &rcub; &NewLine;&rcub; &NewLine; &NewLine;<&sol;pre>&NewLine;<p><b>AppTest&period;java<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight">package com&period;dineshonjava&semi; &NewLine; &NewLine;import junit&period;framework&period;Test&semi; &NewLine;import junit&period;framework&period;TestCase&semi; &NewLine;import junit&period;framework&period;TestSuite&semi; &NewLine; &NewLine;&sol;&ast;&ast; &NewLine; &ast; Unit test for simple App&period; &NewLine; &ast;&sol; &NewLine;public class AppTest &NewLine; extends TestCase &NewLine;&lbrace; &NewLine; &sol;&ast;&ast; &NewLine; &ast; Create the test case &NewLine; &ast; &NewLine; &ast; &commat;param testName name of the test case &NewLine; &ast;&sol; &NewLine; public AppTest&lpar; String testName &rpar; &NewLine; &lbrace; &NewLine; super&lpar; testName &rpar;&semi; &NewLine; &rcub; &NewLine; &NewLine; &sol;&ast;&ast; &NewLine; &ast; &commat;return the suite of tests being tested &NewLine; &ast;&sol; &NewLine; public static Test suite&lpar;&rpar; &NewLine; &lbrace; &NewLine; return new TestSuite&lpar; AppTest&period;class &rpar;&semi; &NewLine; &rcub; &NewLine; &NewLine; &sol;&ast;&ast; &NewLine; &ast; Rigourous Test &colon;-&rpar; &NewLine; &ast;&sol; &NewLine; public void testApp&lpar;&rpar; &NewLine; &lbrace; &NewLine; assertTrue&lpar; true &rpar;&semi; &NewLine; &rcub; &NewLine;&rcub; &NewLine; &NewLine;<&sol;pre>&NewLine;<h3><b>Run the created java project<&sol;b><&sol;h3>&NewLine;<p>For running created java project we have to compile first then we can run it&period;<&sol;p>&NewLine;<p>For compile use this command &&num;8220&semi;<b>mvn clean compile<&sol;b>&&num;8221&semi;<&sol;p>&NewLine;<pre class&equals;"highlight">&dollar; mvn clean compile &NewLine;&lbrack;INFO&rsqb; Scanning for projects&period;&period;&period; &NewLine;&lbrack;INFO&rsqb; &NewLine;&lbrack;INFO&rsqb; ------------------------------------------------------------------------ &NewLine;&lbrack;INFO&rsqb; Building JavaHelloWorld 1&period;0-SNAPSHOT &NewLine;&lbrack;INFO&rsqb; ------------------------------------------------------------------------ &NewLine;&lbrack;INFO&rsqb; &NewLine;&lbrack;INFO&rsqb; --- maven-clean-plugin&colon;2&period;5&colon;clean &lpar;default-clean&rpar; &commat; JavaHelloWorld --- &NewLine;&lbrack;INFO&rsqb; &NewLine;&lbrack;INFO&rsqb; --- maven-resources-plugin&colon;2&period;6&colon;resources &lpar;default-resources&rpar; &commat; JavaHelloWorld --- &NewLine;&lbrack;WARNING&rsqb; Using platform encoding &lpar;Cp1252 actually&rpar; to copy filtered resources&comma; i&period;e&period; build is platform dependent&excl; &NewLine;&lbrack;INFO&rsqb; skip non existing resourceDirectory D&colon;personal datadojmaven-tutorailsJavaHelloWorldsrcmainresources &NewLine;&lbrack;INFO&rsqb; &NewLine;&lbrack;INFO&rsqb; --- maven-compiler-plugin&colon;3&period;1&colon;compile &lpar;default-compile&rpar; &commat; JavaHelloWorld --- &NewLine;&lbrack;INFO&rsqb; Changes detected - recompiling the module&excl; &NewLine;&lbrack;WARNING&rsqb; File encoding has not been set&comma; using platform encoding Cp1252&comma; i&period;e&period; build is platform dependent&excl; &NewLine;&lbrack;INFO&rsqb; Compiling 1 source file to D&colon;personal datadojmaven-tutorailsJavaHelloWorldtargetclasses &NewLine;&lbrack;INFO&rsqb; ------------------------------------------------------------------------ &NewLine;&lbrack;INFO&rsqb; BUILD SUCCESS &NewLine;&lbrack;INFO&rsqb; ------------------------------------------------------------------------ &NewLine;&lbrack;INFO&rsqb; Total time&colon; 3&period;829 s &NewLine;&lbrack;INFO&rsqb; Finished at&colon; 2016-10-25T00&colon;19&colon;50&plus;05&colon;30 &NewLine;&lbrack;INFO&rsqb; Final Memory&colon; 9M&sol;22M &NewLine;&lbrack;INFO&rsqb; ------------------------------------------------------------------------ &NewLine; &NewLine;<&sol;pre>&NewLine;<p>For run this project go to the project <b>directorytargetclasses<&sol;b>&comma;<&sol;p>&NewLine;<p>And run following command for executing java class&period;<&sol;p>&NewLine;<p><i>java com&period;dineshonjava&period;App<&sol;i><&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<&sol;div>&NewLine;<div class&equals;"wp-post-navigation"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-pre"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;maven-settings-file&sol;">Previous<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-next"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;maven-project-dependencies&sol;">Next<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;<&sol;div>&NewLine;<script type&equals;"text&sol;javascript">&NewLine;jQuery&lpar;document&rpar;&period;ready&lpar;function&lpar;&dollar;&rpar; &lbrace;&NewLine; &dollar;&period;post&lpar;'https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-admin&sol;admin-ajax&period;php'&comma; &lbrace;action&colon; 'mts&lowbar;view&lowbar;count'&comma; id&colon; '103'&rcub;&rpar;&semi;&NewLine;&rcub;&rpar;&semi;&NewLine;<&sol;script>

Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design PatternsĀ using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

4 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

4 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

4 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

4 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

4 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

4 years ago