2012年5月23日水曜日

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:

view plaincopy to clipboardprint?
<!--?xml version="1.0" encoding="UTF-8"?-->
<projectdescription>
<name>warewolf</name>
<comment></comment>
<projects>
</projects>
<buildspec>
<buildcommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildcommand>
<buildcommand>
<name>org.maven.ide.eclipse.maven2Builder</name>
<arguments>
</arguments>
</buildcommand>
</buildspec>
<natures>
<nature>org.maven.ide.eclipse.maven2Nature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectdescription>


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:


view plaincopy to clipboardprint?
<!--?xml version="1.0" encoding="UTF-8"?-->
<projectdescription>
<name>warewolf</name>
<comment></comment>
<projects>
</projects>
<buildspec>
<buildcommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildcommand>
<buildcommand>
<name>org.maven.ide.eclipse.maven2Builder</name>
<arguments>
</arguments>
</buildcommand>

<buildcommand>
<name>org.eclipse.wst.common.project.facet.core.builder</name>
<arguments>
</arguments>
</buildcommand>

<buildcommand>
<name>org.eclipse.wst.validation.validationbuilder</name>
<arguments>
</arguments>
</buildcommand>
</buildspec>
<natures>
<nature>org.maven.ide.eclipse.maven2Nature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>

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

<nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>

</natures>
</projectdescription>


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)

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:

view plaincopy to clipboardprint?
<project-modules id="moduleCoreId" project-version="1.5.0">
<wb-module deploy-name="shoppingcart">
<wb-resource deploy-path="/" source-path="/mycontext">
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/java">
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/resources">
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/test/java">
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/test/resources">
<property name="java-output-path">
<property name="context-root" value="/mycontext">
</property></property></wb-resource></wb-resource></wb-resource></wb-resource></wb-resource></wb-module>
</project-modules>
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

0 件のコメント:

コメントを投稿