Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Integrate the Eclipse Web Tools Platform and Maven : Page 2

Leveraging the Eclipse Web Tools Platform (WTP) and the M2Eclipse (M2) plugin in the same project has compelling benefits for the Java web developer.


advertisement
Project Setup
In this section, you will create a WTP dynamic web application project and enable Maven support using the M2 plugin. More specifically, you will learn the details behind the following tasks:
  • Modifying the project folder structure and Java build settings so that both correspond to Maven
  • Modifying a specific WTP metadata file for the same purpose
  • Making WTP use the pom.xml fully

Before you get started, explore the M2 plugin briefly by creating a new Maven web project.

New Maven Web Project
Launch the "New Maven2 project" wizard by navigating to File→New→Other→Maven→Maven Project. Enter the data in Table 2 below into the wizard screen pages. Accept defaults for all data except those shown in yellow-highlighted bold.



Page Data
Page 1
Project Name tempForRef
"Create in Workspace" radio button Select this button
Page 2
Group ID tempForRef
Artifact ID tempForRef
Version 0.0.1-SNAPSHOT
Packaging war
Description Leave blank or fill in with whatever is appropriate
Project Layout Ensure these checkboxes are checked: src/main/java, src/main/resources, src/test/java, src/test/resources, and src/main/webapp
Page 3 Ignore this page
Table 2. Data Details for Creating Maven 2 Project

You may be wondering whether you can convert this M2 project into a WTP dynamic web project. Yes, you can. But it's easier to convert a WTP dynamic web project into a M2 project than a M2 project into a dynamic web project.

Now you're ready to create a separate dynamic web project.

New Dynamic Web Project
Create a new dynamic project from the following menu path File→New→Other→Web→Dynamic Web Project. Use the project name sample1, and choose the defaults for everything else. If you are prompted to go into the "Java EE" perspective, accept it by pressing the "Yes" button.

Table 3 lists the values in each screen for reference. Note that the project name is the only value that needs to be entered.

Page Data
Page 1
Project Name sample1
Project Contents Use default (For Directory)
Target Runtime Apache Tomcat v6.0
Configurations Default Configuration for Apache Tomcat v6.0
Page 2
Dynamic Web Module Selected by default
Java Selected by default
Page 3
Context Root sample1
Content Directory WebContent
Java Source Directory src
Table 3. Data Details for Creating New Dynamic Web Project

It's now time to use the M2 plugin on this project.

Enable Maven
Right click on the project, and in the popup menu, enable Maven by navigating to the path Maven→Enable Dependency Management. This will launch the POM creation wizard (see Figure 3).

 
Figure 3. POM Creation Wizard: Enable Maven by launching the POM creation wizard.

Choose "war" for packaging, and press the "finish" button.

After enabling Maven you still have a few steps to complete the integration:

  1. Creating Maven folder structure
  2. Updating source folders
  3. Some manual tweaking of WTP internals
  4. Configuring J2EE Module dependencies

Create Maven Folder Structure
Open the navigator view using Window→Show View→Other→General→Navigator. By default the dynamic project has a different structure, which interferes with the integration. So give the project a typical Maven folder structure. You can do this in two ways:

  1. Using the navigator view, go into the Maven project you created (tempForRef). Copy the folders "src" and "target." Again in the navigator view, go into the project sample1 and carefully paste the folders there.
  2. Create these folders using File→New→Folder: src/main/java, src/main/resources, src/main/webapp, src/test/java, src/test/resources, target/classes, and target/test-classes.

Update Source Folders
By default the dynamic project also differs in its Java build settings (e.g., its source and output folders) from what's needed for M2 integration. You need to the configure source folders for Java code. You can do this using either a manual approach and an automatic approach through a menu. This exercise uses the manual approach because it enables you to get everything working with the least effort.

In the project sample1, use navigator view to open the file .classpath. Verify that it has the following contents.

<?xml version="1.0" encoding="UTF-8"?> <classpath> <classpathentry kind="src" path="src"/> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> <classpathentry kind="con"
path="org.eclipse.jst.server.core.container/org.eclipse.jst.server.tomcat.runtimeTarget/Apache Tomcat v6.0"/> <classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.module.container"/> <classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.web.container"/> <classpathentry kind="con" path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER"/> <classpathentry kind="output" path="build/classes"/> </classpath>

There may be some differences. For example, details of the Tomcat version mentioned in the XML may be different. In WTP 2.0.1 there is this entry for org.eclipse.jst.j2ee.internal.module.container, which is not there in WTP2.0. The highlighted sections are what you need to change. Therefore, don't copy and paste the entire XML text. Directly edit this file and make the highlighted changes:

<?xml version="1.0" encoding="UTF-8"?> <classpath> <classpathentry kind="src" path="src/main/java"/> <classpathentry kind="src" path="src/main/resources"/> <classpathentry kind="src" output="target/test-classes" path="src/test/java"/> <classpathentry kind="src" output="target/test-classes" path="src/test/resources"/> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> <classpathentry kind="con"
path="org.eclipse.jst.server.core.container/org.eclipse.jst.server.tomcat.runtimeTarget/Apache Tomcat v6.0"/> <classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.module.container"/> <classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.web.container"/> <classpathentry kind="con" path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER"/> <classpathentry kind="output" path="target/classes"/> </classpath>

Note: The version of Tomcat mentioned in the actual .classpath file might differ. Do not modify the Tomcat details when making the changes. Remember to save the changes when done.

Some Manual Tweaking of WTP Internals
After updating the source folders you need to do some additional tweaking for better integration. Open the project in navigator view and edit the file .settings/org.eclipse.wst.common.component. If required open the source tab in the editor and do the following:

  • Replace /WebContent with /src/main/webapp.
  • Replace build/classes with target/classes.

Delete the entry for the following:

<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/test/java"/> <wb-resource deploy-path="/WEB-INF/classes" source-path="/src/test/resources"/>

This should be the resulting XML:

<?xml version="1.0" encoding="UTF-8"?> <project-modules id="moduleCoreId" project-version="1.5.0"> <wb-module deploy-name="sample1"> <wb-resource deploy-path="/" source-path="/src/main/webapp"/> <wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/java"/> <wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/resources"/> <property name="context-root" value="sample1"/> <property name="java-output-path" value="target/classes"/> </wb-module> </project-modules>

 
Figure 4. J2EE Module Dependencies: Link the maven2 dependencies with the J2EE Module dependencies.

Remember to save the changes when done.

Carefully move the folders META-INF and WEB-INF under /WebContent to src/main/webapp. In the navigator view, delete the folders WebContent and build.

Configuring J2EE Module Dependencies
Launch the project properties by right clicking on the project and selecting "Properties" in the pop up menu. Select "J2EE Module Dependencies" in the left panel. Link the maven2 dependencies with the J2EE Module dependencies by checking the checkbox shown on the screen (see Figure 4). Ignore any other checkboxes.

Pressing "Apply" button should clear the warning shown in Figure 4.

Review your previous steps and ensure everything was done properly. At this point you should have no error messages or warnings related to the sample1 and tempForRef projects in the Problem view in Eclipse. You are now set for actual development.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap