How to create Maven project in Eclipse IDE

Hi friends, In this tutorial, I will guide you step by step about how to create Maven project in Eclipse IDE. The Eclipse IDE provides nice and excellent support for Maven. If you do not have Maven installed in Eclipse IDE then I advise you to install it first. Before proceeding to the tutorial you should know something about Maven.

How to create Maven project in Eclipse IDE

What is Maven?

Maven is a powerful tool for software project management and comprehension that based on Project Object Model (POM). It is used for project builds, reporting, dependency, releases, distribution, documentation and so many other things from a central place that is called POM. For more information on Maven, you can visit the following link — Maven.

What is Maven Archetype?

Maven Archetype is a Maven project templating toolkit and plugin which is used to create project structure as per the template of it. Maven provides a very large amount of different types of archetypes for different project structure. Some most popular and important archetypes are —

  1. maven-archetype-j2ee-simple – An archetype to generate a simplified sample J2EE application.
  2. maven-archetype-quickstart – An archetype to generate a sample Maven project.
  3. maven-archetype-webapp – An archetype to generate a sample Maven webapp project.
  4. maven-archetype-archetype -An archetype to generate a sample archetype project.

For more information on Maven Archetype please visit the following link — Maven Archetype.

Create Maven project in Eclipse IDE

Now it is the time to see how we will create Maven project in Eclipse IDE with the help of maven-archetype-quickstart. Just follow the below the steps —

Click on File menu -> New -> Maven Project. It will open a New Maven Project window and do not change any settings of it. Just click the Next button.

Create Maven project in Eclipse IDE

 

Next, you will see a New Maven Project window where do not change anything for now just click on Next button.

New maven project window in Eclipse IDE

In the next and below window box choose maven-archetype-quickstart as the default archetype. Write quickstart in the Filter box and select the quickstart archetype from the list and click Next.

Maven archetype selection during new maven project creation

When you are creating a Maven project in Eclipse IDE, you have to keep some basic knowledge in your mind such as,

Group Id: It is a unique identifier which identifies you project among all others, so we need to force a naming schema and it should follow the Java’s package name rule. This means it must start with a reversed domain name you control. For example, com.mtb.maven

Artifact Id: It is the name of jar file without any version. For example, maven_demo

Version: If you distribute it, it should have a version. You can specify any version of your own.

So, these are the basic things you have to remember when you create a Maven project in Eclipse IDE.

After filling up the necessary fields click on the Finish button.

Specifying archetype parameter during new maven project creation in Eclipse IDE

Project structure of a Maven project in Eclipse IDE

After all the above steps a Maven project with Artifact Id “maven_demo” will be created as the below image.

Project structure of a Maven project in Eclipse IDE

As you can see in the above image the default JRE version is 1.5 although the even higher version is installed in your system. By two ways you can assign higher or installed version of JRE in this project.

  1. First, delete default JRE System Library [J2SE-1.5] and then add the library of installed JDK as in your system.
  2. Another way is you can use Maven compiler to configure JRE version.

After any changes in the project you have to update the maven project — maven_demo -> Maven -> Update Project… -> Update Maven Project dialog box will appear. Make sure that maven_demo is checked -> OK button.

Write Java Code

Let’s write some Java code in the App.java file. The following Java code just prints some text and today’s date time in the console.

package com.mtb.maven.maven_demo;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class App 
{
    public static void main( String[] args )
    {
        System.out.println("Mitrajit's Tech Blog -- Welcome to the my first maven project");
        Date date = new Date();
        String strDateFormat = "hh:mm:ss a";
        DateFormat dateFormat = new SimpleDateFormat(strDateFormat);
        String formattedDate= dateFormat.format(date);
        System.out.println("Today's date and time is : " + formattedDate);
    }
}

Compile, build and execute the Maven project

In order to compile, build and execute the Maven project we need another Maven plugin that is exec-maven-plugin. So we need to update the pom.xml file for that plugin.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.mtb.maven</groupId>
  <artifactId>maven_demo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>maven_demo</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  
  

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  
  <build>
     <finalName>Maven Demo</finalName>
     <plugins>
        <plugin>
           <groupId>org.codehaus.mojo</groupId>
           <artifactId>exec-maven-plugin</artifactId>
           <version>1.6.0</version>
           <configuration>
              <mainClass>com.mtb.maven.maven_demo.App</mainClass>
           </configuration>
        </plugin>
     </plugins>
  </build>
</project>

Now, Right-Click on maven_demo -> Run As -> Maven Build…. After that write exec:java in the Goals field and press the Run button.

Edit configuration and launch maven project in Eclipse IDE

When you clicked on the Run button, Maven will start to build the project and after successful build the following output you will get from the console —

After successfull build the Maven project the following output in the console

If you face any problem feel free to inform me in the comment section below. Please like and share this tutorial on How to create Maven project in Eclipse IDE” with others. Kindly subscribe Mitrajit’s Tech Blog to keep yourself updated with the upcoming article.

1 thought on “How to create Maven project in Eclipse IDE

Leave a Reply

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