Maven Example


We can make a basic maven example by executing the archetype:generate direction of mvn tool.

To make a basic java venture utilizing maven , you have to open direction brief and run the archetype:generate order of mvn tool.

Syntax

The syntax to create the venture engineering is given below:

  1. mvn archetype:generate -DgroupId=groupid -DartifactId=artifactid   
  2. -DarchetypeArtifactId=maven-original quickstart -DinteractiveMode=booleanValue  

Example

The example to create the venture engineering is given below:

  1. mvn archetype:generate -DgroupId=com.javacodegeeks -DartifactId=CubeGenerator   
  2. -DarchetypeArtifactId=maven-example quickstart -DinteractiveMode=false  

Note: Here, we are utilizing maven example quickstart to make basic maven center venture. on the off chance that you use maven paradigm webapp, it will create a straightforward maven web application.

Output

Now it will generate following code in the command prompt:

  1. mvn archetype:generate -DgroupId=com.javacodegeeks -DartifactId=Cub  
  2. eGenerator -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=fa  
  3. lse  
  4. [INFO] Scanning for projects...  
  5. [INFO]  
  6. [INFO] ------------------------------------------------------------------------  
  7. [INFO] Building Maven Stub Project (No POM) 1  
  8. [INFO] ------------------------------------------------------------------------  
  9. [INFO]  
  10. [INFO] >>> maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom >>  
  11. >  
  12. [INFO]  
  13. [INFO] <<< maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom <<  
  14. <  
  15. [INFO]  
  16. [INFO] --- maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom --  
  17. -  
  18. [INFO] Generating project in Batch mode  
  19. Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/archetypes/mav  
  20. en-archetype-quickstart/1.0/maven-archetype-quickstart-1.0.jar  
  21. Downloaded: http://repo.maven.apache.org/maven2/org/apache/maven/archetypes/mave  
  22. n-archetype-quickstart/1.0/maven-archetype-quickstart-1.0.jar (5 KB at 3.5 KB/se  
  23. c)  
  24. Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/archetypes/mav  
  25. en-archetype-quickstart/1.0/maven-archetype-quickstart-1.0.pom  
  26. Downloaded: http://repo.maven.apache.org/maven2/org/apache/maven/archetypes/mave  
  27. n-archetype-quickstart/1.0/maven-archetype-quickstart-1.0.pom (703 B at 0.9 KB/s  
  28. ec)  
  29. [INFO] -------------------------------------------------------------------------  
  30. ---  
  31. [INFO] Using following parameters for creating project from Old (1.x) Archetype:  
  32.  maven-archetype-quickstart:1.0  
  33. [INFO] -------------------------------------------------------------------------  
  34. ---  
  35. [INFO] Parameter: groupId, Value: com.javacodegeeks  
  36. [INFO] Parameter: packageName, Value: com.javacodegeeks  
  37. [INFO] Parameter: package, Value: com.javacodegeeks  
  38. [INFO] Parameter: artifactId, Value: CubeGenerator  
  39. [INFO] Parameter: basedir, Value: C:\Users\SSS IT  
  40. [INFO] Parameter: version, Value: 1.0-SNAPSHOT  
  41. [INFO] project created from Old (1.x) Archetype in dir: C:\Users\SSS IT\CubeGene  
  42. rator  
  43. [INFO] ------------------------------------------------------------------------  
  44. [INFO] BUILD SUCCESS  
  45. [INFO] ------------------------------------------------------------------------  
  46. [INFO] Total time: 10.913s  
  47. [INFO] Finished at: Thu Dec 26 16:45:18 IST 2013  
  48. [INFO] Final Memory: 9M/25M  
  49. [INFO] ------------------------------------------------------------------------  
  50. 'cmd' is not recognized as an internal or external command,  
  51. operable program or batch file.  

Generated Directory Structure

Now go to the current directory from where you have executed the mvn command. For example: C:\Users\SSS IT\CubeGenerator. You will see that a simple java project is created that has the following directory:

  1. CubeGenerator  
  2. -src  
  3. --main  
  4. ---java  
  5. ----com  
  6. -----javacodegeeks  
  7. ------App.java  
  8. --test  
  9. ---java  
  10. ----com  
  11. -----javacodegeeks  
  12. ------AppTest.java  
  13. -pom.xml  

As you can see, there are created 3 files pom.xml, App.java and AppTest.java. Let's have a quick look at these files:

1) Automatically Generated pom.xml file

  1. <project xmlns="http://maven.apache.org/POM/4.0.0"   
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0   
  4. http://maven.apache.org/maven-v4_0_0.xsd">  
  5.   
  6.   <example Version>4.0.0</example Version>  
  7.   <groupId>com.javacodegeeks</groupId>  
  8.   <artifactId>CubeGenerator</artifactId>  
  9.   <packaging>jar</packaging>  
  10.   <version>1.0-SNAPSHOT</version>  
  11.   <name>CubeGenerator</name>  
  12.   <url>http://maven.apache.org</url>  
  13.   <dependencies>  
  14.     <dependency>  
  15.       <groupId>junit</groupId>  
  16.       <artifactId>junit</artifactId>  
  17.       <version>3.8.1</version>  
  18.       <scope>test</scope>  
  19.     </dependency>  
  20.   </dependencies>  
  21. </project>  

2) Automatically Generated App.java file

  1. package com.javacodegeeks;  
  2. /** 
  3.  * Hello world! 
  4.  * 
  5.  */  
  6. public class App   
  7. {  
  8.     public static void main( String[] args )  
  9.     {  
  10.         System.out.println( "Hello World!" );  
  11.     }  
  12. }  

3) Automatically Generated AppTest.java file

  1. package com.javacodegeeks;  
  2.   
  3. import junit.framework.Test;  
  4. import junit.framework.TestCase;  
  5. import junit.framework.TestSuite;  
  6. /** 
  7.  * Unit test for simple App. 
  8.  */  
  9. public class AppTest   
  10.     extends TestCase  
  11. {  
  12.     /** 
  13.      * Create the test case 
  14.      * 
  15.      * @param testName name of the test case 
  16.      */  
  17.     public AppTest( String testName )  
  18.     {  
  19.         super( testName );  
  20.     }  
  21.     /** 
  22.      * @return the suite of tests being tested 
  23.      */  
  24.     public static Test suite()  
  25.     {  
  26.         return new TestSuite( AppTest.class );  
  27.     }  
  28.     /** 
  29.      * Rigourous Test :-) 
  30.      */  
  31.     public void testApp()  
  32.     {  
  33.         assertTrue( true );  
  34.     }  
  35. }  

To gather the task, go to the undertaking directory,

for instance: C:\Users\SSS IT\CubeGenerator and compose the accompanying direction on the order prompt:

Now, you will see a ton of execution on the direction brief. On the off chance that you check your undertaking catalog, target directory is made that contains the class files.

Run the Maven Java Project

To run the project, go to the project directory\target\classes,

for example: C:\Users\SSS IT\CubeGenerator\target\classes and write the following command on the command prompt:

  1. java com.javacodegeeks.App  

Now, you will see the output on the command prompt:

Output of the maven example

  1. Hello World!  

How to manufacture the maven venture or how to bundle maven project?

The mvn package order finishes the assemble life cycle of the maven task such as:

  1. validate
  2. compile
  3. test
  4. package
  5. integration-test
  6. verify
  7. install
  8. deploy

Visit this connect to find out about form life cycle http://maven.apache.org/guides/presentation/prologue to-the-lifecycle.html

You need to execute the accompanying direction on the order brief to bundle the maven project:

  1. mvn package  

Now you will see that a container record is created inside the task/target directory.

You can likewise run the maven task by the container record. To do as such, go to the maven task index, for instance: C:\Users\SSS IT\CubeGenerator and execute the accompanying direction on the cmd:

  1. java - classpath target\CubeGenerator-1.0-SNAPSHOT.jar;.; com.javacodegeeks.App  

Now you will see the accompanying output:

  1. Hello World!  





javacodegeeks is optimized for learning.© javacodegeeks .
All Right Reserved and you agree to have read and accepted our term and condition