Converting a Java project to a Dynamic Web project in Eclipse

 

Unfortunatelly there is no easy way to convert project type in Eclipse. In order to do this conversion we need to get to the guts of the eclipse project settings the ".project" file.

 

The .project file is located in the root directory of your project and may look some thing like this:



    warewolf
    
    
    
    
        
            org.eclipse.jdt.core.javabuilder
            
            
        
        
            org.maven.ide.eclipse.maven2Builder
            
            
        
    
    
        org.maven.ide.eclipse.maven2Nature
        org.eclipse.jdt.core.javanature
    

 

The above example represents a maven project that has just been created from scratch

As you can see the Eclipse identifies the type of the project by analysing the "natures" of the project that are specified in the .project file. So in order to tell eclipse that we would like to enable dynamic web project features we need to add additional natures to our file. Please note that it is best to close eclipse while you are performing the modification. And here is the modified file that includes the changes:

 



    warewolf
    
    
    
    
        
            org.eclipse.jdt.core.javabuilder
            
            
        
        
            org.maven.ide.eclipse.maven2Builder
            
            
        

        
            org.eclipse.wst.common.project.facet.core.builder
            
            
        

        
            org.eclipse.wst.validation.validationbuilder
            
            
        
    
    
        org.maven.ide.eclipse.maven2Nature
        org.eclipse.jdt.core.javanature

        org.eclipse.jem.workbench.JavaEMFNature
        org.eclipse.wst.common.project.facet.core.nature

        org.eclipse.wst.common.modulecore.ModuleCoreNature

    


 

Once the modifications are done you can open the eclipse and open the properties of the project by right clicking on the project root and selecting properties (or use a shortcut ALT+Enter).

Then you need to select the Project Facets configurations and check the Dynamic Web Module (choose the servlet api version as you require)

Eclipse facets properties

 

BTW you may have troubles with the context root, which is by default is set to WebContext. If you want to change that you need to look in .settings/org.eclipse.wst.common.component and within that file modify the "context-root" property. Here is a sample file for "/mycontext" context:



 






                            
  

And you're done :-), but a word of advice after doing all of the above - I suggest simply to use RunJettyRun plugin instead. It will save you a lot of time and effort

This page was last updated on: 16/03/2011 17:29