Saturday, 13 August 2011

Maven and eclipse

Maven is a powerful tool to make building and managing java projects easier. It is extremely quick and easy to get a basic project setup using maven, but for the more advanced user there is a lot of features and even more plugins to extend the functionality.
Maven uses a  Project Object Model (POM.xml) file to describe and build your project. This is a maven build file that defines all the dependencies and rules needed to compile this project.
The pom.xml is located under
Open  a command window by running cmd.exe then navigate to the pom.xml file.
One feature of Maven is that we can have maven create the eclipse project for us. To create the eclipse project type
            mvn eclipse:eclipse -Dwtpversion=2.0
into the command window. 
This is going to create the eclipse project files such as .settings, .classpath, and .project. It is also going to download a lot of dependencies that we defined, so instead of having to hunt down dozens of jar libraries, we can tell maven what libraries we want, and it will download them for us.
Maven saves these library's to 
            C:\Users\<your login>\.m2\repository

Now open eclipse. When it asks for the workspace, enter in the directory you want to work in. This should not be the same directory you just build the project in, but the workspace directory should contain your project. So in my case my working directory is 
D:\java-rest\server and my project is in D:\java-rest\server\rest.
Once eclipse has opened we need to import our new project that we created with maven. So Select file->import->general->existing project into workspace. Select the directory with the .settings, .classpath, and .project files.

You probably have several build errors such as:
            Unbound classpath variable: 'M2_REPO/asm/asm/3.1/asm-3.1.jar'
This is because eclipse doesn't know where maven downloaded these jar files to. 
So we need to add maven integration with eclipse so we can automatically launch maven builds from eclipse. For this we will use a eclipse plug-in M2E
Luckily eclipse has a great plug-in architecture that makes it extremely easy download and install plugins directly from eclipse. 
To install plugins we need to go to help->install new software then paste in
into the "works with:" field.

If you want to save this site you can click add then give it a name so you can remember it later.
Click ok and wait for it to download the packages. then click the check box and click next. Finish the install.
Now we need to install the maven wtp for eclipse plugin -
Click the window -> preferences -> maven -> discovery ->open catalog.
Find and select the m2e-wtp plugin to check it off and click finish to install it. 
After these steps we have integrated maven into eclipse but we still need to tell eclipse that this specific project is a maven project. To convert the project to a maven project by right clicking on your project then configure-> convert to maven project

(above picture is skipping some menu items at the black bar to make the list shorter)

 - It looks like the m2e-wtp plugin is no longer available though the m2e catalog. But you can still install it using the install software method. With the same install instructions as we used to install m2e add the link to the help->install new software.
Updates at:

No comments:

Post a Comment