Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Last Updated: Mar 27, 2024
Difficulty: Easy

Maven Eclipse Plugin

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

Maven is a CLI (Command Line Interface) tool that helps us manage software projects. It is generally popular for Java and JVM-language based (such as Scala, and Kotlin) projects. It allows us to automate parts of software development such as compiling, packaging, testing,etc. We can run all of these using simple commands from the terminal.

Maven Eclipse  Plugin

Eclipse is an IDE used for software development. It's mainly used for Java Development. It's a powerful IDE providing all the tools necessary to write great code. We can use Maven with Eclipse with the help of the Maven Eclipse Plugin and the  M2E (Maven To Eclipse) integration.

Let's take a look at them.

Maven Eclipse Plugin

As we know, most of the work in Maven is done by plugins. Maven is, at its core, a plugin execution framework. It has a plugin for Eclipse that allows us to use Maven with Eclipse. This plugin uses Maven to generate Eclipse IDE files, such as .classpath.project.settings, etc.

Goals

The standard goals of the Maven Eclipse Plugin include:

  • eclipse:configure-workspace -> This lets Eclipse know where you have installed Maven in your system. It does this by adding a variable M2_REPO to Eclipse. We can also do this manually through Eclipse settings, instead of using the command. 
     
  • eclipse:eclipse -> This command generates the configuration files, such as .classpath,.project, etc. 
     
  • eclipse:resolve-workspace-dependencies ->This downloads missing dependencies for all projects in a workspace. This is useful if the project is being worked on in a group and other users are also adding dependencies. We will need to resolve new dependencies after an update.
     
  • eclipse:clean-> This deletes the files used by the Eclipse IDE.
     

This plugin has now been officially retired. It is no longer updated or maintained and is not recommended anymore. Maven recommends using the M2E, or Maven To Eclipse, Integration.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

M2E

M2E stands for Maven to Eclipse. It is an integration that is part of the Eclipse IDE. It automatically manages many Maven procedures, such as installing dependencies, running builds, etc. 

Installing

Most versions of Eclipse already come with M2E installed. We can check this by going to 

Help->About section. If the M2E logo is present, it means Maven is already installed. 

Help Menu Bar
M2E logo present

 

If not, we can install it ourselves. 

1. Go to Help->Install New Software

Help->Install New Software

2. In the Work With section, you can specify a download link for a particular version. To use the latest version, choose the link shown below.

Enter Work With field

3. Type Maven in the search box. You should see M2E listed. 

Search for Maven

4. Click on the checkbox next to it and click on next. 

Choose M2E and Click on Next

Confirm the download on the next screen, and accept the license agreement. Click on Finish. Eclipse will install the integration. You may have to restart Eclipse in order for it to be usable. 

Create a New Maven Project

We can use Eclipse to create a project that already uses Maven. Eclipse will also set up the necessary pom.xml file for us.

1. Go to File->New->Other

 File->New->Other

2. Type Maven in the search box and choose Maven Project

Search for Maven -> Maven Project

3. Tick the checkbox for a simple project, and specify the workspace if necessary.

Create simple project in desired directory

4. Type the Group ID and Artifact ID. 

Group ID is generally the reverse of your website/domain name. For example, for the domain codingninjas.com, the group ID would be com.codingninjas.

ArtifactID is the unique name of your project. No other project should exist with this name in your organization.

Specify Group ID and Artifact ID

5. Click on Finish.

A pom.xml file has been created

Eclipse will create the project, and fill in the values you entered in the pom.xml file. 

Configure Eclipse to automatically update Maven dependencies

We can set Eclipse to automatically download and update dependencies, whenever we make changes to our pom.xml file. 

Go to Window->Preferences->Maven.

 Window->Preferences->Maven.

Tick the box to Automatically update Maven projects configuration. 

Let's try this out. We will add a new dependency to our pom.xml file and see if anything changes. 

Project Structure - before
pom.xml - before

This is our project folder and pom.xml before adding the dependency. 

Let us add a dependency for MySQL. 

pom.xml after adding MySQL dependency

Let us save it and see the changes to our project structure.

Project Structure - after

Simply after saving the pom.xml file, we can see a new folder Maven Dependencies has been added.

MySQL driver has been installed

On expanding, we can see it has downloaded and installed the dependency.

Importing Existing Project

If you already have an existing project that uses Maven, we can easily import it to Eclipse. 

1. Go to File->Import.

File->Import

2. Type Maven in the search box and choose Existing Maven Project. 

Existing Maven Project

3. Type the path to your project, or browse it using your system file explorer.

Choose desired project

Eclipse will automatically detect pom.xml files and .jar executable outputs from your project Folder. Check the box to Add project(s) to working set. Click on Finish.

4. Eclipse has imported our project along with its dependencies. 

Project and its dependencies have been imported

We can also go into the project folder and see.

Eclipse has created its config files in the project

Eclipse has added its configuration files - the .settings folder and the .classpath and .project files. 

Running Maven Builds

Let us try running a Maven build using Eclipse - without typing any Maven commands. 

1. Right-click on the pom.xml file. Choose Run As->Maven Build. 

Run As->Maven Build.

2. Type clean verify in the Goals section. 

Enter goal as clean verify

The verify command makes sure everything is correct with our Maven project. It runs all the integration tests that Maven finds in our project. 

If we run the same Maven command multiple times without changing any code, sometimes Maven will simply say "Nothing to compile, all classes are up to date". To prevent this, we add the clean command before the verify command. This results in Maven deleting the target directory before running any commands, ensuring that all classes are recompiled.

Click on Run.

Maven build runs successfully

Our Maven build has run successfully!

Frequently Asked Questions

Can I configure Eclipse to update dependencies from remote repositories?

Yes, you can. We can go to Window->Preferences->Maven, and uncheck the box for Do not automatically update dependencies from remote repositories. But it is not recommended, as this might install breaking changes in your plugins. Your code might be incompatible with the changes in newer versions.

Is IntelliJ better or Eclipse?

Both are powerful and popular IDEs for development. IntelliJ is better in ease of use,refactoring and autocompletion. Eclipse is better in terms of plugin support, indexing large projects. Eclipse supports other languages like C++,Perl, etc. IntelliJ is mainly for Java and other JVM languages like Scala and Kotlin.

Can the Maven Eclipse Plugin still be used?

Yes, it can still be used. However, it is retired and not being maintained anymore. This means it will not receive updates for newer versions of Maven and Eclipse. Bugs and security issues will also not be fixed. 

Conclusion

This blog has explored the Maven Eclipse Plugin, as well as the M2E integration in Eclipse. We have seen how to install M2E, how to create new Maven projects in Eclipse, and how to import existing projects. We have also seen how to set up auto-updating of Maven dependencies in Eclipse, and how to run Maven builds using Eclipse. 

We hope you leave this article with a broader knowledge of Maven, Eclipse, and Software project management. We recommend that you explore our different articles on these topics as well, such as:

What do you mean by Maven

Introduction to Maven Command

Ant vs Maven

You can practice questions on various problems on Coding Ninjas Studio, attempt mock tests. You can also go through interview experiences, interview bundle, go along guided paths for preparations, and a lot more!

 

Keep coding, keep reading Ninjas. 

 

Topics covered
1.
Introduction
2.
Maven Eclipse Plugin
2.1.
Goals
3.
M2E
3.1.
Installing
3.2.
Create a New Maven Project
3.3.
Configure Eclipse to automatically update Maven dependencies
3.4.
Importing Existing Project
3.5.
Running Maven Builds
4.
Frequently Asked Questions
4.1.
Can I configure Eclipse to update dependencies from remote repositories?
4.2.
Is IntelliJ better or Eclipse?
4.3.
Can the Maven Eclipse Plugin still be used?
5.
Conclusion