Sunday, April 5, 2009

Maven Eclipse Tomcat "There are no projects that can be added or removed from the server"

I started to give up my MyEclipse IDE and turn to use a simple Eclipse instead. The main reason is because I found Eclipse actually do what I want it to do, like hot deploy to tomcat. Frankly it's not as good as MyEclipse, but it does the job.

I hit the wall as soon as I start to use it. When I tried to get rid of all eclipse project files (e.g. .project and .classpath) by using mvn eclipse:clean command, I regreted 2 minutes afterward. It was because once the command mvn eclipse:eclipse finished, I switched back to Eclipse, imported the project once again and then added the project to Tomcat, Eclipse prompt me a message "There are no projects that can be added or removed from the server". No matter what I tried to do, like adding "EAR Libraries", "Web App Libraries" and even "Tomcat Server Runtime Library", it does not help a bit. And I could not find a single hint from the net to solve it.

After struggling a whole day, I looked at the eclipse maven plugin. I found the solution.

Since Eclipse is using WTP for web project, and Eclipse needs the WTP configuration in order to recognise it is a web project. If you create a dynamic web project from Eclipse, it works nicely. However, it does not work when you start your project using maven. In order to enable WTP using maven, first we have to add maven-eclipse-plugin and make sure the version of maven-eclipse-plugin is specified (2.6 is the latest version at the moment). Then we have to put a configuration property <wtpversion> and set it to either 1.0, 1.5 or 2.0. Other setting like none or R7 does not work. So here is my setting:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.6</version>
<configuration>
<buildOutputDirectory>src/main/webapp/WEB-INF/classes</buildOutputDirectory>
<downloadJavadocs>true</downloadJavadocs>
<downloadSources>true</downloadSources>
<wtpversion>2.0</wtpversion>
</configuration>
</plugin>

There are other settings like <wtpmanifest>, <wtpapplicationxml>, <manifest>. But they are not necessary in order to deploy to Tomcat. This trick also apply on other server like glassfish.

After I put the setting in, it works nice and sweet!

The environment:
Eclipse 3.4
Maven 2.0.8
maven-eclipse-plugin 2.6
Tomcat 5.5, 6.0
Glassfish v3-prelude
MacOSX 10.5

For more information: http://maven.apache.org/plugins/maven-eclipse-plugin/index.html

2 comments:

  1. THANK YOU SO MUCH for this post!!
    I met the issue just like u met and also looked for the solution a whole day.

    ReplyDelete
  2. Hi

    I was also getting the now famous "There are no projects that can be added or removed" message.

    Following the instructions above didn't seem to work on its own. I also had to fix the file: .settings/ org.eclipse.wst.common.component


    Note that there are some symptomatic problems

    1. We were able to do an Eclipse Export War and that works when deployed, but could not do an Eclipse deploy. If you have a problem using Export War there may be other problems to look for.


    2. Viewing the Project Explorer: It does not list anything called a "Deployment Descriptor" (web.xml) which generally shows up on an Eclipse Web project.

    Instead Project Explorer lists the following:
    [+] [source folder symbol] Java Resources
    [+] [blank - no symbol] Web Resources: src/main/webapp

    But nothing called Deployment Descriptor appeared even though there is clearly a web.xml under src/main/webapp/WEB-INF

    3. Also I was unable to add a new Servlet. Using File | New | Web |Servlet, the "Next" and "Finish" button were grayed out.

    I compared the Maven project with an ordinary Eclipse dynamic web project. What I discovered is that the .settings/ org.eclipse.wst.common.component file was not correct. It had duplicate entries for mapping class paths, was possibly missing some front slashes on the paths and had an extra argument in a property entry.

    When I fixed these, the problems and the two symptoms were both cured.

    ReplyDelete