------ Guide to using Eclipse with Maven 2.x ------ Bernd Mau, mau@hhla.de ------ 2010-01-21 ------ ~~ Licensed to the Apache Software Foundation (ASF) under one ~~ or more contributor license agreements. See the NOTICE file ~~ distributed with this work for additional information ~~ regarding copyright ownership. The ASF licenses this file ~~ to you under the Apache License, Version 2.0 (the ~~ "License"); you may not use this file except in compliance ~~ with the License. You may obtain a copy of the License at ~~ ~~ http://www.apache.org/licenses/LICENSE-2.0 ~~ ~~ Unless required by applicable law or agreed to in writing, ~~ software distributed under the License is distributed on an ~~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY ~~ KIND, either express or implied. See the License for the ~~ specific language governing permissions and limitations ~~ under the License. ~~ NOTE: For help with the syntax of this file, see: ~~ http://maven.apache.org/doxia/references/apt-format.html Guide to using Eclipse with Maven 2.x This mini guide explains howto use Maven 2 in Eclipse IDE. * {{{Maven_2_repository}Maven 2 repository}} * {{{Maven_as_an_external_tool}Maven as an external tool}} * {{{Simple_Project}Simple Project}} * {{{Multiple_Module_Project}Multiple Module Project}} * {Maven 2 repository} Eclipse needs to know the path to the local maven repository. Therefore the classpath variable has to be set. Execute the following command: +----+ mvn -Declipse.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. * {Maven as an external tool} 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. . When you set up a new external launcher (from the menu bar, select Run > External Tools. Select Program) you can refer to in the location field. Furhtermore refer to as the working directory and specify the maven goals of your choice as arguments, e.g. . For further information please refer to the eclipse help. * {Simple Project} 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: +----+ mvn eclipse:eclipse +----+ 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. * {Multiple Module Project} 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_Project_Layout}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 {{{http://maven.apache.org/guides/mini/guide-multiple-modules.html}recommended hierachical project layout}}. ** {Step by Step from Scratch} Supposing eclipse is your favorite SCM client, this step by step example shows how to set up a new mutiple module project. [[1]] Set up a new eclipse workspace called and add the classpath variable as {{{Maven_2_repository}described above}}. [[1]] Open the command line shell and change to the newly created workspace directory. [[1]] From the command line change to newly created workspace and create a new maven project using the archetype plugin. +----+ mvn archetype:generate -DgroupId=guide.ide.eclipse -DartifactId=guide-ide-eclipse +----+ [[1]] Create a new simple project inside the workspace with eclipse (From the menu bar, select File >New >Project. Select Simple >Project). Eclipse will create a simple <.project>-file for your -project and you should be able to see the -file. [[1]] Delete the -folder and open the -file to change the packaging of your parent project to +----+ pom +----+ [[1]] From the command line change to the project directory and create some modules. +----+ 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 +----+ [[1]] Add the newly created modules to your parent pom. +----+ guide-ide-eclipse-site guide-ide-eclipse-core guide-ide-eclipse-module1 +----+ [[1]] Add the parent to the POMs of the new modules: +----+ guide.ide.eclipse guide-ide-eclipse 1.0-SNAPSHOT +----+ [[1]] Add dependency from to the -module: +----+ guide.ide.eclipse.core guide-ide-eclipse-core 1.0-SNAPSHOT +----+ [[1]] Install the project in your local repository and generate the eclipse files: +----+ mvn install mvn eclipse:eclipse +----+ [[1]] Check in your project using the eclipse team support (select from the context menu Team >Share Project). Don not check in the generated eclipse files. If you use CVS you should have a <.cvsignore>-file with the following entries for each module: +----+ 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 . 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 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 for each module (see also {{{Maven_as_an_external_tool}Maven as an external tool}}). Of course all referenced artifacts have to be available from your maven repository. ** {Flat Project Layout} It is possible to move the parent POM in its own directory on the same level with the referenced modules. [[1]] Create a new directory under called and move the parent POM to it. [[1]] Change the module references in the parent POM to: +----+ ../guide-ide-eclipse-site ../guide-ide-eclipse-core ../guide-ide-eclipse-module1 +----+ []