jst j2ee
Integrating existing maven project in Eclipse WTP.
WTP LogoWTP Home
Intoduction
 

This article walkthroughs the steps required to integrate an existing maven project structure in the eclipse WTP platform. This integration uses the flexibile project structure introduced in the WTP M3 release.

 
Pre-requisites
 

Maven should be installed and configured, if not it can be downloaded from http://maven.apache.org/.
This article also assumes that you are familiar with maven project artifacts and the maven eclipse plugin.

 
A Sample Maven Project : WebSample
 

Unzip MavenWebSample.zip. Our MavenWebSample has two projects, a web project and a utility project. The web project "csrweb" contains a HelloWorldServlet and a web resource index.html.
The utility project "bedrock" has a class Util which is used by HelloWorldServlet.

Please build the utility project by going to command prompt and typing maven jar:install and the web project by typing maven war:install

Here is the eclipse enabled project WebSample.zip.

 
Configuring the Web Project
 
  1. Add the follwing in the project.xml of the csrweb to set the bedrock as the dependent project of the csrweb, since csrweb uses bedrock project, setting this dependency in the eclipse will make sure proper project build order.

       <dependency>
          <groupId>WebSample</groupId>
          <artifactId>bedrock</artifactId>
          <version>1.0</version>
          <properties>
            <war.bundle>true</war.bundle>
            <eclipse.dependency>true</eclipse.dependency>
          </properties>
        </dependency>
       
  2. Create a file project.properties under the csrweb folder, the folder where you will find project.xml. Now add the following in this file.
    				
    maven.eclipse.projectnatures=org.eclipse.jst.j2ee.web.WebNature,org.eclipse.jdt.core.javanature,org.eclipse.wst.common.modulecore.ModuleCoreNature
    maven.eclipse.buildcommands=org.eclipse.wst.common.modulecore.DeployableModuleBuilder,org.eclipse.jdt.core.javabuilder,org.eclipse.jst.j2ee.web.LibDirBuilder,org.eclipse.wst.validation.validationbuilder,org.eclipse.jst.j2ee.LibCopyBuilder,org.eclipse.wst.common.modulecore.LocalDependencyResolver
    
  3. Now open the command prompt, go to the csrweb folder and execute "maven eclipse". You will see that two files are generated .project and .classpath

    Open the .project and remove the first buildcommand javabuilder and the first nature org.eclipse.jdt.core.javanature.
    This is done to ensure that only one entry of javabuilder and org.eclipse.jdt.core.javanature exists.
    The correct order of builder and natures are needed to ensure proper functionality.The final .project should look like this.



  4. Create a .j2ee file in the csrweb project
    								
    <?xml version="1.0" encoding="UTF-8"?>
    <j2eesettings version="600">
    <moduleversion>23</moduleversion>
    <webcontent>src/webapp</webcontent>
    <context-root>csrweb</context-root>
    </j2eesettings>
    
    The moduleversion denotes the servlet version, for e.g. enter 22 for Servlet version 2.2, 23 for 2.3 and 24 for 2.4,
    this should match the version specified in the project.xml, in the dependency section of the servletapi.


  5. Now create a .wtpmodule file in the csrweb project and put the following content. The xsd of the .wtpmodule is defined elsewere in the WTP.
    <?xml version="1.0" encoding="UTF-8"?>
    <project-modules id="moduleCoreId">
        <wb-module deploy-name="csrweb.war">
            <wb-module module-type-id="jst.web"/>
            <wb-resource deploy-path="/" source-path="/csrweb/src/webapp"/>
            <wb-resource deploy-path="/WEB-INF/classes" source-path="/csrweb/src/main/java"/>
    
                    
            <dependent-module deploy-path="WEB-INF/lib" handle="module:/resource/csrweb/Library.jar">
                <dependency-type>uses</dependency-type>
            </dependent-module>
                    
            <dependent-module deploy-path="/WEB-INF/lib" handle="module:/resource/bedrock/bedrock.jar" >
                <dependency-type>uses</dependency-type>
            </dependent-module>
        </wb-module>
        <wb-module deploy-name="Library.jar">
            <wb-module module-type-id="jst.utility"/>
            <wb-resource deploy-path="/" source-path="/csrweb/LibraryContents"/>
        </wb-module>
    </project-modules>
    
    

    For quick overview the line <wb-resource deploy-path="/" source-path="/csrweb/src/webapp"/> implies that all the contents of source-path will be placed
    in the csrweb/.deployables folder after the build.
    Similarly the line <wb-resource deploy-path="/WEB-INF/classes" source-path="/csrweb/src/main/java"/>
    implies that after the build of the java files under the source-path, all the .class will be in the csrweb/.deployables/WEB-INF/classes


  6. Note that you will have to create a folder META-INF and MANIFEST.MF manually and add the necessary jars as per your requirement, eclipse WTP
    as of now doesn't generate this file. You will also need to put this file as the wb-resource in the .wtpmodule file as described above.
    The csrweb has the MANIFEST.MF in the folder csrweb/src/webapp/META-INF
Configuring the Util project
 

  1. Create project.properties in the bedrock folder
    maven.eclipse.projectnatures=org.eclipse.wst.common.modulecore.ModuleCoreNature
    maven.eclipse.buildcommands=org.eclipse.wst.common.modulecore.DeployableModuleBuilder,org.eclipse.jdt.core.javabuilder,org.eclipse.wst.common.modulecore.LocalDependencyResolver
    
  2. Now open the command prompt, go to the bedrock folder and execute "maven eclipse". You will see that two files are generated .project and .classpath

    Open the .project and remove the first buildcommand javabuilder.This is done to ensure that only one entry of javabuilder exists.
    The correct order of builder and natures are needed to ensure proper functionality.The final .project should look like this.



  3. Now create a .wtpmodule file in the bedrock project and put the following content.
    <?xml version="1.0" encoding="UTF-8"?>
    <project-modules id="moduleCoreId">
        <wb-module deploy-name="bedrock.jar">
            <wb-module module-type-id="jst.utility"/>
            <wb-resource deploy-path="/" source-path="/bedrock/src/java"/>
        </wb-module>
    
    </project-modules>
    
    

Bringing the project in the eclipse
 

  1. From the Window->Preferences set the classpath variable MAVEN_REPO to point to the repository of the maven.



  2. Import the projects using File->Import->Existing Project into workspace.



  3. Go to the csrweb, project Properties->Java Build Path->Source and add select Add Folder, point to the csrweb/LibraryContents

Running the Servlet in the Tomcat Server
 
  1. Open the J2EE perspective.


  2. From the Window->Preferences, setup the Tomcat Server runtime



  3. Set up the server from the Servers view in the J2EE perspective


  4. Set up the server in the web project



  5. Build the projects.


  6. Run the web project csrweb by right clicking and choosing Run As->Run on Server