This mini guide explains howto use Maven 2 in Eclipse IDE.
Eclipse needs to know the path to the local maven repository. Therefore the classpath variable M2_REPO has to be set. Execute the following command:
mvn -Declipse.workspace=<path-to-eclipse-workspace> eclipse:add-maven-repo
Restart your Eclipse IDE (if you already have it open) for this change to take effect.
You can also define a new classpath variable inside eclipse: From the menu bar, select Window > Preferences. Select the Java > Build Path > Classpath Variables page.
You might want to execute some maven goals from inside eclipse. This is possible by configuring an external launcher. It is best practice to prepare eclipse by adding a variable, which points to your local maven excutable (mvn.bat/mvn). From the menu bar, select Window > Preferences. Select the Run/Debug > String Substitution. Add a new variable e.g. maven_exec. When you set up a new external launcher (from the menu bar, select Run > External Tools. Select Program) you can refer to maven_exec in the location field. Furhtermore refer to project_loc as the working directory and specify the maven goals of your choice as arguments, e.g. eclipse:eclipse. For further information please refer to the eclipse help.
If you have a simple java project which is made up of only one module, using eclipse is very simple. To generate the eclipse project files from your POM you execute the following command:
If you have created or checked out the project with eclipse, you only have to refresh the project in your workspace. Otherwise you have to import the project into your eclipse workspace (From the menu bar, select File >Import >Existing Projects into Workspace). In the latter case the project (directory) should not be located in your workspace, because eclipse might come into trouble, especially if you want to use eclipse as the scm client.
Due to the workspace idea many eclipse users are used to a flat layout and therefore want to keep this structure, which is possible but not recommended. Actually, the only reason for a flat multiple module project layout is the possibility to checkout and edit the parent POM without checking out the whole project. The following sample shows how to handle maven multiple module projects with eclipse while keeping the recommended hierachical project layout.
Supposing eclipse is your favorite SCM client, this step by step example shows how to set up a new mutiple module project.
mvn archetype:generate -DgroupId=guide.ide.eclipse -DartifactId=guide-ide-eclipse
cd guide-ide-eclipse mvn archetype:generate -DgroupId=guide.ide.eclipse -DartifactId=guide-ide-eclipse-site mvn archetype:generate -DgroupId=guide.ide.eclipse.core -DartifactId=guide-ide-eclipse-core mvn archetype:generate -DgroupId=guide.ide.eclipse.module1 -DartifactId=guide-ide-eclipse-module1
<modules> <module>guide-ide-eclipse-site</module> <module>guide-ide-eclipse-core</module> <module>guide-ide-eclipse-module1</module> </modules>
<parent> <groupId>guide.ide.eclipse</groupId> <artifactId>guide-ide-eclipse</artifactId> <version>1.0-SNAPSHOT</version> </parent>
<dependency> <groupId>guide.ide.eclipse.core</groupId> <artifactId>guide-ide-eclipse-core</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
mvn install mvn eclipse:eclipse
target .classpath .project .wtpmodules
Even the parent project should have this .cvsignore-file. Eclipse will automatically generate a new simple .project-file when you check out the project from the repository.
From now on you have different options to proceed. If you are working on all modules simultanously and you rather have eclipse project dependencies than binary dependencies, you should set up a new workspace and import all projects form step-by-step/guide-ide-eclipse. Note, you have to delete the .project-file of your parent project before. The result is equals to checking out the whole project from the command line, running mvn eclipse:eclipse and finally importing the projects into your eclipse workspace. In both cases you will be able to synchronize your changes using eclipse.
In case of large projects whith many people it can be quite tedious to check out all modules and keep them up to date. Especially if you are only interested in one or two modules. In this case using binary dependencies is much more comfortable. Just check out the modules you want to work on with eclipse and run mvn eclipse:eclipse for each module (see also Maven as an external tool). Of course all referenced artifacts have to be available from your maven repository.
It is possible to move the parent POM in its own directory on the same level with the referenced modules.
<modules> <module>../guide-ide-eclipse-site</module> <module>../guide-ide-eclipse-core</module> <module>../guide-ide-eclipse-module1</module> </modules>