How to Develop a Maven Project in Eclipse

As great as Maven is, it does make things a bit more complicated, including how you develop projects in different IDEs. If Maven is supposed to make building projects easier (among other things), but you can't use it in conjunction with an IDE, then what's the point? You shouldn't have to compromise between the two.

There are a few Maven Eclipse plugins to help you work with tools like Maven (like m2eclipse, Eclipse IAM), but I have to admit that I'm not a huge fan of the ones I've used. Personally, I think a hybrid between using Maven on the command line and Eclipse is the most productive way.

Eclipse Setup

To set up Maven with Eclipse, there are a few configurations you need to set. Just follow these instructions and you'll be good to go.

Open Eclipse and navigate to Window->Preferences . Then go to Java->Build Path->Classpath Variables within the preferences window. In the Classpath Variables pane, add a variable named "M2_REPO" with a path pointing to the directory of your local Maven repository. Depending on the operating system you're using, the repo may be in one of these directories:

  • Linux: /home/[username]/.m2/repository
  • Mac: /Users/[username]/.m2/repository
  • Windows: C:/Users/[username]/.m2/repository

Setting up a Maven Project

In the command line, navigate to the directory containing your existing POM file (or start a new Maven project and navigate to that directory).

Assuming you already have Maven installed, run the following command from the command line:

$ mvn eclipse:eclipse

Running this command will download and run the Maven-Eclipse plugin and create files for a new Eclipse project that are tailored for the given Maven project.

Back in Eclipse, select File->Import, then General->Existing Projects into Workspace. Browse to the directory containing your project from above and click Ok.

Your project should show up in the Projects field of the Import dialog. If you see the right project there, click Finish, and your project is now ready for development in Eclipse!

Note: Keep in mind that whenever you add a new dependency to your Maven POM file, you need to re-run the same mvn eclipse:eclipse command to update the .project Eclipse file, and then refresh your project within Eclipse. This will update your list of dependencies so Eclipse knows what to include during compile-time.

Have any questions? Think you have a better setup? Let us know in the comments!