lunedì 20 maggio 2013

How to integrate Maven and Eclipse with m2e-wtp

Abstract

In this post I'll show you how to integrate Maven and Eclipse to develop web applications, using m2e-wtp. It is an Eclipse plugin that includes a set of connectors that help you to use all Maven features for Eclipse WTP projects. In the sections above, I'll show you how to install m2e-wtp and how to setup a new Dynamic Web Project with Maven in Eclipse Juno.

Requires

In order to install m2e-wtp, you must have previously installed Eclipse "for EE developers" 3.7+ version for you OS. Also, you need an active Internet connection to install the plugin from repository.

Installation

Before start, check the installation notes on m2e-wtp web site. If everything is ok, run Eclipse and select Help->Install New Software...
Click on the top right button "Add", Eclipse will show a dialog box called "Add Repository". Digit m2e-wtp in Name field and http://download.eclipse.org/m2e-wtp/releases/juno/ in Location field (http://download.eclipse.org/m2e-wtp/releases/kepler/ for Eclipse Kepler) and confirm by pressing the OK button (Fig 1)
Fig. 1

Now, select all elements in the list and then Next, in the "Install Details" window click Next, now accept the licence agreements and click Finish. Eclipse will download and install the plugin, if it shows a "Security Warning" window, don't mind and click on OK.
When it finish, restart Eclipse. Now you're ready to create a new project.

A new Project

Before creating a new project be sure to have a Tomcat server in Eclipse. If you don't, create a new one as described in this guide.
Also, if you are connected to the Internet by a proxy server, you must have a properly configured Maven installation.
In order to create a new Dynamic Web Project, click on File menu, then New->Other, and select Maven->New Maven Project from the list and press Next (Fig 2).
Fig. 2

Leave the default configuration for the first screen. Now select the archetype: select "All Catalogs" in Catalog field, and search for "maven-archetype-webapp" and select it (Fig. 3)
Fig 3

then click Next. Now insert the project name, you have to insert both Group ID and Artifact ID fields (for more details about them read the Maven naming conventions). I used "test" for Group ID and "webapptest" for Artifact ID. Click Finish to create the project.
Now you have to add the server runtime library, right-click on the project name and select "Properties", then,  in the left tree, select "Java Build Path". Now select the "Libraries" tab, and click on the "Add Library..." button. Eclipse will pop-up a window, select "Server Runtime" and select your server; confirm clicking on Finish then Ok (Fig 4).
Fig. 4

As you can see, in "Java Resources" Eclipse added the server libraries and 2 folders:

  • src/test/java that contains junit classes
  • src/main/java that contains the project logic (e.i. beans, servlets and so on)
The configuration files (such as web.xml) and jsp views can be placed in "Deployed resources" -> webapp.
For more informations about maven folders read "Maven Standard Directory Layout".

Conclusions

In this post I showed you how to install m2e-wtp and how to create a new J2EE project with Maven. Now you're able to run the project as a normal "Dynamic Web Project" but with the Maven features.

Nessun commento:

Posta un commento