Wednesday, June 17, 2015

Create New Project Using Maven In Eclipse

In last tutorial we learnt to configure maven in eclipse . In this tutorial we will learn to create new maven project in eclipse.

To create a new project in maven we need to follow following steps 

1) Open eclipse and follow stated navigation represented in below pic

2) A new pop- up will open , in that pop-up we need to check the provided option as below

3) Again a new pop-up will open, in that pop-up box we need to fill some information related to our project.

After filling required fields click finish button.

4) After clicking finish button you will find a new project being created in the project explorer.

5) You can see a new project has been created with symbol 'M' over the project which shows that it is maven project. 

6) You can see maven has provided the specified folder structure and a POM.xml file by his own. This is why maven is called as "Convention Over Configuration".

7) POM.xml is heart of maven project.





sample project

this project is being created for learning.

8) The content of this pom.xml is being generated based on the information we have filled while creating the new project using maven.

9) By following Convention over configuration maven states that " I am providing specific folder structure to put specific files, like : 
     src/main/java --> to put .java file
     src/main/resources --> to put resource files like properties file in class path
     src/test/java ---> to put . java file for junit test cases.

10)  Now we will be writing simple Java program in src/main/java
package com.dineshonjava;
public class Example1 {
public static void main(String[] args) {
System.out.println("welcome to maven");


11) Now we need to run the application, Before running the application we need to build that and to build that 
  a) right click on the project 
  b) click on the run as 'maven install' 
  c) after clicking that look into console , if every thing will be fine you will find following :
[INFO] --- maven-jar-plugin:2.3.2:jar (default-jar) @ maven_first_example ---
[INFO] Building jar: E:\newsprinws\maven_first_example\target\maven_first_example-0.0.1-SNAPSHOT.jar
[INFO] --- maven-install-plugin:2.3.1:install (default-install) @ maven_first_example ---
[INFO] Installing E:\newsprinws\maven_first_example\target\maven_first_example-0.0.1-SNAPSHOT.jar to C:\Users\lenovo\.m2\repository\com\dineshonjava\example\maven_first_example\0.0.1-SNAPSHOT\maven_first_example-0.0.1-SNAPSHOT.jar
[INFO] Installing E:\newsprinws\maven_first_example\pom.xml to C:\Users\lenovo\.m2\repository\com\dineshonjava\example\maven_first_example\0.0.1-SNAPSHOT\maven_first_example-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.271s
[INFO] Finished at: Tue Jun 16 23:35:35 IST 2015
[INFO] Final Memory: 7M/178M
[INFO] ------------------------------------------------------------------------

12) After running that process , when you will look into project explorer you will find following 

13) Due to above process which ease the building of project , maven is also famous as automated building tool.

14) Now to run this simple application just click on the project and run as a java application and you can find the desired output in console.

More we will learn about the different components of maven in upcoming tutorial .

Keep Learning, Happy Coding