Maven project in Eclipse

In this blog we would be discussing about one of the ways of creating a Maven project in Eclipse.

Tools Used

The Eclipse I am using in this article is – Eclipse IDE for Enterprise Java and Web Developers (includes Incubating components) Version: 2021-03 (4.19.0) Build id: 20210312-0638.

MAVEN version used 3.5.1

Whenever you open the Eclipse workspace for first time, or when there is no project in your workspace, you will see the shortcut to create multiple types or project (although it may vary depending on the type of eclipse you have).

Create Maven Project Shortcut

Else, you can right click on Project Explorer and create a new Maven Project, provided you already have Maven plugin installed in your eclipse.

Step wise guide:

A. Project Setup

1: Create the new Maven Project in eclipse (make sure you have checked the first check box to create simple project)

2: Enter the artifact details to create the project. However, these can be modified later in your pom.xml file as well

Project Details

3: Project is created with default JRE System Library which we will change in next step 4, as you can see in below image it is J2SE-1.5

Maven Project Created

4: Modify the JRE to the one installed in your system. To do so, go to right click on project, go to Build Path and configure-

Configure Build Path

Make the changes under libraries, select the Alternate JRE or Workspace default JRE. Here we will go with third option.

Configure JRE

B. Code modifications

5: After this, open POM.xml to add some dependencies (similar to adding jar files in build path). You can copy and paste below pom.xml.

Here we have added few sample properties, dependencies and build plugin details

6: Add new class “MyFirstMaven.java” in src/main/java package

MyFirstMaven class

7: In case there are some errors in the project, try to Update the project. Right Click on project -> Maven -> Update Project -> Check Force Update -> OK.

Maven Update project

8: Let us add some good example in our Java Class. Here we are trying to create an Excel file with site data using Apache POI library. The pom dependency of apache POI has already been added in our pom.xml.

C. Build and Run

9: Now the project is ready to be build. To create the Run Configuration with goals as “clean install

Run Configuration to build the maven project

Once you click on Run, if everything goes fine, build would be success.

MAVEN PROJECT BUILD SUCCESS

9: Run the project as Java Application. It will generate the Excel file along side to your project folder.

Output

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.