Showing posts with label eclipse. Show all posts
Showing posts with label eclipse. Show all posts

Wednesday, 3 October 2012

Maven and eclipse part 2


Installing the maven eclipse plugin:

In my previous post i described how to use Maven to build an eclipse project to import. Now i will describe how to use the Maven Integration for Eclipse plugin to import a Maven project.

From the help menu select the Eclipse marketplace


Then search for the maven plugin


Then click the install button on the Maven Integration for Eclipse.

Then click next and finish the installation.

Once you have installed the maven Integration for Eclipse, we can import our maven projects.
Open the File menu then select the import option.


From the import menu, we want to import an Existing Maven project. This is located under the Maven folder.


Now browse to the root directory of your project. This is the folder with your parent maven pom file.

Now you can select finish. This will import your project as a Maven project using the Maven integration.

You can now build the project automatically using Maven from Eclipse.

One issue i ran into with Maven in combination with eclipse and wtp is that it wasn't deploying my other pom dependencies correctly so i had to disable "Resolve dependencies from Workspace projects" from the properties menu






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
            <repository>\server\rest\pom.xml
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)

*Update
 - 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:

http://download.jboss.org/jbosstools/updates/m2eclipse-wtp/
                                or 
http://m2eclipse.sonatype.org/sites/m2e-extras

Tomcat eclipse integration

Now we need to add the tomcat server to eclipse:
A good blog post about this is here
First we need to make sure that the server tab is showing in eclipse. Select 
eclipse -> window-> show view-> servers or window-> show view-> other -> find the servers.
Now in the server view right click and select new server.
Select the Apache tomcat 7 server.

Using the default settings above should be fine.
In the next step you need to tell eclipse where Apache is installed to. Under Tomcat installation directory click "Browse.." and navigate to the directory you installed tomcat. For me it was
           

Now click next. Now we can tell tomcat what resources to run. The resources is our rest application. So click the "rest" resource and select add, then click finish.



To test right click on the server and select start.
If you get an error saying the port 8080 is already in use you need to make sure that the tomcat service isn't running. To stop the service run administrative tools from the start menu then select services. Find the Apache Tomcat service and stop it. Also set it to manual start-up, since this is your dev machine you don't need it to always be running. Eclipse will launch it, when you start debugging your project.