Eclipse + Maven: Creating and exploring projects using archetypes

In this article, we will create project using maven provided archetypes. These archetypes are simply template for creating various kinds of projects in Java/J2EE

Now, it’s left to developers to choose what type of project to be created from the lists of available archetypes

Note: There are around 1212 templates available with Apache Maven 3.2.1

There are two ways to create maven based project

  1. Using Eclipse IDE [provided maven integrated with Eclipse IDE]
  2. Maven command approach

 

Way 1: Using Eclipse IDE

Note: Make sure, Maven is integrated with Eclipse IDE before proceeding with below steps

Step 1: Open Eclipse  –> File Menu –> New –> Others

1_Eclipse-Maven_Creating_Exploring_Project_open_eclipse

 

Step 2: Type “maven” in the wizard –> select “Maven Project” –> Click Next

Note: Provided maven is already integrated with Eclipse IDE, otherwise this option won’t be available

2_Eclipse-Maven_Creating_Exploring_Project_new_wizard

 

Step 3: Either use default workspace location or else browse through different workspace location

If we check the option “Create a simple project (skip archetype selection)” –> by default maven archetype “maven-archetype-quickstart” will be selected –> which is similar to creating simple java project

Note: If we want to create web based project, then uncheck this option to move to next step and search for maven archetype “maven-archetype-webapp

3_Eclipse-Maven_Creating_Exploring_Project_new_wizard_wkspc

 

Step 4: By default, “maven-archetype-quickstart” will be selected

For simple Java application, select this option –> move to next step

4_Eclipse-Maven_Creating_Exploring_Project_new_wizard_wkspc_default

 

Step 5: Enter groupId, artifactId & version and package details in text boxes –> Click “Finish

Group Id: in.bench.resources
Artifact Id: SampleJavWebApp
Version: 0.0.1-SNAPSHOT
Package: com.spring.hibernate.application

Note: This combination needs to be unique among various projects in local maven repository

5_Eclipse-Maven_Creating_Exploring_Project_enter_maven_coordinates

 

Step 6: A new project will be created in Eclipse IDE with below structure

Note: we need to modify pom.xml and add packages/classes as per business requirements

6_Eclipse-Maven_Creating_Exploring_Project_structure

 

Way 2: Maven command approach

Before proceeding, we have to make sure that maven installed in the system
For our example, we are using Windows 7 Operating Systems and Apache Maven 3.2.1

Step 1: open command prompt –> check whether maven installed using “mvn –version” command

With below output, we made sure that maven is installed and working fine

Apache Maven 3.2.1 (ea8b2b07643dbb1b84b6d16e1f08391b666bc1e9; 2014-02-14T23:07:52+05:30)
Maven home: D:\Downloads\Jars\apache-maven-3.2.1\bin\..
Java version: 1.7.0_06-ea, vendor: Oracle Corporation
Java home: C:\Program Files\Java\jdk1.7.0_06\jre
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 7", version: "6.1", arch: "x86", family: "windows"

 

Step 2: execute “mvn archetype:generate” command

There are around 1212 template available with Maven 3.2.1 version
Choose among these, whichever suits our requirement

7_Eclipse-Maven_Creating_Exploring_Project_maven_command_approach

 

Step 3: For Demo purpose, we will create simple java project
So choose 507 and enter below details

8_Eclipse-Maven_Creating_Exploring_Project_maven_command_approach_details

A new project will be created at ‘D:\’ drive location

References

http://maven.apache.org/guides/introduction/introduction-to-archetypes.html
http://maven-repository.com/archetypes

Happy Coding !!
Happy Learning !!

Eclipse + Maven: Converting Web project to Maven project
Eclipse + Maven: M2_REPO is Non Modifiable