How To Create Maven Project In Eclipse?

In our Previous Post, we mentioned how to install Maven on your machine, and in this post, we will see how to create project maven.

There are two ways of creating project Maven. You can either use the command prompt or do it from within the Eclipse IDE. Let’s take a look at both methods!

Check Prerequisites

It’s important to make sure that your Eclipse is configured correctly before you try to create a Maven project. Make sure you have the following:

  • Apache Maven 3.8.6
  • JDK
  • Eclipse
Post On:Create Project Maven
Post Type:Maven Tutorial
Published On:www.softwaretestingo.com
Applicable For:Freshers & Experience
Get Updates:Join Our Telegram Group

Create a New Project Maven from Command Prompt

To create a Maven project, Go to your windows operating system and open the command prompt.

Command Prompt
Command Prompt

Go to the specific folder where you want to create your project maven and type the below command.

mvn archetype:generate -DgroupId=ToolsQA -DartifactId=DemoMavenProject -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

Command
Command

When you create a new project in Maven, the DartifactId represents the name of your project, while DarchetypeArtifactId specifies what kind of project it is. For example, there are web projects, java projects, and so on. If you want to see the entire list of available types of Maven projects, you can find it here.

After entering the above command press enter to create the Project Maven.

Successfully Installed Maven
Successfully Installed Maven

Note: If your build is failing, make sure to check the version of Maven in your pom.xml file. It needs to match the version you have installed on your machine.

You should now go to the project location in order to see the newly created maven project. Once you are there, open up the pom.xml file which resides within the project folder. By default, POM is generated like this.

Search pom file
Search pom file
<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>SoftwareTestingo</groupId>
  <artifactId>DemoMavenProject</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>DemoMavenProject</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

Maven Folder Structure

And the default folder structure of Maven will be looking something like the below:

Maven Project Structure
Maven Project Structure

Note: If you’re looking to get your test cases recognized by Maven, be sure to put them under src > test > java > SoftwareTestingo. Anything else and Maven will ignore them.

Maven Project to work with Eclipse

Until this, we have created a Project Maven but that’s not compatible with the Eclipse IDE. So to make it compatible we have to run the “mvn eclipse:eclipse“. To run the command go to the project directory in the command prompt and after that, you can run the command.

Making Maven Project compatible For Eclipse
Making Maven Project compatible With Eclipse

Import Maven Project into Eclipse

As the project maven is compatible with Eclipse now we can import this project to Eclipse by following the below steps.

Open the Eclipse application Choose File from the menu and Select the Import option.

Select File Import
Select File Import

Then Select Existing Projects into Workspace option and click next.

Existing Projects into Workspace
Existing Projects in Workspace

Browse the Maven project folder and Click the Finish button.

Browse The Project Maven
Browse The Project Maven

After Click Finish you can see the project will be added to the project explorer section of the eclipse. That will be looking something like the below image:

Project Maven in Project Explorer Section
Project Maven in Project Explorer Section

When modifying the POM, you’ll want to add the compiler plugin. This tells Maven which JDK version to compile your project with and ensures that everything will run smoothly.

If you are using OpenJDK Java then you have to add the below code to your POM.xml file

  <!-- If you Are Using OpenJDK -->
	<plugin>
		<groupId>org.apache.maven.plugins</groupId>
		<artifactId>maven-compiler-plugin</artifactId>
		<version>3.10.1</version>
		<configuration>
			<release>18</release>
		</configuration>
	</plugin>

Or If you are Using Oracle Java then you have to add the below code to your POM.xml file

<!-- If you Are Using Java JDK -->
    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.10.1</version>
	<configuration>
			<source>1.8</source>
			<target>1.8</target>
		</configuration>
    </plugin>

By default your POM.xml file has the old version of the Junit dependency but we will update it to the new version of TestNG dependency like this.

<dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>7.6.1</version>
    <scope>test</scope>
</dependency>

After updating everything if you check your Maven pom.xml file then it will look something like the below:

<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/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>SoftwareTestingo</groupId>
	<artifactId>DemoMavenProject</artifactId>
	<packaging>jar</packaging>
	<version>1.0-SNAPSHOT</version>
	<name>DemoMavenProject</name>
	<url>http://maven.apache.org</url>
	<dependencies>
		<!-- TestNG -->
		<dependency>
			<groupId>org.testng</groupId>
			<artifactId>testng</artifactId>
			<version>7.6.1</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<!-- If you Are Using OpenJDK Java JDK -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.10.1</version>
				<configuration>
					<release>18</release>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-surefire-plugin</artifactId>
				<version>3.0.0-M7</version>
			</plugin>
		</plugins>
	</build>
</project>

First, make the necessary modifications to your POM file. Then, follow the instructions for configuring Maven projects in Eclipse. Open a command prompt and navigate to your Maven project directory. Type “mvn eclipse:eclipse” at the command prompt.

After Update POM File
After Update POM File

Note: If you run the command for the first time then it will download all the related Jars from the central repository.

How to Run your first Maven Test?

To run the Test program, Right-click over the pom.xml file and go run as an option after that select Maven Test.

Run your first Maven Test
Run your first Maven Test

After running the test in the Eclipse console you can see some information below:

[INFO] Results:
[INFO] 
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  5.528 s
[INFO] Finished at: 2022-07-29T22:55:12+05:30
[INFO] ------------------------------------------------------------------------

Eclipse Maven Integration

If you’re using a recent version of Eclipse, Maven is probably already included! So no need to go through the hassle of installing it from the marketplace. However, if you’re using an older version of Eclipse, you’ll need to install Maven explicitly. Once it’s set up though, you’ll be able to enjoy all the features Maven has to offer right from within Eclipse!

Tweaking Eclipse preferences

Before we get started with our exciting project in Eclipse, there are just a few preferences we should tweak to make things run more smoothly. This step isn’t required but it’ll make life easier as we go along!

Navigate to your Maven menu by selecting Eclipse -> Preferences. Once the Preferences dialog pops up, select Maven on the left. This will show you all of the different configuration settings for Maven. The options “Download Artifact Sources” and “Download Artifact JavaDoc” are unchecked by default, but we want to make sure they’re both checked off. After that, click OK

Eclipse Preference
Eclipse Preference

We want this because it helps a lot when using code autocompletion in Eclipse. With the Javadoc available, Eclipse will automatically read it and provide documentation in a tooltip. It’s not required, but very useful.

How to Create a New Maven Project in Eclipse?

If you’re looking to create a Maven project in Eclipse, there are two methods you can follow. Above we have discussed the first method which is By using the command prompt method and the second method entails creating the project directly within Eclipse, rather than first creating it outside of the IDE and then importing it.

To create Project maven in Eclipse follow the below steps:

Open your Eclipse application and Choose New from File Menu then Select Other.

Create Maven Project
Create Maven Project

It will open New Wizard, there you have to select the Maven Project and Click Next.

Select Maven Project
Select Maven Project

Make sure you un-check the ‘Use default Workspace location’ so that you can choose your own workspace for setting up your Maven project! With the help of the Browse button, of course. and Then Click Next.

Use Own Workspace Location
Use Own Workspace Location

Select “maven-archetype-quickstart” from the list of archetypes and then click next.

Select Maven Archetype
Select Maven Archetype

In the New Maven Project window mention the “Group Id & Artifact Id” and Click Finish.

Specify Group ID & Artifact ID
Specify Group ID & Artifact ID

Note: The Artifact ID represents your Project Name.

You should visit the project location to see the newly created maven project. Then, open the pom.xml file in the project folder. The POM is generated like this by default:

<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>SoftwareTestingo</groupId>
  <artifactId>DemoMavenEclipseProject</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>DemoMavenEclipseProject</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>
</project>

And the default folder structure will be looking like the below image:

Note: If you want your test cases to be considered by Maven, make sure they reside under src > test > java > PackageName. Anything else will be ignored.

Modify the pom.xml file with the Latest TestNG and save the file.

<dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>7.6.1</version>
    <scope>test</scope>
</dependency>

How to Run the Maven Project?

To run your Maven test Right click on the pom.xml file and Select Run as then Select Maven Test.

If you run your pom.xml file it will give you an error because there are no compiler plugins available on your pom.xml file. So you have to add the compiler in the pom.xml file like below.

	<build>
		<plugins>
			<!-- If you Are Using Java JDK -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.10.1</version>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-surefire-plugin</artifactId>
				<version>3.0.0-M7</version>
			</plugin>
		</plugins>
	</build>

After updating everything if you check your Maven pom.xml file then it will look something like the below:

<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>SoftwareTestingo</groupId>
	<artifactId>DemoMavenEclipseProject</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>
	<name>DemoMavenEclipseProject</name>
	<url>http://maven.apache.org</url>
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.testng</groupId>
			<artifactId>testng</artifactId>
			<version>7.6.1</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<!-- If you Are Using Java JDK -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.10.1</version>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-surefire-plugin</artifactId>
				<version>3.0.0-M7</version>
			</plugin>
		</plugins>
	</build>
</project>

Also, you have to update the AppTest.java file with the below piece of code because the default AppTest.java file has the Junit codes.

package SoftwareTestingo.DemoMavenEclipseProject;
import org.testng.annotations.Test;
public class AppTest 
{
	@Test
	public void test()
	{
		System.out.println("Hi User");
	}
}

After running the pom.xml file. if everything is configured properly and the build got successful then you will get the result something like the below:

[INFO] Results:
[INFO] 
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  7.218 s
[INFO] Finished at: 2022-07-30T10:38:12+05:30
[INFO] ------------------------------------------------------------------------

Conclusion:

In this blog post, we have learned both the ways of Creating a Project Maven in Command prompt and also Using Eclipse. But after following the steps if you still face any errors or confusion then you can ask us in the comment section. Also, let us know if your organization is following any different methods for creating a maven projects.

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

2 thoughts on “How To Create Maven Project In Eclipse?”

Leave a Comment