Do you think IIT Guwahati certified course can help you in your career?
No
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.
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.
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.
If not, we can install it ourselves.
1. Go to 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.
3. Type Maven in the search box. You should see M2E listed.
4. Click on the checkbox next to it 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
2. Type Maven in the search box and choose Maven Project.
3. Tick the checkbox for a simple project, and specify the workspace if necessary.
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.
5. Click on Finish.
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.
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.
This is our project folder and pom.xml before adding the dependency.
Let us add a dependency for MySQL.
Let us save it and see the changes to our project structure.
Simply after saving the pom.xml file, we can see a new folder Maven Dependencies has been added.
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.
2. Type Maven in the search box and choose Existing Maven Project.
3. Type the path to your project, or browse it using your system file explorer.
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.
We can also go into the project folder and see.
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.
2. Type clean verify in the Goals section.
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.
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: