Maven 2 (Part 3): Configuring eclipse for Apache Maven 2 projects
Today we're going on with the third part of our Maven 2 tutorial series. Because of the comment of Enrico I decided that this article will focus on how to configure eclipse for the usage of Maven 2 projects and how to generate the eclipse-specific files by using Maven 2. I will show these things by using the example project of part 1 and part 2 of our Maven 2 tutorial series.
As you remember I showed you some simple Maven tasks like
- mvn clean
- mvn test and
- mvn package
Today I will focus on the two other tasks
- mvn eclipse:clean and
- mvn eclipse:eclipse
Each of these two tasks are combining two informations. The first information is located before the colon-symbol: „eclipse“. This string defines that the Maven 2 eclipse plugin will be used. Maven 2 will automatically download this Plugin from the central Maven repository. The string after the colon-symbol („clean“ or „eclipse“) indicates the explicit target the Maven 2 eclipse plugin should execute.
Cleaning eclipse resources
Please navigate to the root directory of our example application and run
mvn eclipse:clean
If you now list the root directory of our project you will see that nothing has been changed. Why? This is quiet easy. If you run „mvn eclipse:clean“ Maven will use the Maven 2 eclipse plugin to remove the eclipse resources like the .settings-folder, the .project-file and the .classpath-file. In our case nothing has been deleted because our project never had eclipse-specific resources. To see how Maven 2 generates those things please go on reading this article. ;-)
Generating project-specific eclipse resources
Generating project-specific eclipse-resources is as easy as removing them. Simply navigate to your projects root directory and run
mvn eclipse:eclipse
Maven will now generate all eclipse resources needed to import the project into the eclipse IDE. Now the project is ready to become imported. But is eclipse ready to use it?
Important. Important! IMPORTANT! Please be so kind and do never commit these eclipse resources into your versioning system (e.g. subversion, cvs, git or mercurial). Do it only if you really hate your colleagues.
Importing the project
The following informations could be a little bit boring because it contains a small click-guidance on how to import the project to the eclipse IDE. Let's start by running our Maven 2 tasks again:
mvn eclipse:clean eclipse:eclipse
Now we ensured that all previous eclipse resources are removed and generated again. Open your eclipse IDE and click on „File --> Import“. Choose "Existing Projects into Workspace" and hit the "Next >"-button.
Make that the radiobutton is switched to "Select root directory" and hit the "Browse..."-button.
Navigate to your projects parent folder and select your project. Then hit the "OK"-button.
Make sure that the checkbox in front of your project is checked and press "Finish". Now eclipse starts to import the new project.
As you can see your project cannot be build. Why? Your eclipse IDE is missing the local Maven 2 repository in your build path.
Adding the local Maven 2 to eclipse build path
To let eclipse know where the local Maven 2 repository is located you should know where it is located. By default the Maven 2 repository is located at
/home/[USERNAME]/.m2/repository
on linux platforms and
C:\[PATH_TO_YOUR_HOME_FOLDER]\.m2\repository
on Windows platforms. It is important to configure this directory as a classpath variable in eclipse. To do so please click on "Window --> Preferences" in your eclipse IDE. Navigate to "Classpath variables" of the "Java"- subnode "Build Path" and press the "New..."-button. Enter "M2_REPO" as the name of the new classpath variable and the location of your Maven 2 repository as the associated path.
After that hit "OK" to create your classpath variable and again "OK" to apply your changes to the IDE preferences. Your eclipse IDE will now ask you to rebuild all projects because your classpath variables has been changes. Choose "Yes"!
As you can see your project has been successfully imported and you are able to start working on it.
For those of you who like some further informations I've added 3 hints to this article.
Hint: Relocating the local Maven 2 repository
Sometimes the default location of the Maven 2 repository isn't very nice. Therefore Maven 2 allow to change the repository location. To modify your repository location navigate to the directory
[PATH_TO_YOUR_MAVEN_INSTALLATION]/conf
Open the file "settings.xml" and uncomment the XML-node "localRepository" and switch it to whereever you want. Make sure that the XML-node "localRepository" is defined outside the comment and save it. After changing the location of your repository you should modify the classpath variable in your eclipse IDE too. Don't forget that! I've spend hours to find out why eclipse doesn't find my dependencies although they were placed in my repository before I recognized that I forget to update my classpath variable in eclipse.
Hint: Do not use network drives for the Maven 2 repository
Do not place your Maven 2 repository on network drives. If you do so your eclipse will loose performance due to the network roundtrips between eclipse and the repository.
Hint: After adding new dependencies
After you added some new dependencies please run
mvn clean eclipse:clean eclipse:eclipse
to regenerate the project-specific eclipse resources. Click on your projects root directory in the "Project Explorer" in your eclipse IDE and hit "F5" to let eclipse reload all resources. Not until that eclipse will find your new dependencies inside of your Java classes.
Previous Maven 2 articles:
Maven 2 (Part 1): Setting up a simple Apache Maven 2 Project
Maven 2 (Part 2): Dependencies, properties and scopes
- Login to post comments
Comments
Anonymous - Sun, 06/21/2009 - 14:52
Personnaly, I prefer to use eclipse:m2eclipse instead of eclipse:eclipse
For one thing, it groups all your maven dependencies together in your project/package view.
Patrick
Anonymous - Wed, 06/24/2009 - 17:45
Not sure what kind of grouping you mean, but eclipse:m2eclipse can produce an incompatible configuration, so it is better to use File / Import... / Maven Projects instead. There you can specify the project naming pattern, as well as assign a working set.
Anonymous - Sun, 06/21/2009 - 17:49
Can you please explain how to work with multi-module Maven2 Web projects? I have been trying this for so long but no luck.
Everytime I make some change in one of the module (which gets packaged as jar in the final web project), I have to manually run "mvn clean install" and then restart tomcat (from eclipse) to get the latest changes deployed properly.
Please share, if you have a step by step guide for Maven2, Eclipse and Tomcat integration.
Thanks a ton !!
Phillip Steffensen - Mon, 06/22/2009 - 08:45
If you did some changes on a module referenced by your application you always need to install it to your local repository. I think there ist no other way to install modules to your local Maven 2 repository than "maven install". OK, you may copy the modules by hand to your repository, but I don't recommend that because it is very very ugly.
In the next (4th) part of my Maven 2 series i'll write about webapplication archives and how to set them up on tomcat. I know there are a lot of eclipse-plugins, that can be used for tomcat integration on eclipse but I rarely used some of them. Therefore I'm going to show by an shell example. If there should be enough time left I'll try to write some things about the usage of eclipse integration plugins.
Anonymous - Tue, 06/23/2009 - 10:06
Cool..I am really looking forward to Eclipse+Maven2+Tomcat integration.
Bookmarked your blog :)
Anonymous - Wed, 06/24/2009 - 17:44
Here is the thing. With m2eclipse you can debug/deploy and edit running code in your workspace without installing into the Maven local repository.
Anonymous - Fri, 09/11/2009 - 07:46
Hi Phillip,
you didn't already write the fourth part of your Maven 2 series, did you? I am interested in this fourth part, too, even I already know about integration of Maven 2 or Tomcat into Eclipse, but I never did an integration of Maven 2 AND Tomcat.
Well, the last time when I had to use Tomcat and Eclipse was in 2005. In the meantime lot of things in Java World changed, so a refresh is highly welcome.
regards
Markus
Anonymous - Wed, 08/19/2009 - 02:03
Thanks for taking the time to put this on the web. It really helped me out. Well done!
Anonymous (not verified) - Fri, 12/11/2009 - 12:37
Hi,
I want to edit the M2_REPO variable but ecliipse is not allowing me to do this. Any suggestion.
Pascal (not verified) - Wed, 12/16/2009 - 16:54
My eclipse suddenly lost all maven dependencies entries of a multi module project.
Maybe a bug in m2eclipse as it happended after (at least) maven udpate dependencies.
He also lost connection between some module specifically the service an webapp one, consistently saying he could not find claxx X, proposing to add the service module to the class path to no avail.
Rebooting did not help, nor updage dependencies nor both.
mvn eclipse:clean eclispe:eclipse then reopening worked better, and maven dependencies node is back.
Amit Saha (not verified) - Sat, 11/26/2011 - 09:06
How to create an android project using maven in eclipse??
Please tell !!
Jimmy (not verified) - Thu, 03/22/2012 - 17:48
Pretty good. it is very useful for new.
question: it means we can develop the plugin in Eclipse IDE? when the development is finished, we can use mvn package to generate the plugin again?
please help, thanks! any reference book is welcome!