Previous – Chapter 7

Maven Tutorial – Chapter 8

Project Creation in Maven

In the previous project, we have seen how a maven project is architecture and the different configurations involved for a project. Now, we will understand how a sample java project can be created using maven. Also, will see how the project is structured when created using maven.

Archetype
Maven uses archetype to create any project. Archetype is nothing but the tool provided by the maven to create a project. It is also defined as “an organizational pattern from which all other things of the similar kind are made”. By using archetypes provides an easy and instrumental way to create projects to the developers, where the best practices are employed as per the standards defined at the organization level. Also, archetypes enables the developers to quickly create a project and start running the same within a matter of commands.

In order to create a new simple maven java project just use the maven archetype plugin in the command line. Currently there are more than 850+ maven archetypes listed out in order to create projects under different frameworks like struts, spring, hibernate, web services (RESTful/SOAP) and many others.

So, let us create one simple project in maven. Open the command prompt and navigate to the workspace folder in your local machine (E.g. D:\Java\workspace). Then type the below maven command and press enter.

mvn archetype:generate -DgroupId=com.java.samples -DartifactId=JavaSamples   -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

pic1

 

 

 

 

 

 

 

 

pic2

 

 

 

 

 

 

 

 

 

 

 

 

 

The above command instructs the maven to create a project with maven-archetype-quickstart template.

If you navigate to the folder D:\Java\workspace, a new project named “JavaSamples” will be created including all the base structure of a java project as well as shown below:

pic3

 

 

 

 

 

 

Folders created Description
JavaSamples Root folder of the project containing source and pom.xml
src/main/java Contains all the java code under the package com.java.samples
src/main/test Contains all the java test code under the package com.java.samples
src/main/resources Contains all the resources like xml/properties file

Under the folder D:\Java\workspace\JavaSamples\src\main\java\com\java\samples a sample java file called App.java will be created by default which will be having one print statement.

[java]
package com.java.samples;

/**
 * Hello world!
 *
 */

public class App {
   public static void main (String[] args)   {
       System.<em>out</em>.println("Hello World!");
   }
}
[/java]

Under the folder D:\Java\workspace\JavaSamples\src\test\java\com\java\samples a sample java test class (JUnit) will be created AppTest.java

[java]
package com.java.samples;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Unit test for simple App.
*/
public class AppTest extends TestCase{  
public AppTest( String testName ){
super( testName )
}

public static Test suite(){
return new TestSuite( AppTest.class );
}

public void testApp(){
assertTrue( true );
}
}
[/java]

Developers are required to add their files as described in the above mentioned table. They are very much free to add any new packages as well as required during the project development. The above said folder structure are just created as per the archetype plugin defined.

There are 2 major versions made available for any maven project:

Next – Chapter 9

Leave a Reply

Your email address will not be published. Required fields are marked *