RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Integrate the Eclipse Web Tools Platform and Maven : Page 3

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

This section mainly deals with using Maven to perform the basic M2 plugin task of modifying a classpath by adding jars. It details how you can edit the pom.xml file using the editor and the repository search tool, and explains how to launch Maven goals. More trivially, it demonstrates how to launch and debug a "Mavenized" project using WTP. By the end, you will have written some code and created a very basic MVC application.

Figure 5. Repository Search for Adding Dependencies: Begin by adding a few dependencies.

In order to verify that the application works without any issues related to tag libraries, you will be using the displaytag library.

Dependency Management
Begin by adding a few dependencies. In the repository search (launched by right clicking on the project and in the pop up menu navigating to Maven→Add Dependencies), type in "tomcat," select the highlighted servlet-api jar, and press the "OK" button to add the jar into the POM (see Figure 5).

Open the pom.xml file. In the editor, click on the "source" tab (if needed). Using the standard content assist in the editor, insert "provided" in the scope (see Figure 6). If you encounter any difficulty, just type <scope>provided</scope> in the appropriate location. Refer to the pom.xml file in Figure 6.

Likewise, add the JUnit and displaytag dependencies. The resulting pom.xml file looks like this:

Figure 6. The POM Editor: Using the standard content assist in the editor, insert "provided" in the scope.

<?xml version="1.0" encoding="UTF-8"?>

Figure 7. Repository Search Highlights Existing Dependencies: Using the standard content assist in the editor, insert "provided" in the scope.

For JUnit's and Tomcat's servlet-api jars, the scope is test and provided, respectively.

Note: The servlet-api jar is not really needed when developing with WTP. However it's needed for running Maven 2 goals from the command line.

The next time you perform the same search, the newly added dependencies will display in red to indicate their existence in the pom.xml file (see Figure 7). You won't be able to add them again unless the dependency is first removed.

Maven Run Configurations
Create a run configuration for Maven called sample1.package by using the tool bar button circled in red in Figure 8. Then click on "Open External Tools Dialog." Follow the instructions to create a new Maven run configuration by selecting "Maven Build" and pressing the button pointed to by the red arrow in Figure 8. Use the buttons "Browse Workspace" and "Select" to set the Base directory and the Goals, respectively.

The following is the data for your Maven run configuration:

Figure 8. Using External Tools for Maven Run Configurations: Create a new Maven run configuration.
  • Name: sample1.package
  • Base Directory: ${workspace_loc:/sample1}
  • Goals: package

Configuring such goals also allows you to pass additional arguments and VM arguments to the Maven command. Press the "Run" button. Thereafter, wait for the jars to be downloaded into the local repository and monitor the download progress from the Eclipse console.

After executing the target once, you can quickly access it using a drop down in the toolbar menu (see Figure 9).

For additional common Maven goals such as clean, test, and so on, right click on the project, execute them from the popup menu "Run As," and select the appropriate ready-to-use Maven goal.

You have now progressed to the point where you can add some code.

Figure 9. Recently Used Maven Run Configurations: You can quickly access the target.

Adding Some Code
You can create servlets and JavaServer Pages (JSPs) using the wizard you'll find in File→New→Other→Web. If you used this wizard to create a servlet, verify that the servlet is registered in web.xml (as it should). If it is not, web.xml is also provided with the downloadable code for the example application.

In this simple application example, index.jsp forwards to a servlet, which in turn forwards to a view JSP. The application also makes use of the displaytag tag library to display a table. You can use the downloadable code to quickly create the application. You can easily unzip it and copy the src folder contents into the src folder of the sample1 project. The pom.xml file, which is outside the src folder anyway, is the only other file that's code-related.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date